ECE59500_ESP8266_K64_MQTT_Pub_Sub_client

Dependencies:   mbed ESP8266Interface MbedJSONValue mbed-rtos MQTT JSON

Committer:
sbetrabet
Date:
Thu Mar 07 08:55:54 2019 +0000
Revision:
19:9dfb938f07eb
Parent:
18:76d0899bc3ce
Child:
20:568d04eb3d63
For the ECE59500 project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sbetrabet 19:9dfb938f07eb 1 #include "MQTTESP8266.h"
sbetrabet 19:9dfb938f07eb 2 #include "MQTTClient.h"
sbetrabet 19:9dfb938f07eb 3 #include "string.h"
sbetrabet 19:9dfb938f07eb 4 #include "MbedJSONValue.h"
sbetrabet 19:9dfb938f07eb 5 char buf[100];
geky 17:92a64d43ee61 6
sbetrabet 19:9dfb938f07eb 7 MQTTESP8266 ipstack(D1, D0, D10, "BHNTG1682GFFE2","220960b5"); // change to match your wifi access point
sbetrabet 19:9dfb938f07eb 8 float version = 0.47;
sbetrabet 19:9dfb938f07eb 9 char* topic = "k64";
sbetrabet 19:9dfb938f07eb 10 char* topic2 = "k64rec";
sbetrabet 19:9dfb938f07eb 11 char* topic3 = "bulb";
sbetrabet 19:9dfb938f07eb 12
sbetrabet 19:9dfb938f07eb 13 char* key2 = "key";
sbetrabet 19:9dfb938f07eb 14 char* hostname = "192.168.0.6"; // Ip
sbetrabet 19:9dfb938f07eb 15 int port = 1883;
sbetrabet 19:9dfb938f07eb 16 int rc = ipstack.connect(hostname, port);
geky 17:92a64d43ee61 17
sbetrabet 19:9dfb938f07eb 18 MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack);
sbetrabet 19:9dfb938f07eb 19 MbedJSONValue demo;
sbetrabet 19:9dfb938f07eb 20 int arrivedcount = 0;
sbetrabet 19:9dfb938f07eb 21 //using namespace std;
sbetrabet 19:9dfb938f07eb 22 std::string my_str;
sbetrabet 19:9dfb938f07eb 23 std::string my_str2;
sbetrabet 19:9dfb938f07eb 24 std::string someString;
sbetrabet 19:9dfb938f07eb 25 std::string clearsky ("clear");
sbetrabet 19:9dfb938f07eb 26 std::string clouds ("few clouds");
sbetrabet 19:9dfb938f07eb 27 std::string snow ("light snow");
sbetrabet 19:9dfb938f07eb 28 int j=0;
sbetrabet 19:9dfb938f07eb 29 // callback for subscribe topic
icraggs 1:a1d5c7a6acbc 30
icraggs 2:638c854c0695 31
mbedAustin 18:76d0899bc3ce 32 void subscribeCallback(MQTT::MessageData& md)
icraggs 2:638c854c0695 33 {
icraggs 9:5beb8609e9f7 34 MQTT::Message &message = md.message;
mbedAustin 18:76d0899bc3ce 35 printf("Message received: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
sbetrabet 19:9dfb938f07eb 36
sbetrabet 19:9dfb938f07eb 37 printf("%s",(char*)message.payload);
sbetrabet 19:9dfb938f07eb 38 printf("\r\n");
sbetrabet 19:9dfb938f07eb 39
icraggs 9:5beb8609e9f7 40 printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
sbetrabet 19:9dfb938f07eb 41 printf("nonedited payload is %s and payload is %d\n",(char*)message.payload,message.payloadlen);
sbetrabet 19:9dfb938f07eb 42 printf("hey\n");
sbetrabet 19:9dfb938f07eb 43 // int f = int(message.payload);
sbetrabet 19:9dfb938f07eb 44
sbetrabet 19:9dfb938f07eb 45 std::string someString((char*)message.payload);
sbetrabet 19:9dfb938f07eb 46 someString.resize (message.payloadlen);
sbetrabet 19:9dfb938f07eb 47 printf("Edited payload iss %s \n",someString.c_str());
sbetrabet 19:9dfb938f07eb 48 j= someString.find(clouds);
sbetrabet 19:9dfb938f07eb 49 printf("j = %d \r\n", j);
sbetrabet 19:9dfb938f07eb 50
sbetrabet 19:9dfb938f07eb 51 if ( !(someString.find(clouds)) )
sbetrabet 19:9dfb938f07eb 52 {
sbetrabet 19:9dfb938f07eb 53 sprintf(buf, "dCglDR-G9WoQUiig0XMNhOV4ozIEIVu3rWdBh5IlQ7d,cloud %s\n", key2);
sbetrabet 19:9dfb938f07eb 54 message.payload = (void*)buf;
sbetrabet 19:9dfb938f07eb 55 message.payloadlen = strlen(buf)+1;
sbetrabet 19:9dfb938f07eb 56 rc = client.publish(topic3, message);
sbetrabet 19:9dfb938f07eb 57 printf("if condition checked, its cloudy\n");
sbetrabet 19:9dfb938f07eb 58 }
sbetrabet 19:9dfb938f07eb 59 else if ( !(someString.find(clearsky)) )
sbetrabet 19:9dfb938f07eb 60 {
sbetrabet 19:9dfb938f07eb 61 sprintf(buf, "dCglDR-G9WoQUiig0XMNhOV4ozIEIVu3rWdBh5IlQ7d,clearsky %s\n", key2);
sbetrabet 19:9dfb938f07eb 62 message.payload = (void*)buf;
sbetrabet 19:9dfb938f07eb 63 message.payloadlen = strlen(buf)+1;
sbetrabet 19:9dfb938f07eb 64 rc = client.publish(topic3, message);
sbetrabet 19:9dfb938f07eb 65 printf("if condition checked, its clear\n");
sbetrabet 19:9dfb938f07eb 66 }
sbetrabet 19:9dfb938f07eb 67 else if ( !(someString.find(snow)) )
sbetrabet 19:9dfb938f07eb 68 {
sbetrabet 19:9dfb938f07eb 69 sprintf(buf, "dCglDR-G9WoQUiig0XMNhOV4ozIEIVu3rWdBh5IlQ7d,snow %s\n",key2);
sbetrabet 19:9dfb938f07eb 70 message.payload = (void*)buf;
sbetrabet 19:9dfb938f07eb 71 message.payloadlen = strlen(buf)+1;
sbetrabet 19:9dfb938f07eb 72 rc = client.publish(topic3, message);
sbetrabet 19:9dfb938f07eb 73 printf("if condition checked, its snowy\n");
sbetrabet 19:9dfb938f07eb 74 }
sbetrabet 19:9dfb938f07eb 75 else
sbetrabet 19:9dfb938f07eb 76 {
sbetrabet 19:9dfb938f07eb 77 printf("went into else\n");
sbetrabet 19:9dfb938f07eb 78 }
sbetrabet 19:9dfb938f07eb 79
icraggs 2:638c854c0695 80 }
icraggs 0:0cae29831d01 81
icraggs 2:638c854c0695 82 int main(int argc, char* argv[])
sbetrabet 19:9dfb938f07eb 83 {
sbetrabet 19:9dfb938f07eb 84 wait (1);
geky 17:92a64d43ee61 85 printf("Starting\n");
sbetrabet 19:9dfb938f07eb 86
sam_grove 5:4a257f6ac09a 87 printf("Version is %f\n", version);
geky 17:92a64d43ee61 88
sbetrabet 19:9dfb938f07eb 89
geky 17:92a64d43ee61 90
icraggs 6:e4c690c45021 91 if (rc != 0)
geky 17:92a64d43ee61 92 printf("rc from TCP connect is %d\n", rc);
geky 17:92a64d43ee61 93
geky 17:92a64d43ee61 94 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
sbetrabet 19:9dfb938f07eb 95 //data.MQTTVersion = 3;
sbetrabet 19:9dfb938f07eb 96 //data.clientID.cstring = "mbed-clientID";
sbetrabet 19:9dfb938f07eb 97 //data.username.cstring = "testuser";
sbetrabet 19:9dfb938f07eb 98 //data.password.cstring = "testpassword";
sbetrabet 19:9dfb938f07eb 99
icraggs 16:28d062c5522b 100 if ((rc = client.connect(data)) != 0)
geky 17:92a64d43ee61 101 printf("rc from MQTT connect is %d\n", rc);
geky 17:92a64d43ee61 102
sbetrabet 19:9dfb938f07eb 103 if ((rc = client.subscribe(topic2, MQTT::QOS1, subscribeCallback)) != 0)
mbedAustin 18:76d0899bc3ce 104 printf("Recv'd from MQTT subscribe is %d\n", rc);
icraggs 2:638c854c0695 105
icraggs 2:638c854c0695 106 MQTT::Message message;
sbetrabet 19:9dfb938f07eb 107 // QoS 0
icraggs 0:0cae29831d01 108
sbetrabet 19:9dfb938f07eb 109 sprintf(buf, "fa02d31610ce9ea0de15b22bb2fe279d, %s\n", key2);
icraggs 2:638c854c0695 110 message.qos = MQTT::QOS0;
icraggs 2:638c854c0695 111 message.retained = false;
icraggs 2:638c854c0695 112 message.dup = false;
icraggs 2:638c854c0695 113 message.payload = (void*)buf;
icraggs 2:638c854c0695 114 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 115 rc = client.publish(topic, message);
sbetrabet 19:9dfb938f07eb 116 wait(5);
sbetrabet 19:9dfb938f07eb 117 /*
sbetrabet 19:9dfb938f07eb 118 sprintf(buf, "dCglDR-G9WoQUiig0XMNhOV4ozIEIVu3rWdBh5IlQ7d, %s\n", key2);
sbetrabet 19:9dfb938f07eb 119 message.payload = (void*)buf;
sbetrabet 19:9dfb938f07eb 120 message.payloadlen = strlen(buf)+1;
sbetrabet 19:9dfb938f07eb 121 rc = client.publish(topic2, message);
sbetrabet 19:9dfb938f07eb 122 */
sbetrabet 19:9dfb938f07eb 123 while (arrivedcount < 10)
icraggs 2:638c854c0695 124 client.yield(100);
sbetrabet 19:9dfb938f07eb 125 /*
icraggs 2:638c854c0695 126 // QoS 1
icraggs 2:638c854c0695 127 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version);
icraggs 2:638c854c0695 128 message.qos = MQTT::QOS1;
icraggs 2:638c854c0695 129 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 130 rc = client.publish(topic, message);
sbetrabet 19:9dfb938f07eb 131 while (arrivedcount < 10)
icraggs 2:638c854c0695 132 client.yield(100);
sbetrabet 19:9dfb938f07eb 133
icraggs 2:638c854c0695 134 // QoS 2
icraggs 2:638c854c0695 135 sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version);
icraggs 2:638c854c0695 136 message.qos = MQTT::QOS2;
icraggs 2:638c854c0695 137 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 138 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 139 while (arrivedcount < 3)
icraggs 2:638c854c0695 140 client.yield(100);
geky 17:92a64d43ee61 141
icraggs 12:086a9314e8a5 142 // n * QoS 2
sbetrabet 19:9dfb938f07eb 143
geky 17:92a64d43ee61 144 for (int i = 1; i <= 10; ++i) {
icraggs 12:086a9314e8a5 145 sprintf(buf, "Hello World! QoS 2 message number %d from app version %f\n", i, version);
icraggs 12:086a9314e8a5 146 message.qos = MQTT::QOS2;
icraggs 12:086a9314e8a5 147 message.payloadlen = strlen(buf)+1;
icraggs 16:28d062c5522b 148 rc = client.publish(topic, message);
icraggs 12:086a9314e8a5 149 while (arrivedcount < i + 3)
icraggs 12:086a9314e8a5 150 client.yield(100);
icraggs 12:086a9314e8a5 151 }
sbetrabet 19:9dfb938f07eb 152 */
sbetrabet 19:9dfb938f07eb 153
icraggs 8:a3e3113054a1 154 if ((rc = client.unsubscribe(topic)) != 0)
sam_grove 5:4a257f6ac09a 155 printf("rc from unsubscribe was %d\n", rc);
geky 17:92a64d43ee61 156
icraggs 8:a3e3113054a1 157 if ((rc = client.disconnect()) != 0)
sam_grove 5:4a257f6ac09a 158 printf("rc from disconnect was %d\n", rc);
geky 17:92a64d43ee61 159
sbetrabet 19:9dfb938f07eb 160 //ipstack.disconnect();
sbetrabet 19:9dfb938f07eb 161 //printf("Finishing with %d messages received\n", arrivedcount);
geky 17:92a64d43ee61 162
icraggs 0:0cae29831d01 163 return 0;
icraggs 0:0cae29831d01 164 }