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

Committer:
rozendhyan
Date:
Tue Aug 29 17:05:03 2017 +0000
Revision:
8:f1e261b1e25b
Parent:
7:fed254c30643
Child:
9:668337c0fb8a
add Tigger servo

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