MQTT_PM25_1
Dependencies: mbed-os-example-wifi-DISCO_IOTBOARD_MQTT MQTT
Diff: main.cpp
- Revision:
- 59:9bbcc1b368ba
- Parent:
- 58:8d4bde75ebb9
- Child:
- 60:5e586131c009
diff -r 8d4bde75ebb9 -r 9bbcc1b368ba main.cpp --- a/main.cpp Tue Feb 27 14:07:49 2018 +0100 +++ b/main.cpp Tue Apr 23 09:45:23 2019 +0000 @@ -14,6 +14,20 @@ * limitations under the License. */ +//MQTT+WIFI + + +#define logMessage printf +#define MQTTCLIENT_QOS2 1 + +#include "MQTTNetwork.h" +#include "MQTTmbed.h" +#include "MQTTClient.h" + +int arrivedcount = 0; +// + + #include "mbed.h" #include "TCPSocket.h" @@ -32,6 +46,16 @@ #endif + +void messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); + logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); + ++arrivedcount; +} + + const char *sec2str(nsapi_security_t sec) { switch (sec) { @@ -137,6 +161,7 @@ printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); + //int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA); if (ret != 0) { printf("\nConnection error\n"); return -1; @@ -151,7 +176,84 @@ http_demo(&wifi); - wifi.disconnect(); + //wifi.disconnect(); + + printf("\Wifi Example Done,MQTT Example Start\n"); + + // MQTT Example Start + float version = 0.6; + char* topic = "test1"; + + logMessage("HelloMQTT: version is %.2f\r\n", version); + + NetworkInterface* network = &wifi; + if (!network) { + return -1; + } + + MQTTNetwork mqttNetwork(network); + + MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); + + const char* hostname = "192.168.0.120"; + int port = 1883; + logMessage("Connecting to %s:%d\r\n", hostname, port); + int rc = mqttNetwork.connect(hostname, port); + if (rc != 0) + logMessage("rc from TCP connect is %d\r\n", rc); + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "mbed-sample"; + data.username.cstring = "testuser"; + data.password.cstring = "testpassword"; + if ((rc = client.connect(data)) != 0) + logMessage("rc from MQTT connect is %d\r\n", rc); + + if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) + logMessage("rc from MQTT subscribe is %d\r\n", rc); + + MQTT::Message message; - printf("\nDone\n"); + // QoS 0 + char buf[100]; + sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version); + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + 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\r\n", version); + message.qos = MQTT::QOS1; + message.payloadlen = strlen(buf)+1; + rc = client.publish(topic, message); + while (arrivedcount < 2) + client.yield(100); + + // QoS 2 + sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version); + message.qos = MQTT::QOS2; + message.payloadlen = strlen(buf)+1; + rc = client.publish(topic, message); + while (arrivedcount < 3) + client.yield(100); + + if ((rc = client.unsubscribe(topic)) != 0) + logMessage("rc from unsubscribe was %d\r\n", rc); + + if ((rc = client.disconnect()) != 0) + logMessage("rc from disconnect was %d\r\n", rc); + + mqttNetwork.disconnect(); + + logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount); + + return 0; + + }