mqtt esp test
Dependencies: ESP8266Interface MQTT mbed-rtos mbed-src
Fork of ESP8266_MQTT_HelloWorld by
Diff: main.cpp
- Revision:
- 19:1174b87e2111
- Parent:
- 18:76d0899bc3ce
--- a/main.cpp Tue Jun 09 17:57:43 2015 +0000 +++ b/main.cpp Wed Dec 21 10:08:19 2016 +0000 @@ -29,7 +29,7 @@ #include "MQTTClient.h" int arrivedcount = 0; - +Serial pc(PB_6, PB_7);; // callback for subscribe topic void subscribeCallback(MQTT::MessageData& md) { @@ -42,31 +42,31 @@ // main function that sets up the subscription and makes a couple publishes int main(int argc, char* argv[]) { - printf("Starting\n"); - MQTTESP8266 ipstack(D1, D0, D10, "demossid","passphrase"); // change to match your wifi access point + pc.printf("Starting\n"); + MQTTESP8266 ipstack(PA_2, PA_3, PA_7, "FearOfTheDark","PabloEscobar"); // change to match your wifi access point float version = 0.47; - char* topic = "mbed-sample"; + char* topic = "abc123"; - printf("Version is %f\n", version); + pc.printf("Version is %f\n", version); MQTT::Client<MQTTESP8266, Countdown> client = MQTT::Client<MQTTESP8266, Countdown>(ipstack); - char* hostname = "85.119.83.194"; // test.mosquitto.org + char* hostname = "128.199.121.240"; // test.mosquitto.org int port = 1883; int rc = ipstack.connect(hostname, port); if (rc != 0) - printf("rc from TCP connect is %d\n", rc); + pc.printf("rc from TCP connect is %d\n", rc); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; - data.clientID.cstring = "mbed-clientID"; - data.username.cstring = "testuser"; - data.password.cstring = "testpassword"; + //data.clientID.cstring = "mbed-clientID"; + //data.username.cstring = "testuser"; + //data.password.cstring = "testpassword"; if ((rc = client.connect(data)) != 0) - printf("rc from MQTT connect is %d\n", rc); + pc.printf("rc from MQTT connect is %d\n", rc); if ((rc = client.subscribe(topic, MQTT::QOS1, subscribeCallback)) != 0) - printf("Recv'd from MQTT subscribe is %d\n", rc); + pc.printf("Recv'd from MQTT subscribe is %d\n", rc); MQTT::Message message; @@ -80,7 +80,10 @@ message.payloadlen = strlen(buf)+1; rc = client.publish(topic, message); while (arrivedcount < 1) + { client.yield(100); + + } // QoS 1 sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); @@ -109,13 +112,13 @@ } if ((rc = client.unsubscribe(topic)) != 0) - printf("rc from unsubscribe was %d\n", rc); + pc.printf("rc from unsubscribe was %d\n", rc); if ((rc = client.disconnect()) != 0) - printf("rc from disconnect was %d\n", rc); + pc.printf("rc from disconnect was %d\n", rc); ipstack.disconnect(); - printf("Finishing with %d messages received\n", arrivedcount); + pc.printf("Finishing with %d messages received\n", arrivedcount); return 0; }