Cayenne demo

Dependencies:   Cayenne-MQTT-mbed NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed Servo

Fork of Cayenne-X-NUCLEO-IDW01M1-TMP36 by myDevicesIoT

Committer:
rozendhyan
Date:
Tue Aug 29 17:11:37 2017 +0000
Revision:
9:668337c0fb8a
Parent:
8:f1e261b1e25b
remove not used argument & define

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:4d7a64104b46 1 /**
jburhenn 0:4d7a64104b46 2 * Example app for using the Cayenne MQTT mbed library to send data from a TMP36 sensor. This example uses
jburhenn 0:4d7a64104b46 3 * the X-NUCLEO-IDW01M1 WiFi expansion board via the X_NUCLEO_IDW01M1v2 library.
jburhenn 0:4d7a64104b46 4 */
jburhenn 0:4d7a64104b46 5
jburhenn 0:4d7a64104b46 6 #include "MQTTTimer.h"
jburhenn 0:4d7a64104b46 7 #include "CayenneMQTTClient.h"
jburhenn 0:4d7a64104b46 8 #include "MQTTNetworkIDW01M1.h"
jburhenn 0:4d7a64104b46 9 #include "SpwfInterface.h"
rozendhyan 6:cec678bb15ef 10 #include "BME280.hpp"
rozendhyan 7:fed254c30643 11 #include "LED.hpp"
rozendhyan 8:f1e261b1e25b 12 #include "Servo.h"
jburhenn 0:4d7a64104b46 13 // WiFi network info.
rozendhyan 6:cec678bb15ef 14 char* ssid = "megu megu fire";
rozendhyan 6:cec678bb15ef 15 char* wifiPassword = "66666667";
jburhenn 0:4d7a64104b46 16
jburhenn 0:4d7a64104b46 17 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
rozendhyan 6:cec678bb15ef 18 char* username = "1f764db0-122b-11e7-b235-9359079e690e";
rozendhyan 6:cec678bb15ef 19 char* password = "0d4e9d94e7897df9af861007853ed3f906b0627d";
rozendhyan 7:fed254c30643 20 char* clientID = "536277d0-8cb9-11e7-9727-55550d1a07e7";
jburhenn 0:4d7a64104b46 21
rozendhyan 8:f1e261b1e25b 22 LED led(D7);
rozendhyan 8:f1e261b1e25b 23 Servo servo(D6);
jburhenn 0:4d7a64104b46 24 SpwfSAInterface interface(D8, D2); // TX, RX
jburhenn 0:4d7a64104b46 25 MQTTNetwork<SpwfSAInterface> network(interface);
jburhenn 0:4d7a64104b46 26 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
jburhenn 0:4d7a64104b46 27
jburhenn 0:4d7a64104b46 28 DigitalOut led1(LED1);
jburhenn 0:4d7a64104b46 29
jburhenn 0:4d7a64104b46 30 /**
jburhenn 0:4d7a64104b46 31 * Print the message info.
jburhenn 0:4d7a64104b46 32 * @param[in] message The message received from the Cayenne server.
jburhenn 0:4d7a64104b46 33 */
jburhenn 0:4d7a64104b46 34 void outputMessage(CayenneMQTT::MessageData& message)
jburhenn 0:4d7a64104b46 35 {
rozendhyan 7:fed254c30643 36 int channel;
rozendhyan 7:fed254c30643 37 int value ;
rozendhyan 7:fed254c30643 38
jburhenn 0:4d7a64104b46 39 switch (message.topic) {
jburhenn 0:4d7a64104b46 40 case COMMAND_TOPIC:
jburhenn 0:4d7a64104b46 41 printf("topic=Command");
jburhenn 0:4d7a64104b46 42 break;
jburhenn 0:4d7a64104b46 43 case CONFIG_TOPIC:
jburhenn 0:4d7a64104b46 44 printf("topic=Config");
jburhenn 0:4d7a64104b46 45 break;
jburhenn 0:4d7a64104b46 46 default:
jburhenn 0:4d7a64104b46 47 printf("topic=%d", message.topic);
jburhenn 0:4d7a64104b46 48 break;
jburhenn 0:4d7a64104b46 49 }
jburhenn 0:4d7a64104b46 50 printf(" channel=%d", message.channel);
rozendhyan 7:fed254c30643 51 channel = message.channel;
jburhenn 0:4d7a64104b46 52 if (message.clientID) {
jburhenn 0:4d7a64104b46 53 printf(" clientID=%s", message.clientID);
jburhenn 0:4d7a64104b46 54 }
jburhenn 0:4d7a64104b46 55 if (message.type) {
jburhenn 0:4d7a64104b46 56 printf(" type=%s", message.type);
jburhenn 0:4d7a64104b46 57 }
rozendhyan 8:f1e261b1e25b 58 printf("message.valueCount = %d\n",message.valueCount);
jburhenn 0:4d7a64104b46 59 for (size_t i = 0; i < message.valueCount; ++i) {
jburhenn 0:4d7a64104b46 60 if (message.getValue(i)) {
jburhenn 0:4d7a64104b46 61 printf(" value=%s", message.getValue(i));
rozendhyan 7:fed254c30643 62 /*get trigger message*/
rozendhyan 7:fed254c30643 63 value = (int)*message.getValue(i);
rozendhyan 7:fed254c30643 64 //strcpy(value,*message.getValue(i));
jburhenn 0:4d7a64104b46 65 }
jburhenn 0:4d7a64104b46 66 if (message.getUnit(i)) {
jburhenn 0:4d7a64104b46 67 printf(" unit=%s", message.getUnit(i));
jburhenn 0:4d7a64104b46 68 }
jburhenn 0:4d7a64104b46 69 }
jburhenn 0:4d7a64104b46 70 if (message.id) {
jburhenn 0:4d7a64104b46 71 printf(" id=%s", message.id);
jburhenn 0:4d7a64104b46 72 }
jburhenn 0:4d7a64104b46 73 printf("\n");
rozendhyan 8:f1e261b1e25b 74 printf("get channel ==%d\nget value ==%c\n",channel,value);
rozendhyan 7:fed254c30643 75 if(channel ==1 && value ==49){
rozendhyan 7:fed254c30643 76 printf("LED has been Triger\n");
rozendhyan 8:f1e261b1e25b 77 led.On();
rozendhyan 8:f1e261b1e25b 78 }else if(channel ==2 && (value>=48 && value <=58)){
rozendhyan 8:f1e261b1e25b 79 servo = ((float)(value-48)/10);
rozendhyan 7:fed254c30643 80 }else{
rozendhyan 8:f1e261b1e25b 81 led.Off();
rozendhyan 7:fed254c30643 82 }
jburhenn 0:4d7a64104b46 83 }
jburhenn 0:4d7a64104b46 84
jburhenn 0:4d7a64104b46 85 /**
jburhenn 0:4d7a64104b46 86 * Handle messages received from the Cayenne server.
jburhenn 0:4d7a64104b46 87 * @param[in] message The message received from the Cayenne server.
jburhenn 0:4d7a64104b46 88 */
jburhenn 0:4d7a64104b46 89 void messageArrived(CayenneMQTT::MessageData& message)
jburhenn 0:4d7a64104b46 90 {
jburhenn 0:4d7a64104b46 91 int error = 0;
jburhenn 0:4d7a64104b46 92 // Add code to process the message. Here we just ouput the message data.
jburhenn 0:4d7a64104b46 93 outputMessage(message);
jburhenn 0:4d7a64104b46 94
jburhenn 0:4d7a64104b46 95 if (message.topic == COMMAND_TOPIC) {
jburhenn 0:4d7a64104b46 96 switch(message.channel) {
jburhenn 0:4d7a64104b46 97 case 0:
jburhenn 0:4d7a64104b46 98 // Set the onboard LED state
jburhenn 0:4d7a64104b46 99 led1 = atoi(message.getValue());
jburhenn 0:4d7a64104b46 100 // Publish the updated LED state
jburhenn 0:4d7a64104b46 101 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 102 printf("Publish LED state failure, error: %d\n", error);
jburhenn 0:4d7a64104b46 103 }
jburhenn 0:4d7a64104b46 104 break;
jburhenn 0:4d7a64104b46 105 }
jburhenn 0:4d7a64104b46 106
jburhenn 0:4d7a64104b46 107 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
jburhenn 0:4d7a64104b46 108 // An error response should be sent if there are issues processing the message.
jburhenn 2:d7c27e622707 109 if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 110 printf("Response failure, error: %d\n", error);
jburhenn 0:4d7a64104b46 111 }
jburhenn 0:4d7a64104b46 112 }
jburhenn 0:4d7a64104b46 113 }
jburhenn 0:4d7a64104b46 114
jburhenn 0:4d7a64104b46 115 /**
jburhenn 0:4d7a64104b46 116 * Connect to the Cayenne server.
jburhenn 0:4d7a64104b46 117 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
jburhenn 0:4d7a64104b46 118 */
jburhenn 0:4d7a64104b46 119 int connectClient(void)
jburhenn 0:4d7a64104b46 120 {
jburhenn 0:4d7a64104b46 121 int error = 0;
jburhenn 0:4d7a64104b46 122 // Connect to the server.
jburhenn 0:4d7a64104b46 123 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
jburhenn 0:4d7a64104b46 124 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
jburhenn 0:4d7a64104b46 125 printf("TCP connect failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 126 wait(2);
jburhenn 0:4d7a64104b46 127 }
jburhenn 0:4d7a64104b46 128
jburhenn 0:4d7a64104b46 129 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
jburhenn 0:4d7a64104b46 130 printf("MQTT connect failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 131 return error;
jburhenn 0:4d7a64104b46 132 }
jburhenn 0:4d7a64104b46 133 printf("Connected\n");
jburhenn 0:4d7a64104b46 134
jburhenn 0:4d7a64104b46 135 // Subscribe to required topics.
jburhenn 0:4d7a64104b46 136 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 137 printf("Subscription to Command topic failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 138 }
jburhenn 0:4d7a64104b46 139 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 140 printf("Subscription to Config topic failed, error:%d\n", error);
jburhenn 0:4d7a64104b46 141 }
jburhenn 0:4d7a64104b46 142
jburhenn 0:4d7a64104b46 143 // 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.
jburhenn 0:4d7a64104b46 144 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
jburhenn 0:4d7a64104b46 145 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
jburhenn 0:4d7a64104b46 146 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
jburhenn 0:4d7a64104b46 147 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
jburhenn 0:4d7a64104b46 148
jburhenn 0:4d7a64104b46 149 return CAYENNE_SUCCESS;
jburhenn 0:4d7a64104b46 150 }
jburhenn 0:4d7a64104b46 151
jburhenn 0:4d7a64104b46 152 /**
jburhenn 0:4d7a64104b46 153 * Main loop where MQTT code is run.
jburhenn 0:4d7a64104b46 154 */
jburhenn 0:4d7a64104b46 155 void loop(void)
jburhenn 0:4d7a64104b46 156 {
jburhenn 5:294a8b1bca28 157 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
jburhenn 0:4d7a64104b46 158 MQTTTimer timer(5000);
rozendhyan 6:cec678bb15ef 159 //TMP36 tmpSensor(A5);
rozendhyan 6:cec678bb15ef 160 BME280 bmpSensor;
rozendhyan 8:f1e261b1e25b 161
rozendhyan 6:cec678bb15ef 162
jburhenn 0:4d7a64104b46 163
jburhenn 0:4d7a64104b46 164 while (true) {
jburhenn 0:4d7a64104b46 165 // Yield to allow MQTT message processing.
jburhenn 0:4d7a64104b46 166 mqttClient.yield(1000);
rozendhyan 6:cec678bb15ef 167 //printf("%d\n",bmpSensor->Temp_read());
jburhenn 0:4d7a64104b46 168 // Check that we are still connected, if not, reconnect.
jburhenn 0:4d7a64104b46 169 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:4d7a64104b46 170 network.disconnect();
jburhenn 0:4d7a64104b46 171 mqttClient.disconnect();
jburhenn 0:4d7a64104b46 172 printf("Reconnecting\n");
jburhenn 0:4d7a64104b46 173 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 174 wait(2);
jburhenn 0:4d7a64104b46 175 printf("Reconnect failed, retrying\n");
jburhenn 0:4d7a64104b46 176 }
jburhenn 0:4d7a64104b46 177 }
rozendhyan 6:cec678bb15ef 178 printf("before you post data,your temp is %d\n",bmpSensor.Temp_read());
jburhenn 0:4d7a64104b46 179 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
jburhenn 0:4d7a64104b46 180 if (timer.expired()) {
jburhenn 0:4d7a64104b46 181 int error = 0;
rozendhyan 6:cec678bb15ef 182 if ((error = mqttClient.publishData(DATA_TOPIC, 5, TYPE_TEMPERATURE, UNIT_CELSIUS,(float)bmpSensor.Temp_read())) != CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 183 printf("Publish temperature failed, error: %d\n", error);
jburhenn 0:4d7a64104b46 184 }
jburhenn 5:294a8b1bca28 185 // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
jburhenn 0:4d7a64104b46 186 timer.countdown_ms(5000);
jburhenn 0:4d7a64104b46 187 }
jburhenn 0:4d7a64104b46 188 }
jburhenn 0:4d7a64104b46 189 }
jburhenn 0:4d7a64104b46 190
jburhenn 0:4d7a64104b46 191 /**
jburhenn 0:4d7a64104b46 192 * Main function.
jburhenn 0:4d7a64104b46 193 */
jburhenn 0:4d7a64104b46 194 int main()
jburhenn 0:4d7a64104b46 195 {
jburhenn 0:4d7a64104b46 196 // Initialize the network interface.
jburhenn 0:4d7a64104b46 197 printf("Initializing interface\n");
jburhenn 0:4d7a64104b46 198 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
rozendhyan 6:cec678bb15ef 199 printf("connect AP successful\n");
jburhenn 0:4d7a64104b46 200 // Set the default function that receives Cayenne messages.
jburhenn 0:4d7a64104b46 201 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:4d7a64104b46 202
jburhenn 0:4d7a64104b46 203 // Connect to Cayenne.
jburhenn 0:4d7a64104b46 204 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:4d7a64104b46 205 // Run main loop.
jburhenn 0:4d7a64104b46 206 loop();
jburhenn 0:4d7a64104b46 207 }
jburhenn 0:4d7a64104b46 208 else {
jburhenn 0:4d7a64104b46 209 printf("Connection failed, exiting\n");
jburhenn 0:4d7a64104b46 210 }
jburhenn 0:4d7a64104b46 211
jburhenn 0:4d7a64104b46 212 if (mqttClient.connected())
rozendhyan 6:cec678bb15ef 213 printf("client disconnect\n");
jburhenn 0:4d7a64104b46 214 mqttClient.disconnect();
jburhenn 0:4d7a64104b46 215 if (network.connected())
rozendhyan 6:cec678bb15ef 216 printf("network disconnect\n");
jburhenn 0:4d7a64104b46 217 network.disconnect();
rozendhyan 8:f1e261b1e25b 218
rozendhyan 8:f1e261b1e25b 219
rozendhyan 8:f1e261b1e25b 220
rozendhyan 6:cec678bb15ef 221
jburhenn 0:4d7a64104b46 222
jburhenn 0:4d7a64104b46 223 }
jburhenn 0:4d7a64104b46 224