MQTT-WIFI
Dependents: MQTT_PM25 MQTT_PM25_1 mbed_wifi_MQTT_PM25 1218
Revision 59:9bbcc1b368ba, committed 2019-04-23
- Comitter:
- cornetlin
- Date:
- Tue Apr 23 09:45:23 2019 +0000
- Parent:
- 58:8d4bde75ebb9
- Commit message:
- MQTT_WIFI
Changed in this revision
diff -r 8d4bde75ebb9 -r 9bbcc1b368ba MQTT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTT.lib Tue Apr 23 09:45:23 2019 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/teams/mqtt/code/MQTT/#9cff7b6bbd01
diff -r 8d4bde75ebb9 -r 9bbcc1b368ba MQTTNetwork.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTTNetwork.h Tue Apr 23 09:45:23 2019 +0000 @@ -0,0 +1,39 @@ +#ifndef _MQTTNETWORK_H_ +#define _MQTTNETWORK_H_ + +#include "NetworkInterface.h" +#include "TCPSocket.h" + +class MQTTNetwork { +public: + MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) { + socket = new TCPSocket(); + } + + ~MQTTNetwork() { + delete socket; + } + + int read(unsigned char* buffer, int len, int timeout) { + return socket->recv(buffer, len); + } + + int write(unsigned char* buffer, int len, int timeout) { + return socket->send(buffer, len); + } + + int connect(const char* hostname, int port) { + socket->open(network); + return socket->connect(hostname, port); + } + + int disconnect() { + return socket->close(); + } + +private: + NetworkInterface* network; + TCPSocket* socket; +}; + +#endif // _MQTTNETWORK_H_
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; + + }
diff -r 8d4bde75ebb9 -r 9bbcc1b368ba mbed_app.json --- a/mbed_app.json Tue Feb 27 14:07:49 2018 +0100 +++ b/mbed_app.json Tue Apr 23 09:45:23 2019 +0000 @@ -6,11 +6,11 @@ }, "wifi-ssid": { "help": "WiFi SSID", - "value": "\"SSID\"" + "value": "\"note9\"" }, "wifi-password": { "help": "WiFi Password", - "value": "\"PASSWORD\"" + "value": "\"tp67651209\"" }, "wifi-tx": { "help": "TX pin for serial connection to external device",
diff -r 8d4bde75ebb9 -r 9bbcc1b368ba wifi-ism43362.lib --- a/wifi-ism43362.lib Tue Feb 27 14:07:49 2018 +0100 +++ b/wifi-ism43362.lib Tue Apr 23 09:45:23 2019 +0000 @@ -1,2 +1,1 @@ -https://github.com/ARMmbed/wifi-ism43362/#e4ecc27e87d96072f7df62a25ef007986dc95c4e - +https://github.com/ARMmbed/wifi-ism43362/#43817d8fe90469c57d5b21f7a8a1acb66d890392
diff -r 8d4bde75ebb9 -r 9bbcc1b368ba wifi-x-nucleo-idw01m1.lib --- a/wifi-x-nucleo-idw01m1.lib Tue Feb 27 14:07:49 2018 +0100 +++ b/wifi-x-nucleo-idw01m1.lib Tue Apr 23 09:45:23 2019 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/wifi-x-nucleo-idw01m1/#5871f7011d7ff2c50e3faf992ebab88e9f69dc95 \ No newline at end of file +https://github.com/ARMmbed/wifi-x-nucleo-idw01m1/#376b457a84631f11178b895bee5c790ff6c3d768