Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LDR_sensor Cayenne-MQTT-mbed mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI moisture_sensor Pir_sensor TMP36 lib_dht22 millis
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 #include "millis.h" 00006 #include "MQTTTimer.h" 00007 #include "CayenneMQTTClient.h" 00008 #include "MQTTNetworkIDW01M1.h" 00009 #include "SpwfInterface.h" 00010 00011 #include "math.h" 00012 00013 00014 00015 //#include "TMP36.h" 00016 //#include "DHT.h" 00017 #include "dht22.h" 00018 #include "Pir_sensor.h" 00019 #include "moisture_sensor.h" 00020 #include "LDR_sensor.h" 00021 00022 DHT22 dht22(D7); 00023 00024 /* 00025 DHT sensor(D7,DHT22); 00026 void task_DHT(); 00027 */ 00028 Serial pc(USBTX, USBRX); 00029 // WiFi network info. 00030 char* ssid = "DIN1"; 00031 char* wifiPassword = "sharif82"; 00032 00033 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. 00034 char* username = "59176890-3ee5-11e9-ad96-c15442ccb423"; 00035 char* password = "2ba1e1d7467a27f966d41c53fef22f4ae3b986d3"; 00036 char* clientID = "9c2293c0-4cc9-11e9-ba40-5d168a516101"; 00037 00038 SpwfSAInterface interface(D8, D2); // TX, RX 00039 MQTTNetwork<SpwfSAInterface> network(interface); 00040 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID); 00041 00042 DigitalOut led2(PB_6); 00043 DigitalOut led1(PA_6); 00044 //DigitalIn Sensor1(PA_7); 00045 //InterruptIn button(PA_7); 00046 /** 00047 * Print the message info. 00048 * @param[in] message The message received from the Cayenne server. 00049 */ 00050 00051 double status=0; 00052 void pressed() 00053 { 00054 status = 1 ;// 00055 } 00056 00057 void released() 00058 { 00059 status = 0; // 00060 } 00061 00062 void outputMessage(CayenneMQTT::MessageData& message) 00063 { 00064 switch (message.topic) { 00065 case COMMAND_TOPIC: 00066 printf("topic=Command"); 00067 break; 00068 case CONFIG_TOPIC: 00069 printf("topic=Config"); 00070 break; 00071 default: 00072 printf("topic=%d", message.topic); 00073 break; 00074 } 00075 printf(" channel=%d", message.channel); 00076 if (message.clientID) { 00077 printf(" clientID=%s", message.clientID); 00078 } 00079 if (message.type) { 00080 printf(" type=%s", message.type); 00081 } 00082 for (size_t i = 0; i < message.valueCount; ++i) { 00083 if (message.getValue(i)) { 00084 printf(" value=%s", message.getValue(i)); 00085 } 00086 if (message.getUnit(i)) { 00087 printf(" unit=%s", message.getUnit(i)); 00088 } 00089 } 00090 if (message.id) { 00091 printf(" id=%s", message.id); 00092 } 00093 printf("\n"); 00094 } 00095 00096 //int previousState = -1; 00097 00098 /* 00099 void checkSensor() 00100 { 00101 int status = 0; 00102 button.fall(&pressed); 00103 button.rise(&released); 00104 00105 printf("Publish sensor detection, status: %d\n",status); 00106 00107 } 00108 00109 */ 00110 int previousState = -1; 00111 int currentState = -1; 00112 unsigned long previousMillis = 0; 00113 00114 /* 00115 void checkSensor() 00116 { 00117 00118 unsigned long currentMillis = millis(); 00119 // Check sensor data every 250 milliseconds 00120 while (currentMillis - previousMillis >= 1000) 00121 { 00122 00123 pc.printf("millissensor = %d\r\n", millis()); 00124 // Check the sensor state and send data when it changes. 00125 // currentState = Sensor1; 00126 wait (0.5); 00127 int error = 0; 00128 if (Sensor1==1) 00129 { 00130 if ((error = mqttClient.publishData(DATA_TOPIC, 6, TYPE_TEMPERATURE, UNIT_CELSIUS, 1)) != CAYENNE_SUCCESS) 00131 { 00132 printf("Publish temperature failed, error: %d\n", error); 00133 } 00134 led2 = 1; 00135 wait (0.1); 00136 00137 } 00138 if ((error = mqttClient.publishData(DATA_TOPIC, 6, TYPE_TEMPERATURE, UNIT_CELSIUS , 0)) != CAYENNE_SUCCESS) 00139 { 00140 printf("Publish temperature failed, error: %d\n", error); 00141 } 00142 led2 = 0; 00143 previousMillis=currentMillis; 00144 00145 00146 00147 } 00148 } 00149 */ 00150 00151 /** 00152 * Handle messages received from the Cayenne server. 00153 * @param[in] message The message received from the Cayenne server. 00154 */ 00155 /* 00156 void task_DHT() 00157 { 00158 int error = 0; 00159 int h, c,f; 00160 float dp = 0.0f; 00161 00162 wait(2.0f); 00163 error = sensor.readData(); 00164 if (0 == error) 00165 { 00166 c = sensor.ReadTemperature(CELCIUS); 00167 f = sensor.ReadTemperature(FARENHEIT); 00168 h = sensor.ReadHumidity(); 00169 dp = sensor.CalcdewPoint(c, h); 00170 00171 printf("Temperature in Celcius: %d, Farenheit %d\r\n", c, f); 00172 printf("Humidity is %d, Dewpoint: %4.2f\r\n\n", h, dp); 00173 } else { 00174 printf("Error: %d\r\n", error); 00175 } 00176 } 00177 00178 */ 00179 00180 void Read_DHT() 00181 00182 { 00183 DHT22_data_t dht22_data; 00184 dht22.read(&dht22_data); 00185 00186 float temperature = dht22_data.temp / 10.0f; 00187 float humidity = dht22_data.humidity / 10.0f; 00188 00189 pc.printf("Temperature: %2.2f Humidity: %2.2f%%\r\n", temperature, humidity); 00190 wait_ms(200); 00191 return; 00192 } 00193 00194 00195 00196 void messageArrived(CayenneMQTT::MessageData& message) 00197 { 00198 int error = 0; 00199 // Add code to process the message. Here we just ouput the message data. 00200 outputMessage(message); 00201 00202 if (message.topic == COMMAND_TOPIC) 00203 { 00204 switch(message.channel) 00205 { 00206 case 1: 00207 // Set the onboard LED state 00208 led1 = atoi(message.getValue()); 00209 // Publish the updated LED state 00210 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) 00211 { 00212 printf("Publish LED state failure, error: %d\n", error); 00213 } 00214 break; 00215 00216 case 2: 00217 // Set the onboard LED state 00218 led2 = atoi(message.getValue()); 00219 // Publish the updated LED state 00220 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) 00221 { 00222 printf("Publish LED state failure, error: %d\n", error); 00223 } 00224 break; 00225 } 00226 00227 // If this is a command message we publish a response. Here we are just sending a default 'OK' response. 00228 // An error response should be sent if there are issues processing the message. 00229 if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) { 00230 printf("Response failure, error: %d\n", error); 00231 } 00232 } 00233 } 00234 00235 /** 00236 * Connect to the Cayenne server. 00237 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise. 00238 */ 00239 int connectClient(void) 00240 { 00241 int error = 0; 00242 // Connect to the server. 00243 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT); 00244 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) { 00245 printf("TCP connect failed, error: %d\n", error); 00246 wait(10); 00247 } 00248 00249 if ((error = mqttClient.connect()) != MQTT::SUCCESS) { 00250 printf("MQTT connect failed, error: %d\n", error); 00251 return error; 00252 } 00253 printf("Connected\n"); 00254 00255 // Subscribe to required topics. 00256 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { 00257 printf("Subscription to Command topic failed, error: %d\n", error); 00258 } 00259 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) { 00260 printf("Subscription to Config topic failed, error:%d\n", error); 00261 } 00262 00263 // 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. 00264 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION); 00265 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice"); 00266 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model"); 00267 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000"); 00268 00269 return CAYENNE_SUCCESS; 00270 } 00271 00272 /** 00273 * Main loop where MQTT code is run. 00274 */ 00275 void loop(void) 00276 { 00277 00278 00279 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. 00280 MQTTTimer timer(1000); 00281 double pow(unsigned int, unsigned int); 00282 00283 00284 Pir_sensor tmpSensor1(PA_7); 00285 moisture_sensor tmpSensor2(PA_0); //use A0 analog pin to received output from soil sensor 00286 LDR_sensor tmpSensor3(PA_1); //use A1 analog pin to received output from soil sensor 00287 // pc.printf("Reading Pir Sensor!! %d\r\n"); 00288 if (tmpSensor1.read() ==1) 00289 00290 { 00291 wait (0.1); 00292 } 00293 led2 =0; 00294 00295 wait (0.1); 00296 00297 00298 00299 while (true) 00300 { 00301 // Yield to allow MQTT message processing. 00302 mqttClient.yield(10000); 00303 00304 // Check that we are still connected, if not, reconnect. 00305 if (!network.connected() || !mqttClient.connected()) 00306 { 00307 // network.disconnect(); 00308 mqttClient.disconnect(); 00309 printf("Reconnecting\n"); 00310 while (connectClient() != CAYENNE_SUCCESS) 00311 { 00312 wait(2); 00313 printf("Reconnect failed, retrying\n"); 00314 } 00315 } 00316 00317 00318 pc.printf("Reading ALL Sensor!! %d\r\n"); 00319 00320 00321 // Read_DHT(); 00322 DHT22_data_t dht22_data; 00323 dht22.read(&dht22_data); 00324 00325 float temperature = dht22_data.temp / 10.0f; 00326 float humidity = dht22_data.humidity / 10.0f; 00327 pc.printf("Temperature: %2.2f Humidity: %2.2f%%\r\n", temperature, humidity); 00328 wait_ms(10); 00329 00330 //start to read soil sensor// 00331 float meas_r=tmpSensor2.read(); 00332 float meas_v=meas_r * 3300; 00333 float water_level=100*((3300-meas_v)/3300); 00334 00335 00336 pc.printf("measure = %f = %.0f mV\r\n", meas_r, meas_v); 00337 pc.printf("Water level ratio = %.0f %\r\n", water_level); 00338 //end read soil sensor// 00339 00340 00341 //start to LDR sensor// 00342 float meas_r1; 00343 float meas_v1; 00344 double Rest; 00345 float LDRvolt; 00346 float ldrLux; 00347 meas_r1=tmpSensor3.read(); 00348 00349 meas_v1=meas_r1 * 3300; 00350 00351 LDRvolt=3300-meas_v1; 00352 Rest=(LDRvolt/1000)/(3.3/10000); 00353 //ldrLux = pow(Rest,4.47687); 00354 00355 pc.printf("LDR Voltage: %2.2f\r\n",LDRvolt); 00356 pc.printf("LDR Resistance : %2.2f\r\n",Rest); 00357 //end read LDR sensor// 00358 00359 00360 00361 00362 00363 if (tmpSensor1.read() ==1) 00364 00365 { led2 =1; 00366 wait (0.1); 00367 } 00368 led2 =0; 00369 00370 // wait (0.5); 00371 00372 00373 00374 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. 00375 if (timer.expired()) 00376 { 00377 int error = 0; 00378 /*if ((error = mqttClient.publishData(DATA_TOPIC, 5, TYPE_TEMPERATURE, UNIT_CELSIUS, tmpSensor.read())) != CAYENNE_SUCCESS) 00379 { 00380 printf("Publish temperature failed, error: %d\n", error); 00381 } 00382 */ 00383 00384 if ((error = mqttClient.publishData(DATA_TOPIC, 7, TYPE_VOLTAGE, UNIT_MILLIVOLTS, LDRvolt)) != CAYENNE_SUCCESS) 00385 { 00386 printf("Publish temperature failed, error: %d\n", error); 00387 } 00388 00389 if ((error = mqttClient.publishData(DATA_TOPIC, 6, TYPE_TEMPERATURE, UNIT_DIGITAL, tmpSensor1.read())) != CAYENNE_SUCCESS) 00390 { 00391 printf("Publish temperature failed, error: %d\n", error); 00392 } 00393 00394 00395 if ((error = mqttClient.publishData(DATA_TOPIC, 5, TYPE_TEMPERATURE, UNIT_CELSIUS, temperature)) != CAYENNE_SUCCESS) 00396 { 00397 printf("Publish temperature failed, error: %d\n", error); 00398 } 00399 if ((error = mqttClient.publishData(DATA_TOPIC, 4, TYPE_VOLTAGE, UNIT_PERCENT, humidity)) != CAYENNE_SUCCESS) 00400 { 00401 printf("Publish temperature failed, error: %d\n", error); 00402 } 00403 00404 if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_VOLTAGE, UNIT_PERCENT, water_level)) != CAYENNE_SUCCESS) 00405 { 00406 printf("Publish temperature failed, error: %d\n", error); 00407 } 00408 00409 timer.countdown_ms(5000); 00410 //led2=0; 00411 } 00412 00413 00414 } 00415 } 00416 00417 /** 00418 * Main function. 00419 */ 00420 00421 00422 int main() 00423 { millisStart(); 00424 00425 00426 //led2 = 1; 00427 // Initialize the network interface. 00428 printf("Initializing interface\n"); 00429 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2); 00430 00431 00432 00433 // Set the default function that receives Cayenne messages. 00434 mqttClient.setDefaultMessageHandler(messageArrived); 00435 00436 // Connect to Cayenne. 00437 if (connectClient() == CAYENNE_SUCCESS) 00438 { 00439 00440 //while(1) 00441 //{ 00442 // Run main loop. 00443 00444 // button.fall(&pressed); 00445 // button.rise(&released); 00446 // led2 = 1; 00447 // checkSensor(); 00448 // led2 = 0; 00449 loop(); 00450 // checkSensor(); 00451 // pc.printf("millis = %d\r\n", millis()); 00452 // } 00453 // Assign functions to button 00454 } 00455 00456 else 00457 { 00458 printf("Connection failed, exiting\n"); 00459 } 00460 00461 if (mqttClient.connected()) 00462 mqttClient.disconnect(); 00463 if (network.connected()) 00464 network.disconnect(); 00465 00466 return 0; 00467 } 00468
Generated on Mon Jul 18 2022 20:09:16 by
1.7.2