CHANG rozen / Mbed 2 deprecated Cayenne-X-NUCLEO-IDW01M1-TMP36

Dependencies:   Cayenne-MQTT-mbed NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed Servo

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 * Example app for using the Cayenne MQTT mbed library to send data from a TMP36 sensor. This example uses
00003 * the X-NUCLEO-IDW01M1 WiFi expansion board via the X_NUCLEO_IDW01M1v2 library.
00004 */
00005 
00006 #include "MQTTTimer.h"
00007 #include "CayenneMQTTClient.h"
00008 #include "MQTTNetworkIDW01M1.h"
00009 #include "SpwfInterface.h"
00010 #include "BME280.hpp"
00011 #include "LED.hpp"
00012 #include "Servo.h"
00013 // WiFi network info.
00014 char* ssid = "megu megu fire";
00015 char* wifiPassword = "66666667";
00016 
00017 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
00018 char* username = "1f764db0-122b-11e7-b235-9359079e690e";
00019 char* password = "0d4e9d94e7897df9af861007853ed3f906b0627d";
00020 char* clientID = "536277d0-8cb9-11e7-9727-55550d1a07e7";
00021 
00022 LED led(D7);
00023 Servo servo(D6);
00024 SpwfSAInterface interface(D8, D2); // TX, RX
00025 MQTTNetwork<SpwfSAInterface> network(interface);
00026 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
00027 
00028 DigitalOut led1(LED1);
00029 
00030 /**
00031 * Print the message info.
00032 * @param[in] message The message received from the Cayenne server.
00033 */
00034 void outputMessage(CayenneMQTT::MessageData& message)
00035 {
00036     int channel;
00037     int value ;
00038     
00039     switch (message.topic)  {
00040     case COMMAND_TOPIC:
00041         printf("topic=Command");
00042         break;
00043     case CONFIG_TOPIC:
00044         printf("topic=Config");
00045         break;
00046     default:
00047         printf("topic=%d", message.topic);
00048         break;
00049     }
00050     printf(" channel=%d", message.channel);
00051     channel = message.channel;
00052     if (message.clientID) {
00053         printf(" clientID=%s", message.clientID);
00054     }
00055     if (message.type) {
00056         printf(" type=%s", message.type);
00057     }
00058     printf("message.valueCount = %d\n",message.valueCount);
00059     for (size_t i = 0; i < message.valueCount; ++i) {
00060         if (message.getValue(i)) {
00061             printf(" value=%s", message.getValue(i));
00062             /*get trigger message*/
00063             value = (int)*message.getValue(i);
00064             //strcpy(value,*message.getValue(i));
00065         }
00066         if (message.getUnit(i)) {
00067             printf(" unit=%s", message.getUnit(i));
00068         }
00069     }
00070     if (message.id) {
00071         printf(" id=%s", message.id);
00072     }
00073     printf("\n");
00074     printf("get channel ==%d\nget value ==%c\n",channel,value);
00075     if(channel ==1 && value ==49){
00076         printf("LED has been Triger\n");
00077         led.On();
00078     }else if(channel ==2 && (value>=48 && value <=58)){
00079         servo = ((float)(value-48)/10);
00080     }else{
00081         led.Off();
00082     }
00083 }
00084 
00085 /**
00086 * Handle messages received from the Cayenne server.
00087 * @param[in] message The message received from the Cayenne server.
00088 */
00089 void messageArrived(CayenneMQTT::MessageData& message)
00090 {
00091     int error = 0;
00092     // Add code to process the message. Here we just ouput the message data.
00093     outputMessage(message);
00094 
00095     if (message.topic == COMMAND_TOPIC) {
00096         switch(message.channel) {
00097         case 0:
00098             // Set the onboard LED state
00099             led1 = atoi(message.getValue());
00100             // Publish the updated LED state
00101             if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
00102                 printf("Publish LED state failure, error: %d\n", error);
00103             }
00104             break;
00105         }
00106         
00107         // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
00108         // An error response should be sent if there are issues processing the message.
00109         if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
00110             printf("Response failure, error: %d\n", error);
00111         }
00112     }
00113 }
00114 
00115 /**
00116 * Connect to the Cayenne server.
00117 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
00118 */
00119 int connectClient(void)
00120 {
00121     int error = 0;
00122     // Connect to the server.
00123     printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
00124     while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
00125         printf("TCP connect failed, error: %d\n", error);
00126         wait(2);
00127     }
00128 
00129     if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
00130         printf("MQTT connect failed, error: %d\n", error);
00131         return error;
00132     }
00133     printf("Connected\n");
00134 
00135     // Subscribe to required topics.
00136     if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
00137         printf("Subscription to Command topic failed, error: %d\n", error);
00138     }
00139     if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
00140         printf("Subscription to Config topic failed, error:%d\n", error);
00141     }
00142 
00143     // 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.
00144     mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
00145     mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
00146     //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
00147     //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
00148 
00149     return CAYENNE_SUCCESS;
00150 }
00151 
00152 /**
00153 * Main loop where MQTT code is run.
00154 */
00155 void loop(void)
00156 {
00157     // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
00158     MQTTTimer timer(5000);
00159     //TMP36 tmpSensor(A5);
00160     BME280 bmpSensor;
00161     
00162     
00163     
00164     while (true) {
00165         // Yield to allow MQTT message processing.
00166         mqttClient.yield(1000);
00167         //printf("%d\n",bmpSensor->Temp_read());
00168         // Check that we are still connected, if not, reconnect.
00169         if (!network.connected() || !mqttClient.connected()) {
00170             network.disconnect();
00171             mqttClient.disconnect();
00172             printf("Reconnecting\n");
00173             while (connectClient() != CAYENNE_SUCCESS) {
00174                 wait(2);
00175                 printf("Reconnect failed, retrying\n");
00176             }
00177         }
00178         printf("before you post data,your temp is %d\n",bmpSensor.Temp_read());
00179         // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
00180         if (timer.expired()) {
00181             int error = 0;
00182             if ((error = mqttClient.publishData(DATA_TOPIC, 5, TYPE_TEMPERATURE, UNIT_CELSIUS,(float)bmpSensor.Temp_read())) != CAYENNE_SUCCESS) {
00183                 printf("Publish temperature failed, error: %d\n", error);
00184             }
00185             // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
00186             timer.countdown_ms(5000);
00187         }
00188     }
00189 }
00190 
00191 /**
00192 * Main function.
00193 */
00194 int main()
00195 {   
00196     // Initialize the network interface.
00197     printf("Initializing interface\n");
00198     interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
00199     printf("connect AP successful\n");
00200     // Set the default function that receives Cayenne messages.
00201     mqttClient.setDefaultMessageHandler(messageArrived);
00202 
00203     // Connect to Cayenne.
00204     if (connectClient() == CAYENNE_SUCCESS) {
00205         // Run main loop.
00206         loop();
00207     }
00208     else {
00209         printf("Connection failed, exiting\n");
00210     }
00211 
00212     if (mqttClient.connected())
00213         printf("client disconnect\n");
00214         mqttClient.disconnect();
00215     if (network.connected())
00216         printf("network disconnect\n");
00217         network.disconnect();
00218         
00219         
00220     
00221     
00222 
00223 }
00224