Skip to content

Commit 5e997e2

Browse files
authored
Revert "Add new data types and units."
1 parent df879cc commit 5e997e2

24 files changed

+844
-357
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=CayenneMQTT
2-
version=1.0.2-beta
2+
version=1.0.1
33
author=myDevices
44
maintainer=myDevices
55
sentence=Connect a device to the Cayenne dashboard using MQTT.

src/CayenneArduinoMQTTClient.h

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAL
2020

2121
#include "CayenneArduinoDefines.h"
2222
#include "CayenneMQTTClient/CayenneMQTTClient.h"
23-
#include "CayenneUtils/CayenneDataArray.h"
2423

2524
const int MAX_CHANNEL_ARRAY_SIZE = 4;
2625

@@ -163,6 +162,30 @@ class CayenneArduinoMQTTClient
163162
publishData(DATA_TOPIC, channel, data, type, unit);
164163
}
165164

165+
/**
166+
* Sends an array of measurements to a Cayenne channel
167+
*
168+
* @param channel Cayenne channel number
169+
* @param values Array of values to be sent
170+
* @param type Measurement type
171+
*/
172+
void virtualWrite(unsigned int channel, const CayenneDataArray& values, const char* type)
173+
{
174+
publishData(DATA_TOPIC, channel, values.getArray(), values.getCount(), type);
175+
}
176+
177+
/**
178+
* Sends an array of measurements to a Cayenne channel
179+
*
180+
* @param channel Cayenne channel number
181+
* @param values Array of values to be sent
182+
* @param type Measurement type
183+
*/
184+
void virtualWrite(unsigned int channel, const CayenneDataArray& values, const __FlashStringHelper* type)
185+
{
186+
publishData(DATA_TOPIC, channel, values.getArray(), values.getCount(), type);
187+
}
188+
166189
/**
167190
* Sends a response after processing a command
168191
*
@@ -267,40 +290,6 @@ class CayenneArduinoMQTTClient
267290
virtualWrite(channel, value, F(TYPE_DIGITAL_SENSOR), F(UNIT_DIGITAL));
268291
}
269292

270-
/**
271-
* Sends an acceleration value array to a Cayenne channel
272-
*
273-
* @param channel Cayenne channel number
274-
* @param x Acceration on the X-axis
275-
* @param y Acceration on the Y-axis
276-
* @param z Acceration on the Z-axis
277-
*/
278-
void accelWrite(unsigned int channel, float x, float y, float z)
279-
{
280-
CayenneDataArray values;
281-
values.add(x, 1);
282-
values.add(y, 1);
283-
values.add(z, 1);
284-
virtualWrite(channel, values.getString(), F(TYPE_ACCELERATION), F(UNIT_G));
285-
}
286-
287-
/**
288-
* Sends a GPS value array to a Cayenne channel
289-
*
290-
* @param channel Cayenne channel number
291-
* @param latitude Latitude in degrees
292-
* @param longitude Longitude in degrees
293-
* @param altitude Altitude in meters
294-
*/
295-
void gpsWrite(unsigned int channel, float latitude, float longitude, float altitude)
296-
{
297-
CayenneDataArray values;
298-
values.add(latitude, 5);
299-
values.add(longitude, 5);
300-
values.add(altitude, 1);
301-
virtualWrite(channel, values.getString(), F(TYPE_GPS), F(UNIT_METER));
302-
}
303-
304293
/**
305294
* Requests Server to re-send current values for all widgets.
306295
*/
@@ -354,9 +343,9 @@ class CayenneArduinoMQTTClient
354343
*/
355344
template <typename T>
356345
static void publishData(CayenneTopic topic, unsigned int channel, const T& data, const char* key = NULL, const char* subkey = NULL) {
357-
CayenneDataArray value;
358-
value.add(data);
359-
publishData(topic, channel, value.getString(), key, subkey);
346+
CayenneDataArray values;
347+
values.add(subkey, data);
348+
publishData(topic, channel, values.getArray(), values.getCount(), key);
360349
}
361350

