ECE59500_ESP8266_K64_MQTT_Pub_Sub_client
Dependencies: mbed ESP8266Interface MbedJSONValue mbed-rtos MQTT JSON
Revision 21:f08f17225c7c, committed 2019-03-08
- Comitter:
- sbetrabet
- Date:
- Fri Mar 08 07:05:46 2019 +0000
- Parent:
- 20:568d04eb3d63
- Commit message:
- Maual and auto override added;
Changed in this revision
--- 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); + } + } + } /*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgbled.cpp Fri Mar 08 07:05:46 2019 +0000 @@ -0,0 +1,84 @@ +#include "mbed.h" +#include "rgbled.h" + +rgbled::rgbled(PinName pin_red, PinName pin_green, PinName pin_blue) + : _pin_red(pin_red), _pin_green(pin_green), _pin_blue(pin_blue){ + _on = false; + _off = !_on; + _pin_red = _off; + _pin_green = _off; + _pin_blue = _off; +} + +void rgbled::active(bool a){ + _on = a; + _off = !_on; +} + +void rgbled::_none(void){ + _pin_red = _off; + _pin_green = _off; + _pin_blue = _off; +} + +void rgbled::_red(void){ + _pin_red = _on; + _pin_green = _off; + _pin_blue = _off; +} + +void rgbled::_green(void){ + _pin_red = _off; + _pin_green = _on; + _pin_blue = _off; +} + +void rgbled::_blue(void){ + _pin_red = _off; + _pin_green = _off; + _pin_blue = _on; +} + +void rgbled::_cyan(void){ + _pin_red = _off; + _pin_green = _on; + _pin_blue = _on; +} + +void rgbled::_yellow(void){ + _pin_red = _on; + _pin_green = _on; + _pin_blue = _off; +} + +void rgbled::_magenta(void){ + _pin_red = _on; + _pin_green = _off; + _pin_blue = _on; +} + +void rgbled::set(color c){ + switch (c) { + case none : + _none(); + break; + case red : + _red(); + break; + case yellow : + _yellow(); + break; + case green : + _green(); + break; + case cyan: + _cyan(); + break; + case blue : + _blue(); + break; + case magenta : + _magenta(); + break; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rgbled.h Fri Mar 08 07:05:46 2019 +0000 @@ -0,0 +1,31 @@ +#ifndef __RGBLED_H__ +#define __RGBLED_H__ + +// includes +#include "mbed.h" + +// defines +enum color {none, red, yellow, green, cyan, blue, magenta}; + +// rgb led +class rgbled { +private: + DigitalOut _pin_red; + DigitalOut _pin_green; + DigitalOut _pin_blue; + bool _on; + bool _off; + void _none(void); + void _red(void); + void _yellow(void); + void _green(void); + void _cyan(void); + void _blue(void); + void _magenta(void); +public: + rgbled(PinName pin_red, PinName pin_green, PinName pin_blue); + void active(bool a); + void set(color c); +}; + +#endif \ No newline at end of file