MQTT_PM25_1

Dependencies:   mbed-os-example-wifi-DISCO_IOTBOARD_MQTT MQTT

Committer:
cornetlin
Date:
Tue Apr 23 09:45:23 2019 +0000
Revision:
59:9bbcc1b368ba
Parent:
58:8d4bde75ebb9
Child:
60:5e586131c009
MQTT_WIFI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:857719181846 1 /* WiFi Example
adustm 57:c8c960b825f0 2 * Copyright (c) 2018 ARM Limited
mbed_official 0:857719181846 3 *
mbed_official 0:857719181846 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:857719181846 5 * you may not use this file except in compliance with the License.
mbed_official 0:857719181846 6 * You may obtain a copy of the License at
mbed_official 0:857719181846 7 *
mbed_official 0:857719181846 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:857719181846 9 *
mbed_official 0:857719181846 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:857719181846 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:857719181846 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:857719181846 13 * See the License for the specific language governing permissions and
mbed_official 0:857719181846 14 * limitations under the License.
mbed_official 0:857719181846 15 */
mbed_official 0:857719181846 16
cornetlin 59:9bbcc1b368ba 17 //MQTT+WIFI
cornetlin 59:9bbcc1b368ba 18
cornetlin 59:9bbcc1b368ba 19
cornetlin 59:9bbcc1b368ba 20 #define logMessage printf
cornetlin 59:9bbcc1b368ba 21 #define MQTTCLIENT_QOS2 1
cornetlin 59:9bbcc1b368ba 22
cornetlin 59:9bbcc1b368ba 23 #include "MQTTNetwork.h"
cornetlin 59:9bbcc1b368ba 24 #include "MQTTmbed.h"
cornetlin 59:9bbcc1b368ba 25 #include "MQTTClient.h"
cornetlin 59:9bbcc1b368ba 26
cornetlin 59:9bbcc1b368ba 27 int arrivedcount = 0;
cornetlin 59:9bbcc1b368ba 28 //
cornetlin 59:9bbcc1b368ba 29
cornetlin 59:9bbcc1b368ba 30
mbed_official 0:857719181846 31 #include "mbed.h"
mbed_official 0:857719181846 32 #include "TCPSocket.h"
mbed_official 0:857719181846 33
mbed_official 44:63be19b7a3db 34 #define WIFI_IDW0XX1 2
mbed_official 32:bca3f5f442b3 35
adustm 58:8d4bde75ebb9 36 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
adustm adustm@gmail.com 55:23a0d1e8270a 37 #include "ISM43362Interface.h"
adustm adustm@gmail.com 55:23a0d1e8270a 38 ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, MBED_CONF_APP_WIFI_SPI_MISO, MBED_CONF_APP_WIFI_SPI_SCLK, MBED_CONF_APP_WIFI_SPI_NSS, MBED_CONF_APP_WIFI_RESET, MBED_CONF_APP_WIFI_DATAREADY, MBED_CONF_APP_WIFI_WAKEUP, false);
adustm adustm@gmail.com 55:23a0d1e8270a 39
mbed_official 33:12f0df4d51d7 40 #else // External WiFi modules
mbed_official 21:b2f2f6a840b4 41
adustm 57:c8c960b825f0 42 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
mbed_official 32:bca3f5f442b3 43 #include "SpwfSAInterface.h"
mbed_official 32:bca3f5f442b3 44 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 44:63be19b7a3db 45 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
mbed_official 10:5b5beb106156 46
mbed_official 33:12f0df4d51d7 47 #endif
mbed_official 0:857719181846 48
cornetlin 59:9bbcc1b368ba 49
cornetlin 59:9bbcc1b368ba 50 void messageArrived(MQTT::MessageData& md)
cornetlin 59:9bbcc1b368ba 51 {
cornetlin 59:9bbcc1b368ba 52 MQTT::Message &message = md.message;
cornetlin 59:9bbcc1b368ba 53 logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
cornetlin 59:9bbcc1b368ba 54 logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
cornetlin 59:9bbcc1b368ba 55 ++arrivedcount;
cornetlin 59:9bbcc1b368ba 56 }
cornetlin 59:9bbcc1b368ba 57
cornetlin 59:9bbcc1b368ba 58
mbed_official 0:857719181846 59 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 60 {
mbed_official 0:857719181846 61 switch (sec) {
mbed_official 0:857719181846 62 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 63 return "None";
mbed_official 0:857719181846 64 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 65 return "WEP";
mbed_official 0:857719181846 66 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 67 return "WPA";
mbed_official 0:857719181846 68 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 69 return "WPA2";
mbed_official 0:857719181846 70 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 71 return "WPA/WPA2";
mbed_official 0:857719181846 72 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 73 default:
mbed_official 0:857719181846 74 return "Unknown";
mbed_official 0:857719181846 75 }
mbed_official 0:857719181846 76 }
mbed_official 0:857719181846 77
mbed_official 37:3a31525e2971 78 int scan_demo(WiFiInterface *wifi)
mbed_official 0:857719181846 79 {
mbed_official 0:857719181846 80 WiFiAccessPoint *ap;
mbed_official 0:857719181846 81
mbed_official 35:052c1ba06ce7 82 printf("Scan:\n");
mbed_official 0:857719181846 83
mbed_official 0:857719181846 84 int count = wifi->scan(NULL,0);
adustm adustm@gmail.com 55:23a0d1e8270a 85 printf("%d networks available.\n", count);
mbed_official 0:857719181846 86
mbed_official 0:857719181846 87 /* Limit number of network arbitrary to 15 */
mbed_official 0:857719181846 88 count = count < 15 ? count : 15;
mbed_official 0:857719181846 89
mbed_official 0:857719181846 90 ap = new WiFiAccessPoint[count];
mbed_official 0:857719181846 91 count = wifi->scan(ap, count);
mbed_official 0:857719181846 92 for (int i = 0; i < count; i++)
mbed_official 0:857719181846 93 {
mbed_official 35:052c1ba06ce7 94 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
mbed_official 0:857719181846 95 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
mbed_official 0:857719181846 96 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
mbed_official 0:857719181846 97 }
mbed_official 0:857719181846 98
mbed_official 0:857719181846 99 delete[] ap;
mbed_official 37:3a31525e2971 100 return count;
mbed_official 0:857719181846 101 }
mbed_official 0:857719181846 102
mbed_official 0:857719181846 103 void http_demo(NetworkInterface *net)
mbed_official 0:857719181846 104 {
mbed_official 0:857719181846 105 TCPSocket socket;
mbed_official 24:323569a565ec 106 nsapi_error_t response;
mbed_official 0:857719181846 107
mbed_official 35:052c1ba06ce7 108 printf("Sending HTTP request to www.arm.com...\n");
mbed_official 0:857719181846 109
mbed_official 0:857719181846 110 // Open a socket on the network interface, and create a TCP connection to www.arm.com
mbed_official 0:857719181846 111 socket.open(net);
mbed_official 24:323569a565ec 112 response = socket.connect("www.arm.com", 80);
mbed_official 24:323569a565ec 113 if(0 != response) {
mbed_official 35:052c1ba06ce7 114 printf("Error connecting: %d\n", response);
mbed_official 24:323569a565ec 115 socket.close();
mbed_official 24:323569a565ec 116 return;
mbed_official 24:323569a565ec 117 }
mbed_official 0:857719181846 118
mbed_official 0:857719181846 119 // Send a simple http request
mbed_official 0:857719181846 120 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
mbed_official 33:12f0df4d51d7 121 nsapi_size_t size = strlen(sbuffer);
mbed_official 24:323569a565ec 122 response = 0;
mbed_official 24:323569a565ec 123 while(size)
mbed_official 24:323569a565ec 124 {
mbed_official 24:323569a565ec 125 response = socket.send(sbuffer+response, size);
mbed_official 24:323569a565ec 126 if (response < 0) {
mbed_official 35:052c1ba06ce7 127 printf("Error sending data: %d\n", response);
mbed_official 24:323569a565ec 128 socket.close();
mbed_official 24:323569a565ec 129 return;
mbed_official 24:323569a565ec 130 } else {
mbed_official 24:323569a565ec 131 size -= response;
mbed_official 24:323569a565ec 132 // Check if entire message was sent or not
mbed_official 35:052c1ba06ce7 133 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 24:323569a565ec 134 }
mbed_official 24:323569a565ec 135 }
mbed_official 0:857719181846 136
mbed_official 0:857719181846 137 // Recieve a simple http response and print out the response line
mbed_official 0:857719181846 138 char rbuffer[64];
mbed_official 24:323569a565ec 139 response = socket.recv(rbuffer, sizeof rbuffer);
mbed_official 24:323569a565ec 140 if (response < 0) {
mbed_official 35:052c1ba06ce7 141 printf("Error receiving data: %d\n", response);
mbed_official 24:323569a565ec 142 } else {
mbed_official 35:052c1ba06ce7 143 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mbed_official 24:323569a565ec 144 }
mbed_official 0:857719181846 145
mbed_official 0:857719181846 146 // Close the socket to return its memory and bring down the network interface
mbed_official 0:857719181846 147 socket.close();
mbed_official 0:857719181846 148 }
mbed_official 0:857719181846 149
mbed_official 0:857719181846 150 int main()
mbed_official 0:857719181846 151 {
mbed_official 37:3a31525e2971 152 int count = 0;
mbed_official 37:3a31525e2971 153
mbed_official 35:052c1ba06ce7 154 printf("WiFi example\n\n");
mbed_official 0:857719181846 155
mbed_official 37:3a31525e2971 156 count = scan_demo(&wifi);
mbed_official 37:3a31525e2971 157 if (count == 0) {
mbed_official 37:3a31525e2971 158 printf("No WIFI APNs found - can't continue further.\n");
mbed_official 37:3a31525e2971 159 return -1;
mbed_official 37:3a31525e2971 160 }
mbed_official 0:857719181846 161
mbed_official 37:3a31525e2971 162 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
mbed_official 0:857719181846 163 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
cornetlin 59:9bbcc1b368ba 164 //int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA);
mbed_official 0:857719181846 165 if (ret != 0) {
mbed_official 35:052c1ba06ce7 166 printf("\nConnection error\n");
mbed_official 0:857719181846 167 return -1;
mbed_official 0:857719181846 168 }
mbed_official 0:857719181846 169
mbed_official 35:052c1ba06ce7 170 printf("Success\n\n");
mbed_official 35:052c1ba06ce7 171 printf("MAC: %s\n", wifi.get_mac_address());
mbed_official 35:052c1ba06ce7 172 printf("IP: %s\n", wifi.get_ip_address());
mbed_official 35:052c1ba06ce7 173 printf("Netmask: %s\n", wifi.get_netmask());
mbed_official 35:052c1ba06ce7 174 printf("Gateway: %s\n", wifi.get_gateway());
mbed_official 35:052c1ba06ce7 175 printf("RSSI: %d\n\n", wifi.get_rssi());
mbed_official 0:857719181846 176
mbed_official 0:857719181846 177 http_demo(&wifi);
mbed_official 0:857719181846 178
cornetlin 59:9bbcc1b368ba 179 //wifi.disconnect();
cornetlin 59:9bbcc1b368ba 180
cornetlin 59:9bbcc1b368ba 181 printf("\Wifi Example Done,MQTT Example Start\n");
cornetlin 59:9bbcc1b368ba 182
cornetlin 59:9bbcc1b368ba 183 // MQTT Example Start
cornetlin 59:9bbcc1b368ba 184 float version = 0.6;
cornetlin 59:9bbcc1b368ba 185 char* topic = "test1";
cornetlin 59:9bbcc1b368ba 186
cornetlin 59:9bbcc1b368ba 187 logMessage("HelloMQTT: version is %.2f\r\n", version);
cornetlin 59:9bbcc1b368ba 188
cornetlin 59:9bbcc1b368ba 189 NetworkInterface* network = &wifi;
cornetlin 59:9bbcc1b368ba 190 if (!network) {
cornetlin 59:9bbcc1b368ba 191 return -1;
cornetlin 59:9bbcc1b368ba 192 }
cornetlin 59:9bbcc1b368ba 193
cornetlin 59:9bbcc1b368ba 194 MQTTNetwork mqttNetwork(network);
cornetlin 59:9bbcc1b368ba 195
cornetlin 59:9bbcc1b368ba 196 MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
cornetlin 59:9bbcc1b368ba 197
cornetlin 59:9bbcc1b368ba 198 const char* hostname = "192.168.0.120";
cornetlin 59:9bbcc1b368ba 199 int port = 1883;
cornetlin 59:9bbcc1b368ba 200 logMessage("Connecting to %s:%d\r\n", hostname, port);
cornetlin 59:9bbcc1b368ba 201 int rc = mqttNetwork.connect(hostname, port);
cornetlin 59:9bbcc1b368ba 202 if (rc != 0)
cornetlin 59:9bbcc1b368ba 203 logMessage("rc from TCP connect is %d\r\n", rc);
cornetlin 59:9bbcc1b368ba 204
cornetlin 59:9bbcc1b368ba 205 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
cornetlin 59:9bbcc1b368ba 206 data.MQTTVersion = 3;
cornetlin 59:9bbcc1b368ba 207 data.clientID.cstring = "mbed-sample";
cornetlin 59:9bbcc1b368ba 208 data.username.cstring = "testuser";
cornetlin 59:9bbcc1b368ba 209 data.password.cstring = "testpassword";
cornetlin 59:9bbcc1b368ba 210 if ((rc = client.connect(data)) != 0)
cornetlin 59:9bbcc1b368ba 211 logMessage("rc from MQTT connect is %d\r\n", rc);
cornetlin 59:9bbcc1b368ba 212
cornetlin 59:9bbcc1b368ba 213 if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0)
cornetlin 59:9bbcc1b368ba 214 logMessage("rc from MQTT subscribe is %d\r\n", rc);
cornetlin 59:9bbcc1b368ba 215
cornetlin 59:9bbcc1b368ba 216 MQTT::Message message;
mbed_official 0:857719181846 217
cornetlin 59:9bbcc1b368ba 218 // QoS 0
cornetlin 59:9bbcc1b368ba 219 char buf[100];
cornetlin 59:9bbcc1b368ba 220 sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
cornetlin 59:9bbcc1b368ba 221 message.qos = MQTT::QOS0;
cornetlin 59:9bbcc1b368ba 222 message.retained = false;
cornetlin 59:9bbcc1b368ba 223 message.dup = false;
cornetlin 59:9bbcc1b368ba 224 message.payload = (void*)buf;
cornetlin 59:9bbcc1b368ba 225 message.payloadlen = strlen(buf)+1;
cornetlin 59:9bbcc1b368ba 226 rc = client.publish(topic, message);
cornetlin 59:9bbcc1b368ba 227 while (arrivedcount < 1)
cornetlin 59:9bbcc1b368ba 228 client.yield(100);
cornetlin 59:9bbcc1b368ba 229
cornetlin 59:9bbcc1b368ba 230 // QoS 1
cornetlin 59:9bbcc1b368ba 231 sprintf(buf, "Hello World! QoS 1 message from app version %f\r\n", version);
cornetlin 59:9bbcc1b368ba 232 message.qos = MQTT::QOS1;
cornetlin 59:9bbcc1b368ba 233 message.payloadlen = strlen(buf)+1;
cornetlin 59:9bbcc1b368ba 234 rc = client.publish(topic, message);
cornetlin 59:9bbcc1b368ba 235 while (arrivedcount < 2)
cornetlin 59:9bbcc1b368ba 236 client.yield(100);
cornetlin 59:9bbcc1b368ba 237
cornetlin 59:9bbcc1b368ba 238 // QoS 2
cornetlin 59:9bbcc1b368ba 239 sprintf(buf, "Hello World! QoS 2 message from app version %f\r\n", version);
cornetlin 59:9bbcc1b368ba 240 message.qos = MQTT::QOS2;
cornetlin 59:9bbcc1b368ba 241 message.payloadlen = strlen(buf)+1;
cornetlin 59:9bbcc1b368ba 242 rc = client.publish(topic, message);
cornetlin 59:9bbcc1b368ba 243 while (arrivedcount < 3)
cornetlin 59:9bbcc1b368ba 244 client.yield(100);
cornetlin 59:9bbcc1b368ba 245
cornetlin 59:9bbcc1b368ba 246 if ((rc = client.unsubscribe(topic)) != 0)
cornetlin 59:9bbcc1b368ba 247 logMessage("rc from unsubscribe was %d\r\n", rc);
cornetlin 59:9bbcc1b368ba 248
cornetlin 59:9bbcc1b368ba 249 if ((rc = client.disconnect()) != 0)
cornetlin 59:9bbcc1b368ba 250 logMessage("rc from disconnect was %d\r\n", rc);
cornetlin 59:9bbcc1b368ba 251
cornetlin 59:9bbcc1b368ba 252 mqttNetwork.disconnect();
cornetlin 59:9bbcc1b368ba 253
cornetlin 59:9bbcc1b368ba 254 logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);
cornetlin 59:9bbcc1b368ba 255
cornetlin 59:9bbcc1b368ba 256 return 0;
cornetlin 59:9bbcc1b368ba 257
cornetlin 59:9bbcc1b368ba 258
mbed_official 0:857719181846 259 }