362351
/**
@@ -369,42 +358,39 @@ class CayenneArduinoMQTTClient
369358
*/
370359
template <typename T>
371360
static void publishData(CayenneTopic topic, unsigned int channel, const T& data, const __FlashStringHelper* key, const __FlashStringHelper* subkey = NULL) {
372-
char keyBuffer[MAX_TYPE_LENGTH + 1] = { 0 };
373-
char subkeyBuffer[MAX_UNIT_LENGTH + 1] = { 0 };
374-
CayenneDataArray value;
375-
value.add(data);
361+
char keyBuffer[MAX_TYPE_LENGTH + 1];
362+
CayenneDataArray values;
363+
values.add(subkey, data);
376364
CAYENNE_MEMCPY(keyBuffer, reinterpret_cast<const char *>(key), CAYENNE_STRLEN(reinterpret_cast<const char *>(key)) + 1);
377-
if (subkey)
378-
CAYENNE_MEMCPY(subkeyBuffer, reinterpret_cast<const char *>(subkey), CAYENNE_STRLEN(reinterpret_cast<const char *>(subkey)) + 1);
379-
publishData(topic, channel, value.getString(), keyBuffer, subkeyBuffer);
365+
publishData(topic, channel, values.getArray(), values.getCount(), keyBuffer);
380366
}
381367

