On Node-Red and MQTT
Dependencies: mbed ESP8266Interface MbedJSONValue MQTT JSON
Diff: main.cpp
- Revision:
- 21:f08f17225c7c
- Parent:
- 20:568d04eb3d63
- Child:
- 22:518ae39b6d77
--- a/main.cpp Fri Mar 08 04:51:18 2019 +0000 +++ b/main.cpp Fri Mar 08 07:05:46 2019 +0000 @@ -1,6 +1,7 @@ #include "MQTTESP8266.h" #include "MQTTClient.h" #include "string.h" +#include "rgbled.h" #include "MbedJSONValue.h" char buf[100]; @@ -8,18 +9,18 @@ float version = 0.47; char* topic = "k64"; char* topic2 = "k64rec"; -char* topic3 = "bulb"; - +char* topic3 = "bulb"; char* key2 = "key"; char* hostname = "192.168.43.87"; // Ip int port = 1883; int rc = ipstack.connect(hostname, port); -unsigned long previousTime = 0; // will store last time LED was updated +unsigned int previousTime = 0; // will store last time LED was updated // constants won't change: const long interval = 20; +int flag =0; MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack); -MbedJSONValue demo; +//MbedJSONValue demo; int arrivedcount = 0; //using namespace std; std::string my_str; @@ -28,10 +29,13 @@ std::string clearsky ("clear"); std::string clouds ("few clouds"); std::string snow ("light snow"); +std::string automatic ("automatic"); +std::string manual ("manual"); int j=0; +rgbled rgb(PTB22, PTE26, PTB21); // create rgb object with pin definitions for FRDM-K64F board + // callback for subscribe topic - void subscribeCallback(MQTT::MessageData& md) { MQTT::Message &message = md.message; @@ -41,15 +45,15 @@ printf("\r\n"); printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); - printf("nonedited payload is %s and payload is %d\n",(char*)message.payload,message.payloadlen); - printf("hey\n"); + printf("nonedited payload is %s and payload is %d\r\n",(char*)message.payload,message.payloadlen); + // int f = int(message.payload); std::string someString((char*)message.payload); someString.resize (message.payloadlen); - printf("Edited payload iss %s \n",someString.c_str()); - j= someString.find(clouds); - printf("j = %d \r\n", j); + printf("Edited payload iss %s \r\n",someString.c_str()); + //j= someString.find(clouds); + //printf("j = %d \r\n", j); if ( !(someString.find(clouds)) ) { @@ -58,6 +62,8 @@ message.payloadlen = strlen(buf)+1; rc = client.publish(topic3, message); printf("if condition checked, its cloudy\r\n"); + //rgb.set(red); + } else if ( !(someString.find(clearsky)) ) { @@ -66,14 +72,33 @@ message.payloadlen = strlen(buf)+1; rc = client.publish(topic3, message); printf("if condition checked, its clear\r\n"); + + //rgb.set(yellow); } else if ( !(someString.find(snow)) ) { + sprintf(buf, "dCglDR-G9WoQUiig0XMNhOV4ozIEIVu3rWdBh5IlQ7d,snow %s\r\n",key2); message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; rc = client.publish(topic3, message); printf("if condition checked, its snowy\r\n"); + + // rgb.set(blue); + } + else if ( !(someString.find(automatic)) ) + { + + printf("Automatic, i.e Decide based on weather \r\n"); + flag = 0; + // rgb.set(blue); + } + else if ( !(someString.find(manual)) ) + { + + printf("Manual Flag set \r\n"); + flag = 1; + // rgb.set(blue); } else { @@ -87,8 +112,16 @@ wait (1); printf("Starting\r\n"); + set_time(1256729737); + time_t seconds = time(NULL); + previousTime = (unsigned int)seconds; + + printf("Version is %f\r\n", version); - + + rgb.active(false); // set led output to active low + rgb.set(none); // set led output of all off + if (rc != 0) @@ -108,7 +141,7 @@ MQTT::Message message; // QoS 0 - time_t seconds = time(NULL); + sprintf(buf, "fa02d31610ce9ea0de15b22bb2fe279d, %s\r\n", key2); message.qos = MQTT::QOS0; @@ -120,6 +153,7 @@ rc = client.publish(topic, message); wait(5); + /* sprintf(buf, "dCglDR-G9WoQUiig0XMNhOV4ozIEIVu3rWdBh5IlQ7d, %s\r\n", key2); message.payload = (void*)buf; @@ -128,7 +162,28 @@ */ while (arrivedcount < 10) - client.yield(100); + { client.yield(100); + + time_t seconds = time(NULL); + //printf("%u\r\n", (unsigned int)seconds); + wait(1); + if ( seconds - previousTime >= interval ) + { + if (flag ==0) + { + //printf("its been a few seconds \r\n"); + printf("Checking the weather\r\n"); + previousTime =seconds; + sprintf(buf, "fa02d31610ce9ea0de15b22bb2fe279d, %s\r\n", key2); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + rc = client.publish(topic, message); + } + } + } /*