https://github.com/RozenAstrayChen/MQTT-IDW01M1
Dependencies: MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Revision 1:2ee9e5685a1e, committed 2017-10-31
- Comitter:
- rozendhyan@gmail.com
- Date:
- Tue Oct 31 23:56:09 2017 +0800
- Parent:
- 0:80c7778c82b8
- Commit message:
- add subscribe
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Oct 31 18:41:30 2017 +0800 +++ b/main.cpp Tue Oct 31 23:56:09 2017 +0800 @@ -13,6 +13,7 @@ #define MQTT_PORT 1883 //MQTT use Topic #define TOPIC "temp" +#define SUB_TOPIC "LED" //Wifi network @@ -37,16 +38,26 @@ MQTTString TopicName={TOPIC}; MQTT::MessageData MsgData(TopicName, message); +void subscribe_LED(char* msg){ + int value = atoi(msg); + //printf("value = %d\n", value); + if(value ==1){ + myled = !myled; + } + +} + void subscribe_cb(MQTT::MessageData & msgMQTT) { char msg[MQTT_MAX_PAYLOAD_SIZE]; msg[0]='\0'; strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen); printf ("--->>> subscribe_cb msg: %s\n\r", msg); + subscribe_LED(msg); } int subscribe(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) { - char* pubTopic = TOPIC; + char* pubTopic = SUB_TOPIC; return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb); } @@ -89,9 +100,9 @@ if((rc = client->connect(data)) == 0){ connected = true; printf("--->MQTT Connected\n\r"); -#ifdef SUBSCRIBE - if (!subscribe(client, ipstack)) printf ("--->>>MQTT subscribed to: %s\n\r",TOPIC); -#endif +//#ifdef SUBSCRIBE + if (!subscribe(client, ipstack)) printf ("--->>>MQTT subscribed to: %s\n\r",SUB_TOPIC); +//#endif }else { WARN("MQTT connect returned %d\n", rc); } @@ -172,7 +183,7 @@ MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); attemptConnect(&client, &ipstack); - myled=1; + int count = 0; // tyeld.start(); while (true) @@ -180,9 +191,9 @@ if (++count == 100) { // Publish a message every second if (publish(&client, &ipstack) != 0) { - myled=0; + attemptConnect(&client, &ipstack); // if we have lost the connection - } else myled=1; + } count = 0; } // int start = tyeld.read_ms();