![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MQTT_PM25
Dependencies: mbed-os-example-wifi-DISCO_IOTBOARD_MQTT MQTT
main.cpp@60:5e586131c009, 2019-12-18 (annotated)
- Committer:
- peng103617
- Date:
- Wed Dec 18 04:09:32 2019 +0000
- Revision:
- 60:5e586131c009
- Parent:
- 59:9bbcc1b368ba
MQTT_PM2.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cornetlin | 59:9bbcc1b368ba | 1 | //MQTT+WIFI |
cornetlin | 59:9bbcc1b368ba | 2 | |
cornetlin | 59:9bbcc1b368ba | 3 | |
cornetlin | 59:9bbcc1b368ba | 4 | #define logMessage printf |
cornetlin | 59:9bbcc1b368ba | 5 | #define MQTTCLIENT_QOS2 1 |
cornetlin | 59:9bbcc1b368ba | 6 | |
cornetlin | 59:9bbcc1b368ba | 7 | #include "MQTTNetwork.h" |
cornetlin | 59:9bbcc1b368ba | 8 | #include "MQTTmbed.h" |
cornetlin | 59:9bbcc1b368ba | 9 | #include "MQTTClient.h" |
cornetlin | 59:9bbcc1b368ba | 10 | |
cornetlin | 59:9bbcc1b368ba | 11 | int arrivedcount = 0; |
cornetlin | 59:9bbcc1b368ba | 12 | // |
cornetlin | 59:9bbcc1b368ba | 13 | |
cornetlin | 59:9bbcc1b368ba | 14 | |
mbed_official | 0:857719181846 | 15 | #include "mbed.h" |
mbed_official | 0:857719181846 | 16 | #include "TCPSocket.h" |
mbed_official | 0:857719181846 | 17 | |
mbed_official | 44:63be19b7a3db | 18 | #define WIFI_IDW0XX1 2 |
mbed_official | 32:bca3f5f442b3 | 19 | |
adustm | 58:8d4bde75ebb9 | 20 | #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH)) |
adustm adustm@gmail.com | 55:23a0d1e8270a | 21 | #include "ISM43362Interface.h" |
adustm adustm@gmail.com | 55:23a0d1e8270a | 22 | 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 | 23 | |
mbed_official | 33:12f0df4d51d7 | 24 | #else // External WiFi modules |
mbed_official | 21:b2f2f6a840b4 | 25 | |
adustm | 57:c8c960b825f0 | 26 | #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
mbed_official | 32:bca3f5f442b3 | 27 | #include "SpwfSAInterface.h" |
mbed_official | 32:bca3f5f442b3 | 28 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
mbed_official | 44:63be19b7a3db | 29 | #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
mbed_official | 10:5b5beb106156 | 30 | |
mbed_official | 33:12f0df4d51d7 | 31 | #endif |
mbed_official | 0:857719181846 | 32 | |
cornetlin | 59:9bbcc1b368ba | 33 | |
cornetlin | 59:9bbcc1b368ba | 34 | void messageArrived(MQTT::MessageData& md) |
cornetlin | 59:9bbcc1b368ba | 35 | { |
cornetlin | 59:9bbcc1b368ba | 36 | MQTT::Message &message = md.message; |
peng103617 | 60:5e586131c009 | 37 | //logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
peng103617 | 60:5e586131c009 | 38 | //logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
peng103617 | 60:5e586131c009 | 39 | //++arrivedcount; |
cornetlin | 59:9bbcc1b368ba | 40 | } |
cornetlin | 59:9bbcc1b368ba | 41 | |
cornetlin | 59:9bbcc1b368ba | 42 | |
mbed_official | 0:857719181846 | 43 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 44 | { |
mbed_official | 0:857719181846 | 45 | switch (sec) { |
mbed_official | 0:857719181846 | 46 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 47 | return "None"; |
mbed_official | 0:857719181846 | 48 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 49 | return "WEP"; |
mbed_official | 0:857719181846 | 50 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 51 | return "WPA"; |
mbed_official | 0:857719181846 | 52 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 53 | return "WPA2"; |
mbed_official | 0:857719181846 | 54 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 55 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 56 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 57 | default: |
mbed_official | 0:857719181846 | 58 | return "Unknown"; |
mbed_official | 0:857719181846 | 59 | } |
mbed_official | 0:857719181846 | 60 | } |
mbed_official | 0:857719181846 | 61 | |
mbed_official | 37:3a31525e2971 | 62 | int scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 63 | { |
mbed_official | 0:857719181846 | 64 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 65 | |
mbed_official | 35:052c1ba06ce7 | 66 | printf("Scan:\n"); |
mbed_official | 0:857719181846 | 67 | |
mbed_official | 0:857719181846 | 68 | int count = wifi->scan(NULL,0); |
adustm adustm@gmail.com | 55:23a0d1e8270a | 69 | printf("%d networks available.\n", count); |
mbed_official | 0:857719181846 | 70 | |
mbed_official | 0:857719181846 | 71 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 72 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 73 | |
mbed_official | 0:857719181846 | 74 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 75 | count = wifi->scan(ap, count); |
mbed_official | 0:857719181846 | 76 | for (int i = 0; i < count; i++) |
mbed_official | 0:857719181846 | 77 | { |
mbed_official | 35:052c1ba06ce7 | 78 | 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 | 79 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
mbed_official | 0:857719181846 | 80 | 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 | 81 | } |
mbed_official | 0:857719181846 | 82 | |
mbed_official | 0:857719181846 | 83 | delete[] ap; |
mbed_official | 37:3a31525e2971 | 84 | return count; |
mbed_official | 0:857719181846 | 85 | } |
mbed_official | 0:857719181846 | 86 | |
mbed_official | 0:857719181846 | 87 | void http_demo(NetworkInterface *net) |
mbed_official | 0:857719181846 | 88 | { |
mbed_official | 0:857719181846 | 89 | TCPSocket socket; |
mbed_official | 24:323569a565ec | 90 | nsapi_error_t response; |
mbed_official | 0:857719181846 | 91 | |
mbed_official | 35:052c1ba06ce7 | 92 | printf("Sending HTTP request to www.arm.com...\n"); |
mbed_official | 0:857719181846 | 93 | |
mbed_official | 0:857719181846 | 94 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
mbed_official | 0:857719181846 | 95 | socket.open(net); |
mbed_official | 24:323569a565ec | 96 | response = socket.connect("www.arm.com", 80); |
mbed_official | 24:323569a565ec | 97 | if(0 != response) { |
mbed_official | 35:052c1ba06ce7 | 98 | printf("Error connecting: %d\n", response); |
mbed_official | 24:323569a565ec | 99 | socket.close(); |
mbed_official | 24:323569a565ec | 100 | return; |
mbed_official | 24:323569a565ec | 101 | } |
mbed_official | 0:857719181846 | 102 | |
mbed_official | 0:857719181846 | 103 | // Send a simple http request |
mbed_official | 0:857719181846 | 104 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
mbed_official | 33:12f0df4d51d7 | 105 | nsapi_size_t size = strlen(sbuffer); |
mbed_official | 24:323569a565ec | 106 | response = 0; |
mbed_official | 24:323569a565ec | 107 | while(size) |
mbed_official | 24:323569a565ec | 108 | { |
mbed_official | 24:323569a565ec | 109 | response = socket.send(sbuffer+response, size); |
mbed_official | 24:323569a565ec | 110 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 111 | printf("Error sending data: %d\n", response); |
mbed_official | 24:323569a565ec | 112 | socket.close(); |
mbed_official | 24:323569a565ec | 113 | return; |
mbed_official | 24:323569a565ec | 114 | } else { |
mbed_official | 24:323569a565ec | 115 | size -= response; |
mbed_official | 24:323569a565ec | 116 | // Check if entire message was sent or not |
mbed_official | 35:052c1ba06ce7 | 117 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
peng103617 | 60:5e586131c009 | 118 | |
mbed_official | 24:323569a565ec | 119 | } |
mbed_official | 24:323569a565ec | 120 | } |
mbed_official | 0:857719181846 | 121 | |
mbed_official | 0:857719181846 | 122 | // Recieve a simple http response and print out the response line |
mbed_official | 0:857719181846 | 123 | char rbuffer[64]; |
mbed_official | 24:323569a565ec | 124 | response = socket.recv(rbuffer, sizeof rbuffer); |
mbed_official | 24:323569a565ec | 125 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 126 | printf("Error receiving data: %d\n", response); |
mbed_official | 24:323569a565ec | 127 | } else { |
mbed_official | 35:052c1ba06ce7 | 128 | printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
mbed_official | 24:323569a565ec | 129 | } |
mbed_official | 0:857719181846 | 130 | |
mbed_official | 0:857719181846 | 131 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:857719181846 | 132 | socket.close(); |
mbed_official | 0:857719181846 | 133 | } |
mbed_official | 0:857719181846 | 134 | |
peng103617 | 60:5e586131c009 | 135 | |
peng103617 | 60:5e586131c009 | 136 | Serial particle_sensor(PA_0,PA_1);//TX,RX |
peng103617 | 60:5e586131c009 | 137 | Serial pc(USBTX,USBRX); |
peng103617 | 60:5e586131c009 | 138 | |
peng103617 | 60:5e586131c009 | 139 | float SDS018 () |
peng103617 | 60:5e586131c009 | 140 | { |
peng103617 | 60:5e586131c009 | 141 | uint8_t oneByte =0, myData[10],mCheck =0; |
peng103617 | 60:5e586131c009 | 142 | uint32_t samples_par = 1000; |
peng103617 | 60:5e586131c009 | 143 | int aux=0, t_init =0,i=0; |
peng103617 | 60:5e586131c009 | 144 | float PM2_5_A = 0, PM10_A = 0, PM2_5_sum = 0, PM10_sum = 0,PM2_5_avg = 0, PM10_avg = 0,PM2_5,PM10; |
peng103617 | 60:5e586131c009 | 145 | Timer t; |
peng103617 | 60:5e586131c009 | 146 | |
peng103617 | 60:5e586131c009 | 147 | time_t seconds = time(NULL); |
peng103617 | 60:5e586131c009 | 148 | //printf("Unix Epoch time = %d\r\n", seconds); |
peng103617 | 60:5e586131c009 | 149 | //while(true){ |
peng103617 | 60:5e586131c009 | 150 | // t.start(); |
peng103617 | 60:5e586131c009 | 151 | // int time_start=t.read_ms(); |
peng103617 | 60:5e586131c009 | 152 | //pc.printf("\nPARTICLE\n\r"); |
peng103617 | 60:5e586131c009 | 153 | while (aux < samples_par) //while(t.read_ms()-t_init < time_avg) |
peng103617 | 60:5e586131c009 | 154 | { |
peng103617 | 60:5e586131c009 | 155 | if (aux==0) |
peng103617 | 60:5e586131c009 | 156 | t_init = t.read_ms(); |
peng103617 | 60:5e586131c009 | 157 | while (particle_sensor.readable()) { // whenever sensor transmits packet (every 1 s) |
peng103617 | 60:5e586131c009 | 158 | oneByte = particle_sensor.getc(); |
peng103617 | 60:5e586131c009 | 159 | if(oneByte == 0xAA) { |
peng103617 | 60:5e586131c009 | 160 | myData[0] = oneByte; |
peng103617 | 60:5e586131c009 | 161 | oneByte = particle_sensor.getc(); |
peng103617 | 60:5e586131c009 | 162 | if(oneByte == 0xC0) { |
peng103617 | 60:5e586131c009 | 163 | myData[1] = oneByte; |
peng103617 | 60:5e586131c009 | 164 | mCheck = 0; |
peng103617 | 60:5e586131c009 | 165 | for(i=0; i < 6; i++) { // DATA 1-6 received and crc calc |
peng103617 | 60:5e586131c009 | 166 | myData[i+2] = particle_sensor.getc(); |
peng103617 | 60:5e586131c009 | 167 | //wait_ms(2); |
peng103617 | 60:5e586131c009 | 168 | mCheck += myData[i+2]; |
peng103617 | 60:5e586131c009 | 169 | if(myData[6]==0x91) |
peng103617 | 60:5e586131c009 | 170 | { |
peng103617 | 60:5e586131c009 | 171 | //pc.printf("91\n\r"); |
peng103617 | 60:5e586131c009 | 172 | } |
peng103617 | 60:5e586131c009 | 173 | if(myData[7]==0xA1) |
peng103617 | 60:5e586131c009 | 174 | { |
peng103617 | 60:5e586131c009 | 175 | //pc.printf("A1\n\r"); |
peng103617 | 60:5e586131c009 | 176 | } |
peng103617 | 60:5e586131c009 | 177 | } |
peng103617 | 60:5e586131c009 | 178 | myData[8] = particle_sensor.getc(); |
peng103617 | 60:5e586131c009 | 179 | myData[9] = particle_sensor.getc(); // tail (AB) |
peng103617 | 60:5e586131c009 | 180 | if(mCheck == myData[8]) { // if checksum OK |
peng103617 | 60:5e586131c009 | 181 | PM2_5 = ((uint16_t)myData[2] | (uint16_t)(myData[3]<<8)); |
peng103617 | 60:5e586131c009 | 182 | PM10 = ((uint16_t)myData[4] | (uint16_t)(myData[5]<<8)); |
peng103617 | 60:5e586131c009 | 183 | PM2_5_A = (float) PM2_5/10; |
peng103617 | 60:5e586131c009 | 184 | PM10_A = (float) PM10/10; |
peng103617 | 60:5e586131c009 | 185 | if(PM2_5_A*10 > 9999) |
peng103617 | 60:5e586131c009 | 186 | PM2_5_A = 999.9; |
peng103617 | 60:5e586131c009 | 187 | if(PM10_A*10 > 9999) |
peng103617 | 60:5e586131c009 | 188 | PM10_A = 999.9; |
peng103617 | 60:5e586131c009 | 189 | aux++; |
peng103617 | 60:5e586131c009 | 190 | PM2_5_sum+=PM2_5_A; // sum samples for future averaging |
peng103617 | 60:5e586131c009 | 191 | |
peng103617 | 60:5e586131c009 | 192 | PM10_sum+=PM10_A; // sum samples for future averaging |
peng103617 | 60:5e586131c009 | 193 | //printf("%3.2f,\t%3.2f\n\r", PM2_5_A, PM10_A); // CSV print: PM2.5, PM10 |
peng103617 | 60:5e586131c009 | 194 | //printf("PM2_5_A = %3.2f", PM2_5_A); //PM2.5 |
peng103617 | 60:5e586131c009 | 195 | return PM2_5_A; |
peng103617 | 60:5e586131c009 | 196 | } |
peng103617 | 60:5e586131c009 | 197 | } // head2 |
peng103617 | 60:5e586131c009 | 198 | } // head1 |
peng103617 | 60:5e586131c009 | 199 | |
peng103617 | 60:5e586131c009 | 200 | } // while readable() |
peng103617 | 60:5e586131c009 | 201 | |
peng103617 | 60:5e586131c009 | 202 | } |
peng103617 | 60:5e586131c009 | 203 | } |
peng103617 | 60:5e586131c009 | 204 | |
peng103617 | 60:5e586131c009 | 205 | |
peng103617 | 60:5e586131c009 | 206 | |
peng103617 | 60:5e586131c009 | 207 | |
peng103617 | 60:5e586131c009 | 208 | |
peng103617 | 60:5e586131c009 | 209 | |
mbed_official | 0:857719181846 | 210 | int main() |
peng103617 | 60:5e586131c009 | 211 | |
peng103617 | 60:5e586131c009 | 212 | { |
peng103617 | 60:5e586131c009 | 213 | |
peng103617 | 60:5e586131c009 | 214 | |
peng103617 | 60:5e586131c009 | 215 | |
peng103617 | 60:5e586131c009 | 216 | pc.baud (115200) ; |
peng103617 | 60:5e586131c009 | 217 | |
mbed_official | 37:3a31525e2971 | 218 | int count = 0; |
mbed_official | 37:3a31525e2971 | 219 | |
mbed_official | 35:052c1ba06ce7 | 220 | printf("WiFi example\n\n"); |
mbed_official | 0:857719181846 | 221 | |
mbed_official | 37:3a31525e2971 | 222 | count = scan_demo(&wifi); |
mbed_official | 37:3a31525e2971 | 223 | if (count == 0) { |
mbed_official | 37:3a31525e2971 | 224 | printf("No WIFI APNs found - can't continue further.\n"); |
mbed_official | 37:3a31525e2971 | 225 | return -1; |
mbed_official | 37:3a31525e2971 | 226 | } |
mbed_official | 0:857719181846 | 227 | |
mbed_official | 37:3a31525e2971 | 228 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
mbed_official | 0:857719181846 | 229 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
cornetlin | 59:9bbcc1b368ba | 230 | //int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA); |
mbed_official | 0:857719181846 | 231 | if (ret != 0) { |
mbed_official | 35:052c1ba06ce7 | 232 | printf("\nConnection error\n"); |
mbed_official | 0:857719181846 | 233 | return -1; |
mbed_official | 0:857719181846 | 234 | } |
mbed_official | 0:857719181846 | 235 | |
mbed_official | 35:052c1ba06ce7 | 236 | printf("Success\n\n"); |
mbed_official | 35:052c1ba06ce7 | 237 | printf("MAC: %s\n", wifi.get_mac_address()); |
mbed_official | 35:052c1ba06ce7 | 238 | printf("IP: %s\n", wifi.get_ip_address()); |
mbed_official | 35:052c1ba06ce7 | 239 | printf("Netmask: %s\n", wifi.get_netmask()); |
mbed_official | 35:052c1ba06ce7 | 240 | printf("Gateway: %s\n", wifi.get_gateway()); |
mbed_official | 35:052c1ba06ce7 | 241 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
mbed_official | 0:857719181846 | 242 | |
mbed_official | 0:857719181846 | 243 | http_demo(&wifi); |
mbed_official | 0:857719181846 | 244 | |
cornetlin | 59:9bbcc1b368ba | 245 | //wifi.disconnect(); |
cornetlin | 59:9bbcc1b368ba | 246 | |
cornetlin | 59:9bbcc1b368ba | 247 | printf("\Wifi Example Done,MQTT Example Start\n"); |
cornetlin | 59:9bbcc1b368ba | 248 | |
cornetlin | 59:9bbcc1b368ba | 249 | // MQTT Example Start |
cornetlin | 59:9bbcc1b368ba | 250 | float version = 0.6; |
peng103617 | 60:5e586131c009 | 251 | char* topic = "test"; |
cornetlin | 59:9bbcc1b368ba | 252 | |
peng103617 | 60:5e586131c009 | 253 | //logMessage("HelloMQTT: version is %.2f\r\n", version); |
cornetlin | 59:9bbcc1b368ba | 254 | |
cornetlin | 59:9bbcc1b368ba | 255 | NetworkInterface* network = &wifi; |
cornetlin | 59:9bbcc1b368ba | 256 | if (!network) { |
cornetlin | 59:9bbcc1b368ba | 257 | return -1; |
cornetlin | 59:9bbcc1b368ba | 258 | } |
cornetlin | 59:9bbcc1b368ba | 259 | |
cornetlin | 59:9bbcc1b368ba | 260 | MQTTNetwork mqttNetwork(network); |
cornetlin | 59:9bbcc1b368ba | 261 | |
cornetlin | 59:9bbcc1b368ba | 262 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
cornetlin | 59:9bbcc1b368ba | 263 | |
peng103617 | 60:5e586131c009 | 264 | const char* hostname = "172.20.10.3"; |
cornetlin | 59:9bbcc1b368ba | 265 | int port = 1883; |
cornetlin | 59:9bbcc1b368ba | 266 | logMessage("Connecting to %s:%d\r\n", hostname, port); |
peng103617 | 60:5e586131c009 | 267 | |
peng103617 | 60:5e586131c009 | 268 | |
peng103617 | 60:5e586131c009 | 269 | |
cornetlin | 59:9bbcc1b368ba | 270 | int rc = mqttNetwork.connect(hostname, port); |
peng103617 | 60:5e586131c009 | 271 | |
cornetlin | 59:9bbcc1b368ba | 272 | if (rc != 0) |
cornetlin | 59:9bbcc1b368ba | 273 | logMessage("rc from TCP connect is %d\r\n", rc); |
cornetlin | 59:9bbcc1b368ba | 274 | |
cornetlin | 59:9bbcc1b368ba | 275 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
cornetlin | 59:9bbcc1b368ba | 276 | data.MQTTVersion = 3; |
cornetlin | 59:9bbcc1b368ba | 277 | data.clientID.cstring = "mbed-sample"; |
cornetlin | 59:9bbcc1b368ba | 278 | data.username.cstring = "testuser"; |
cornetlin | 59:9bbcc1b368ba | 279 | data.password.cstring = "testpassword"; |
cornetlin | 59:9bbcc1b368ba | 280 | if ((rc = client.connect(data)) != 0) |
cornetlin | 59:9bbcc1b368ba | 281 | logMessage("rc from MQTT connect is %d\r\n", rc); |
cornetlin | 59:9bbcc1b368ba | 282 | |
cornetlin | 59:9bbcc1b368ba | 283 | if ((rc = client.subscribe(topic, MQTT::QOS2, messageArrived)) != 0) |
cornetlin | 59:9bbcc1b368ba | 284 | logMessage("rc from MQTT subscribe is %d\r\n", rc); |
cornetlin | 59:9bbcc1b368ba | 285 | |
peng103617 | 60:5e586131c009 | 286 | |
peng103617 | 60:5e586131c009 | 287 | |
peng103617 | 60:5e586131c009 | 288 | |
peng103617 | 60:5e586131c009 | 289 | |
peng103617 | 60:5e586131c009 | 290 | |
peng103617 | 60:5e586131c009 | 291 | while(1) |
peng103617 | 60:5e586131c009 | 292 | { |
peng103617 | 60:5e586131c009 | 293 | |
cornetlin | 59:9bbcc1b368ba | 294 | MQTT::Message message; |
cornetlin | 59:9bbcc1b368ba | 295 | // QoS 0 |
peng103617 | 60:5e586131c009 | 296 | |
peng103617 | 60:5e586131c009 | 297 | |
peng103617 | 60:5e586131c009 | 298 | char buf[100]; |
peng103617 | 60:5e586131c009 | 299 | printf("PM2.5 = %3.2f\r\n",SDS018 ()); |
peng103617 | 60:5e586131c009 | 300 | sprintf(buf, "PM2.5 = %3.2f\r\n",SDS018 ()); |
peng103617 | 60:5e586131c009 | 301 | |
peng103617 | 60:5e586131c009 | 302 | |
peng103617 | 60:5e586131c009 | 303 | message.qos = MQTT::QOS1; |
cornetlin | 59:9bbcc1b368ba | 304 | message.retained = false; |
cornetlin | 59:9bbcc1b368ba | 305 | message.dup = false; |
cornetlin | 59:9bbcc1b368ba | 306 | message.payload = (void*)buf; |
cornetlin | 59:9bbcc1b368ba | 307 | message.payloadlen = strlen(buf)+1; |
cornetlin | 59:9bbcc1b368ba | 308 | rc = client.publish(topic, message); |
peng103617 | 60:5e586131c009 | 309 | client.yield(100); |
peng103617 | 60:5e586131c009 | 310 | |
cornetlin | 59:9bbcc1b368ba | 311 | |
peng103617 | 60:5e586131c009 | 312 | } |
peng103617 | 60:5e586131c009 | 313 | |
peng103617 | 60:5e586131c009 | 314 | |
peng103617 | 60:5e586131c009 | 315 | |
mbed_official | 0:857719181846 | 316 | } |