Sure. Just FYI. The temperature widgets started working again on my iOS app on my iPhone 6. But when I log into MyDevices on my PC (Windows 10) they are not showing up.
Code Begin:
Network network;
CayenneMQTTClient mqttClient;
#define SPA_CHANNEL 20
#define WATERFALL_CHANNEL 21
#define JETS_CHANNEL 22
#define POOL_LIGHT_CHANNEL 23
#define SPA_LIGHT_CHANNEL 24
#define DECK_LIGHT_CHANNEL 25
#define HEATER_CHANNEL 26
#define SPA_SET_CHANNEL 30
void outputMessage(CayenneMessageData* message)
{
int i;
switch (message->topic) {
case COMMAND_TOPIC:
printf("topic=Command");
break;
case CONFIG_TOPIC:
printf("topic=Config");
break;
default:
printf("topic=%d", message->topic);
break;
}
printf(" channel=%d", message->channel);
if (message->clientID) {
printf(" clientID=%s", message->clientID);
}
if (message->type) {
printf(" type=%s", message->type);
}
for (i = 0; i < message->valueCount; ++i) {
if (message->values[i].value) {
printf(" value=%s", message->values[i].value);
}
if (message->values[i].unit) {
printf(" unit=%s", message->values[i].unit);
}
}
if (message->id) {
printf(" id=%s", message->id);
}
printf("\n");
}
// Handle messages received from the Cayenne server.
void messageArrived(CayenneMessageData* message)
{
BOOL status;
UCHAR circuit;
long spaTemp;
outputMessage(message);
// Add code to process the message here.
if (message->topic == COMMAND_TOPIC)
{
if (message->channel == SPA_SET_CHANNEL)
{
spaTemp = strtol(message->values[0].value, NULL, 10);
printf("\nNew Spa Temp = %d\n", (int)spaTemp);
spaTemp = pool_ChangeSpaTemp(spaTemp);
CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, SPA_SET_CHANNEL, TYPE_TEMPERATURE, UNIT_FAHRENHEIT, spaTemp);
return;
}
if (message->values[0].value[0] == '1')
{
status = TRUE;
}
else
{
status = FALSE;
}
switch(message->channel)
{
case WATERFALL_CHANNEL:
circuit = WATERFALL_CIRCUIT;
break;
case SPA_CHANNEL:
circuit = SPA_CIRCUIT;
break;
case JETS_CHANNEL:
circuit = JETS_CIRCUIT;
break;
case POOL_LIGHT_CHANNEL:
circuit = POOL_LIGHT_CIRCUIT;
break;
case SPA_LIGHT_CHANNEL:
circuit = SPA_LIGHT_CIRCUIT;
break;
case DECK_LIGHT_CHANNEL:
circuit = DECK_LIGHT_CIRCUIT;
break;
case HEATER_CHANNEL:
// If this is a command message we publish a response to show we recieved it. Here we are just sending a default 'OK' response.
// An error response should be sent if there are issues processing the message.
CayenneMQTTPublishResponse(&mqttClient, message->clientID, message->id, NULL);
if (g_Heater)
{
// Send the updated state for the channel so it is reflected in the Cayenne dashboard. If a command is successfully processed
// the updated state will usually just be the value received in the command message.
CayenneMQTTPublishData(&mqttClient, message->clientID, DATA_TOPIC, message->channel, NULL, NULL, "1");
}
else
{
// Send the updated state for the channel so it is reflected in the Cayenne dashboard. If a command is successfully processed
// the updated state will usually just be the value received in the command message.
CayenneMQTTPublishData(&mqttClient, message->clientID, DATA_TOPIC, message->channel, NULL, NULL, "0");
}
return;
break;
}
pool_SetCircuit(circuit, status);
// If this is a command message we publish a response to show we recieved it. Here we are just sending a default 'OK' response.
// An error response should be sent if there are issues processing the message.
CayenneMQTTPublishResponse(&mqttClient, message->clientID, message->id, NULL);
// Send the updated state for the channel so it is reflected in the Cayenne dashboard. If a command is successfully processed
// the updated state will usually just be the value received in the command message.
CayenneMQTTPublishData(&mqttClient, message->clientID, DATA_TOPIC, message->channel, NULL, NULL, message->values[0].value);
}
}
// Connect to the Cayenne server.
int connectClient(void)
{
// Connect to the server.
int error = 0;
printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
if ((error = NetworkConnect(&network, CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
return error;
}
if ((error = CayenneMQTTConnect(&mqttClient)) != MQTT_SUCCESS) {
NetworkDisconnect(&network);
return error;
}
printf("Connected\n");
// Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed.
CayenneMQTTPublishData(&mqttClient, NULL, SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
CayenneMQTTPublishData(&mqttClient, NULL, SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "Linux");
CayenneMQTTSubscribe(&mqttClient, NULL, COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
CayenneMQTTSubscribe(&mqttClient, NULL, CONFIG_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
CayenneMQTTSubscribe(&mqttClient, NULL, DATA_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
CayenneMQTTSubscribe(&mqttClient, NULL, ANALOG_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
CayenneMQTTSubscribe(&mqttClient, NULL, ANALOG_COMMAND_TOPIC, CAYENNE_ALL_CHANNELS, NULL);
return CAYENNE_SUCCESS;
}
void myPublish(int Circuit, int Channel)
{
if (pool_GetCircuit(Circuit))
{
CayenneMQTTPublishData(&mqttClient, NULL, DATA_TOPIC, Channel, NULL, NULL, "1");
}
else
{
CayenneMQTTPublishData(&mqttClient, NULL, DATA_TOPIC, Channel, NULL, NULL, "0");
}
}
// Main loop where MQTT code is run.
void loop(void)
{
static int count = 0;
while (1) {
// Yield to allow MQTT message processing.
pool_Update();
CayenneMQTTYield(&mqttClient, 100);
count++;
if (count > 10)
{
count = 0;
// Publish some example data every second. This should be changed to send your actual data to Cayenne.
CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, 0, TYPE_TEMPERATURE, UNIT_FAHRENHEIT, g_PoolTemperature);
CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_FAHRENHEIT, g_PoolSetTemperature);
CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, 2, TYPE_TEMPERATURE, UNIT_FAHRENHEIT, g_AirTemperature);
CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, 3, TYPE_TEMPERATURE, UNIT_FAHRENHEIT, g_SpaSetTemperature);
CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, 4, NULL, NULL, g_Hour);
CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, 5, NULL, NULL, g_Minute);
myPublish(SPA_CIRCUIT, SPA_CHANNEL);
myPublish(JETS_CIRCUIT, JETS_CHANNEL);
myPublish(POOL_LIGHT_CIRCUIT, POOL_LIGHT_CHANNEL);
myPublish(SPA_LIGHT_CIRCUIT, SPA_LIGHT_CHANNEL);
myPublish(DECK_LIGHT_CIRCUIT, DECK_LIGHT_CHANNEL);
myPublish(WATERFALL_CIRCUIT, WATERFALL_CHANNEL);
if (g_Heater)
{
CayenneMQTTPublishData(&mqttClient, NULL, DATA_TOPIC, HEATER_CHANNEL, NULL, NULL, "1");
}
else
{
CayenneMQTTPublishData(&mqttClient, NULL, DATA_TOPIC, HEATER_CHANNEL, NULL, NULL, "0");
}
}
}
}
// Main function.
int main(int argc, char** argv)
{
pool_Init();
// Initialize the network.
NetworkInit(&network);
// Initialize the Cayenne client.
CayenneMQTTClientInit(&mqttClient, &network, username, password, clientID, messageArrived);
// Connect to Cayenne.
if (connectClient() == CAYENNE_SUCCESS) {
// Run main loop.
loop();
}
else {
printf("Connection failed, exiting\n");
}
return 0;
}