382368
/**
383369
* Publish value array using specified topic suffix
384370
* @param topic Cayenne topic
385371
* @param channel Cayenne channel number
386-
* @param data Data to send
372+
* @param values Array of values to be sent
373+
* @param valueCount Count of values in array
387374
* @param key Optional key to use for a key=data pair
388-
* @param subkey Optional subkey to use for a key,subkey=data pair
389375
*/
390-
static void publishData(CayenneTopic topic, unsigned int channel, const char* data, const char* key, const char* subkey) {
391-
CAYENNE_LOG_DEBUG("Publish: topic %d, channel %u, key %s, subkey %s, data %s", topic, channel, key, subkey, data);
392-
CayenneMQTTPublishData(&_mqttClient, NULL, topic, channel, key, subkey, data);
376+
static void publishData(CayenneTopic topic, unsigned int channel, const CayenneValuePair values[], size_t valueCount, const char* key) {
377+
CAYENNE_LOG_DEBUG("Publish: topic %d, channel %u, value %s, subkey %s, key %s", topic, channel, values[0].value, values[0].unit, key);
378+
CayenneMQTTPublishDataArray(&_mqttClient, NULL, topic, channel, key, values, valueCount);
393379
}
394380

395381
/**
396382
* Publish value array using specified topic suffix
397383
* @param topic Cayenne topic
398384
* @param channel Cayenne channel number
399-
* @param data Data to send
385+
* @param values Array of values to be sent
386+
* @param valueCount Count of values in array
400387
* @param key Optional key to use for a key=data pair
401-
* @param subkey Optional subkey to use for a key,subkey=data pair
402388
*/
403-
static void publishData(CayenneTopic topic, unsigned int channel, const char* data, const __FlashStringHelper* key, const char* subkey) {
389+
static void publishData(CayenneTopic topic, unsigned int channel, const CayenneValuePair values[], size_t valueCount, const __FlashStringHelper* key) {
404390
char keyBuffer[MAX_TYPE_LENGTH + 1];
405391
CAYENNE_MEMCPY(keyBuffer, reinterpret_cast<const char *>(key), CAYENNE_STRLEN(reinterpret_cast<const char *>(key)) + 1);
406-
CAYENNE_LOG_DEBUG("Publish: topic %d, channel %u, key %s, subkey %s, data %s", topic, channel, keyBuffer, subkey, data);
407-
CayenneMQTTPublishData(&_mqttClient, NULL, topic, channel, keyBuffer, subkey, data);
392+
CAYENNE_LOG_DEBUG("Publish: topic %d, channel %u, value %s, subkey %s, key %s", topic, channel, values[0].value, values[0].unit, keyBuffer);
393+
CayenneMQTTPublishDataArray(&_mqttClient, NULL, topic, channel, keyBuffer, values, valueCount);
408394
}
409395

410396
/**
@@ -482,8 +468,8 @@ void handleMessage(CayenneMessageData* messageData) {
482468
Request request = { messageData->channel };
483469
const char* response = NULL;
484470
CayenneMessage message(messageData);
485-
if (strlen(messageData->value)) {
486-
CAYENNE_LOG_DEBUG("In: value %s, channel %d", messageData->value, request.channel);
471+
if (strlen(messageData->values[0].value)) {
472+
CAYENNE_LOG_DEBUG("In: value %s, channel %d", messageData->values[0].value, request.channel);
487473
InputHandlerFunction handler = GetInputHandler(request.channel);
488474
if (handler && handler != InputHandler) {
489475
handler(request, message);
@@ -497,14 +483,14 @@ void handleMessage(CayenneMessageData* messageData) {
497483
}
498484
if(response == NULL) {
499485
// If there was no error, we send the new channel state, which should be the command value we received.
500-
CayenneArduinoMQTTClient::publishState(DATA_TOPIC, messageData->channel, messageData->value);
486+
CayenneArduinoMQTTClient::publishState(DATA_TOPIC, messageData->channel, messageData->values[0].value);
501487
}
502488
CayenneArduinoMQTTClient::responseWrite(response, messageData->id);
503489
}
504490

505491
#ifdef DIGITAL_AND_ANALOG_SUPPORT
506492
void handleAnalogMessage(CayenneMessageData* messageData) {
507-
float value = atof(messageData->value);
493+
float value = atof(messageData->values[0].value);
508494
char* response = NULL;
509495
if (value >= 0 && value <= 1) {
510496
double test = value * 255;
@@ -515,18 +501,18 @@ void handleAnalogMessage(CayenneMessageData* messageData) {
515501
else {
516502
response = ERROR_INCORRECT_PARAM;
517503
}
518-
CayenneArduinoMQTTClient::responseWrite(response, messageData->id);
504+
CayenneArduinoMQTTClient::responseWrite(messageData->channel, response, messageData->id);
519505
}
520506

521507
void handleDigitalMessage(CayenneMessageData* messageData) {
522508
char* response = NULL;
523-
if (messageData->value && strlen(messageData->value) == 1) {
524-
CAYENNE_LOG_DEBUG("dw %s, channel %d", messageData->value, messageData->channel);
525-
if (messageData->value[0] == '0') {
509+
if (messageData->values[0].value && strlen(messageData->values[0].value) == 1) {
510+
CAYENNE_LOG_DEBUG("dw %s, channel %d", messageData->values[0].value, messageData->channel);
511+
if (messageData->values[0].value[0] == '0') {
526512
digitalWrite(messageData->channel, LOW);
527513
CayenneArduinoMQTTClient::publishState(DIGITAL_TOPIC, messageData->channel, LOW);
528514
}
529-
else if (messageData->value[0] == '1') {
515+
else if (messageData->values[0].value[0] == '1') {
530516
digitalWrite(messageData->channel, HIGH);
531517
CayenneArduinoMQTTClient::publishState(DIGITAL_TOPIC, messageData->channel, HIGH);
532518
}
@@ -537,7 +523,7 @@ void handleDigitalMessage(CayenneMessageData* messageData) {
537523
else {
538524
response = ERROR_INCORRECT_PARAM;
539525
}
540-
CayenneArduinoMQTTClient::responseWrite(response, messageData->id);
526+
CayenneArduinoMQTTClient::responseWrite(messageData->channel, response, messageData->id);
541527
}
542528
#endif
543529

@@ -553,13 +539,13 @@ void CayenneMessageArrived(CayenneMessageData* message) {
553539
handleDigitalMessage(message);
554540
break;
555541
case DIGITAL_CONFIG_TOPIC:
556-
configChannel(CayenneArduinoMQTTClient::digitalChannels, message->channel, message->value);
542+
configChannel(CayenneArduinoMQTTClient::digitalChannels, message->channel, message->values[0].value);
557543
break;
558544
case ANALOG_COMMAND_TOPIC:
559545
handleAnalogMessage(message);
560546
break;
561547
case ANALOG_CONFIG_TOPIC:
562-
configChannel(CayenneArduinoMQTTClient::analogChannels, message->channel, message->value);
548+
configChannel(CayenneArduinoMQTTClient::analogChannels, message->channel, message->values[0].value);
563549
break;
564550
#endif
565551
default:
@@ -569,11 +555,14 @@ void CayenneMessageArrived(CayenneMessageData* message) {
569555
// CAYENNE_PRINT.print(message->type);
570556
// CAYENNE_PRINT.print(", ");
571557
// }
572-
// if (message->unit) {
573-
// CAYENNE_PRINT.print(message->unit);
574-
// CAYENNE_PRINT.print("=");
558+
// for (int i = 0; i < message->valueCount; ++i) {
559+
// if (message->values[i].unit) {
560+
// CAYENNE_PRINT.print(message->values[i].unit);
561+
// CAYENNE_PRINT.print("=");
562+
// }
563+
// CAYENNE_PRINT.print(message->values[i].value);
564+
// CAYENNE_PRINT.print(" ");
575565
// }
576-
// CAYENNE_PRINT.print(message->value);
577566
// CAYENNE_PRINT.println();
578567
//#endif
579568
break;

src/CayenneMQTTClient/CayenneMQTTClient.c

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ void MQTTMessageArrived(MessageData* md, void* userData)
3232
return;
3333
//Null terminate the string since that is required by CayenneParsePayload. The readbuf is set to CAYENNE_MAX_MESSAGE_SIZE+1 to allow for appending a null.
3434
((char*)md->message->payload)[md->message->payloadlen] = '\0';
35-
result = CayenneParsePayload(&message.type, &message.unit, &message.value, &message.id, message.topic, (char*)md->message->payload);
35+
message.valueCount = CAYENNE_MAX_MESSAGE_VALUES;
36+
result = CayenneParsePayload(message.values, &message.valueCount, &message.type, &message.id, message.topic, (char*)md->message->payload);
3637
if (result != CAYENNE_SUCCESS)
3738
return;
3839

@@ -109,24 +110,10 @@ int CayenneMQTTConnect(CayenneMQTTClient* client)
109110
*/
110111
int CayenneMQTTPublishData(CayenneMQTTClient* client, const char* clientID, CayenneTopic topic, unsigned int channel, const char* type, const char* unit, const char* value)
111112
{
112-
char buffer[CAYENNE_MAX_MESSAGE_SIZE + 1] = { 0 };
113-
int result = CayenneBuildTopic(buffer, sizeof(buffer), client->username, clientID ? clientID : client->clientID, topic, channel);
114-
if (result == CAYENNE_SUCCESS) {
115-
size_t size = strlen(buffer);
116-
char* payload = &buffer[size + 1];
117-
size = sizeof(buffer) - (size + 1);
118-
result = CayenneBuildDataPayload(payload, &size, type, unit, value);
119-
if (result == CAYENNE_SUCCESS) {
120-
MQTTMessage message;
121-
message.qos = QOS0;
122-
message.retained = (topic != COMMAND_TOPIC) ? 1 : 0;
123-
message.dup = 0;
124-
message.payload = (void*)payload;
125-
message.payloadlen = size;
126-
result = MQTTPublish(&client->mqttClient, buffer, &message);
127-
}
128-
}
129-
return result;
113+
CayenneValuePair valuePair[1];
114+
valuePair[0].value = value;
115+
valuePair[0].unit = unit;
116+
return CayenneMQTTPublishDataArray(client, clientID, topic, channel, type, valuePair, 1);
130117
}
131118

132119

@@ -267,6 +254,38 @@ int CayenneMQTTPublishDataFloat(CayenneMQTTClient* client, const char* clientID,
267254
return CayenneMQTTPublishData(client, clientID, topic, channel, type, unit, str);
268255
}
269256

257+
/**
258+
* Send multiple value data array to Cayenne.
259+
* @param[in] client The client object
260+
* @param[in] clientID The client ID to use in the topic, NULL to use the clientID the client was initialized with
261+
* @param[in] topic Cayenne topic
262+
* @param[in] channel The channel to send data to, or CAYENNE_NO_CHANNEL if there is none
263+
* @param[in] type Optional type to use for a type=value pair, can be NULL
264+
* @param[in] values Unit/value array
265+
* @param[in] valueCount Number of values
266+
* @return success code
267+
*/
268+
int CayenneMQTTPublishDataArray(CayenneMQTTClient* client, const char* clientID, CayenneTopic topic, unsigned int channel, const char* type, const CayenneValuePair* values, size_t valueCount)
269+
{
270+
char buffer[CAYENNE_MAX_MESSAGE_SIZE + 1] = { 0 };
271+
int result = CayenneBuildTopic(buffer, sizeof(buffer), client->username, clientID ? clientID : client->clientID, topic, channel);
272+
if (result == CAYENNE_SUCCESS) {
273+
size_t size = strlen(buffer);
274+
char* payload = &buffer[size + 1];
275+
size = sizeof(buffer) - (size + 1);
276+
result = CayenneBuildDataPayload(payload, &size, type, values, valueCount);
277+
if (result == CAYENNE_SUCCESS) {
278+
MQTTMessage message;
279+
message.qos = QOS0;
280+
message.retained = 1;
281+
message.dup = 0;
282+
message.payload = (void*)payload;
283+
message.payloadlen = size;
284+
result = MQTTPublish(&client->mqttClient, buffer, &message);
285+
}
286+
}
287+
return result;
288+
}
270289

271290
/**
272291
* Send a response to a channel.

src/CayenneMQTTClient/CayenneMQTTClient.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEAL
2121
#include "MQTTClient.h"
2222
#include "../CayenneUtils/CayenneDefines.h"
2323
#include "../CayenneUtils/CayenneUtils.h"
24+
#include "../CayenneUtils/CayenneDataArray.h"
2425

2526
#if defined(__cplusplus)
2627
extern "C" {
@@ -47,8 +48,8 @@ extern "C" {
4748
unsigned int channel; /**< The channel the message was received on. */
4849
const char* id; /**< The message ID, if it is a command message, otherwise NULL. */
4950
const char* type; /**< The type of data in the message, if it exists, otherwise NULL. */
50-
const char* unit; /**< The unit in the message, if it exists, otherwise NULL. */
51-
const char* value; /**< The value in the message, if it exists, otherwise NULL. */
51+
CayenneValuePair values[CAYENNE_MAX_MESSAGE_VALUES]; /**< The unit/value data pairs in the message. The units and values can be NULL. */
52+
size_t valueCount; /**< The count of items in the values array. */
5253
} CayenneMessageData;
5354

5455
typedef void(*CayenneMessageHandler)(CayenneMessageData*);
@@ -188,6 +189,19 @@ extern "C" {
188189
*/
189190
DLLExport int CayenneMQTTPublishDataFloat(CayenneMQTTClient* client, const char* clientID, CayenneTopic topic, unsigned int channel, const char* type, const char* unit, float value);
190191

192+
/**
193+
* Send multiple value data array to Cayenne.
194+
* @param[in] client The client object
195+
* @param[in] clientID The client ID to use in the topic, NULL to use the clientID the client was initialized with
196+
* @param[in] topic Cayenne topic
197+
* @param[in] channel The channel to send data to, or CAYENNE_NO_CHANNEL if there is none
198+
* @param[in] type Optional type to use for a type=value pair, can be NULL
199+
* @param[in] values Unit / value array
200+
* @param[in] valueCount Number of values
201+
* @return success code
202+
*/
203+
DLLExport int CayenneMQTTPublishDataArray(CayenneMQTTClient* client, const char* clientID, CayenneTopic topic, unsigned int channel, const char* type, const CayenneValuePair* values, size_t valueCount);
204+
191205
/**
192206
* Send a response to a channel.
193207
* @param[in] client The client object

src/CayenneMQTTClient/MQTTClient.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ int cycle(MQTTClient* c, Timer* timer)
273273
MQTTMessage msg;
274274
int intQoS;
275275
if (MQTTDeserialize_publish(&msg.dup, &intQoS, &msg.retained, &msg.id, &topicName,
276-
(unsigned char**)&msg.payload, &msg.payloadlen, c->readbuf, c->readbuf_size) != 1)
276+
(unsigned char**)&msg.payload, (int*)&msg.payloadlen, c->readbuf, c->readbuf_size) != 1)
277277
goto exit;
278278
msg.qos = (enum QoS)intQoS;
279279
deliverMessage(c, &topicName, &msg);

src/CayenneMQTTClient/MQTTClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
#define DLLExport
3434
#endif
3535

36-
#include <stdio.h>
3736
#include "../MQTTCommon/MQTTPacket.h"
37+
#include "stdio.h"
3838
#include "PlatformHeader.h"
3939

4040
#if defined(MQTTCLIENT_PLATFORM_HEADER)

0 commit comments

Comments
 (0)