mqtt_lora_thingsboard
Dependencies: HTS221 VL53L0X BSP_B-L475E-IOT01 MQTT
main.cpp@1:5a9fdc848911, 2019-12-10 (annotated)
- Committer:
- cornetlin
- Date:
- Tue Dec 10 07:13:20 2019 +0000
- Revision:
- 1:5a9fdc848911
- Parent:
- 0:8d0ccf7acecd
- Child:
- 2:0fbb5fc452de
- Child:
- 3:a21eb3abb874
L475VG-IOT01A+WIFI+MQTT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cornetlin | 1:5a9fdc848911 | 1 | /* WiFi+MQTT Example |
cornetlin | 1:5a9fdc848911 | 2 | */ |
JunkoNakajima |
0:8d0ccf7acecd | 3 | |
JunkoNakajima |
0:8d0ccf7acecd | 4 | #include "mbed.h" |
JunkoNakajima |
0:8d0ccf7acecd | 5 | #include "TCPSocket.h" |
JunkoNakajima |
0:8d0ccf7acecd | 6 | #include "wifi-ism43362/ISM43362Interface.h" |
cornetlin | 1:5a9fdc848911 | 7 | #define logMessage printf |
cornetlin | 1:5a9fdc848911 | 8 | #define MQTTCLIENT_QOS2 1 |
cornetlin | 1:5a9fdc848911 | 9 | #include "MQTTNetwork.h" |
cornetlin | 1:5a9fdc848911 | 10 | #include "MQTTmbed.h" |
cornetlin | 1:5a9fdc848911 | 11 | #include "MQTTClient.h" |
JunkoNakajima |
0:8d0ccf7acecd | 12 | |
JunkoNakajima |
0:8d0ccf7acecd | 13 | ISM43362Interface net; |
JunkoNakajima |
0:8d0ccf7acecd | 14 | // WiFiInterface *wifi; |
JunkoNakajima |
0:8d0ccf7acecd | 15 | |
cornetlin | 1:5a9fdc848911 | 16 | void messageArrived(MQTT::MessageData& md) |
cornetlin | 1:5a9fdc848911 | 17 | { |
cornetlin | 1:5a9fdc848911 | 18 | MQTT::Message &message = md.message; |
cornetlin | 1:5a9fdc848911 | 19 | logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id); |
cornetlin | 1:5a9fdc848911 | 20 | logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload); |
cornetlin | 1:5a9fdc848911 | 21 | //++arrivedcount; |
cornetlin | 1:5a9fdc848911 | 22 | } |
cornetlin | 1:5a9fdc848911 | 23 | |
JunkoNakajima |
0:8d0ccf7acecd | 24 | int main(void){ |
JunkoNakajima |
0:8d0ccf7acecd | 25 | |
JunkoNakajima |
0:8d0ccf7acecd | 26 | int count = 0; |
JunkoNakajima |
0:8d0ccf7acecd | 27 | |
cornetlin | 1:5a9fdc848911 | 28 | printf("\r\nWiFi+MQTT Example Demo\n"); |
JunkoNakajima |
0:8d0ccf7acecd | 29 | |
JunkoNakajima |
0:8d0ccf7acecd | 30 | // Connect to Wifi |
JunkoNakajima |
0:8d0ccf7acecd | 31 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
JunkoNakajima |
0:8d0ccf7acecd | 32 | int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
JunkoNakajima |
0:8d0ccf7acecd | 33 | if (ret != 0) { |
JunkoNakajima |
0:8d0ccf7acecd | 34 | printf("\nConnection error: %d\n", ret); |
JunkoNakajima |
0:8d0ccf7acecd | 35 | return -1; |
JunkoNakajima |
0:8d0ccf7acecd | 36 | } |
JunkoNakajima |
0:8d0ccf7acecd | 37 | |
JunkoNakajima |
0:8d0ccf7acecd | 38 | printf("Success\n\n"); |
JunkoNakajima |
0:8d0ccf7acecd | 39 | printf("MAC: %s\n", net.get_mac_address()); |
JunkoNakajima |
0:8d0ccf7acecd | 40 | printf("IP: %s\n", net.get_ip_address()); |
JunkoNakajima |
0:8d0ccf7acecd | 41 | printf("Netmask: %s\n", net.get_netmask()); |
JunkoNakajima |
0:8d0ccf7acecd | 42 | printf("Gateway: %s\n", net.get_gateway()); |
JunkoNakajima |
0:8d0ccf7acecd | 43 | printf("RSSI: %d\n\n", net.get_rssi()); |
JunkoNakajima |
0:8d0ccf7acecd | 44 | |
cornetlin | 1:5a9fdc848911 | 45 | printf("\Wifi Example Done,MQTT Example Start\n"); |
cornetlin | 1:5a9fdc848911 | 46 | |
cornetlin | 1:5a9fdc848911 | 47 | // MQTT Example Start |
cornetlin | 1:5a9fdc848911 | 48 | float version = 0.6; |
cornetlin | 1:5a9fdc848911 | 49 | char* publishtopic = "publishtest"; |
cornetlin | 1:5a9fdc848911 | 50 | char* subscribetopic = "subscribtest"; |
cornetlin | 1:5a9fdc848911 | 51 | |
cornetlin | 1:5a9fdc848911 | 52 | logMessage("HelloMQTT: version is %.2f\r\n", version); |
JunkoNakajima |
0:8d0ccf7acecd | 53 | |
cornetlin | 1:5a9fdc848911 | 54 | NetworkInterface* network = &net; |
cornetlin | 1:5a9fdc848911 | 55 | if (!network) { |
cornetlin | 1:5a9fdc848911 | 56 | return -1; |
cornetlin | 1:5a9fdc848911 | 57 | } |
JunkoNakajima |
0:8d0ccf7acecd | 58 | |
cornetlin | 1:5a9fdc848911 | 59 | MQTTNetwork mqttNetwork(network); |
cornetlin | 1:5a9fdc848911 | 60 | |
cornetlin | 1:5a9fdc848911 | 61 | MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); |
JunkoNakajima |
0:8d0ccf7acecd | 62 | |
cornetlin | 1:5a9fdc848911 | 63 | const char* hostname = "192.168.0.120"; |
cornetlin | 1:5a9fdc848911 | 64 | int port = 1883; |
cornetlin | 1:5a9fdc848911 | 65 | logMessage("Connecting to %s:%d\r\n", hostname, port); |
cornetlin | 1:5a9fdc848911 | 66 | int rc = mqttNetwork.connect(hostname, port); |
cornetlin | 1:5a9fdc848911 | 67 | if (rc != 0) |
cornetlin | 1:5a9fdc848911 | 68 | logMessage("rc from TCP connect is %d\r\n", rc); |
JunkoNakajima |
0:8d0ccf7acecd | 69 | |
cornetlin | 1:5a9fdc848911 | 70 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
cornetlin | 1:5a9fdc848911 | 71 | data.MQTTVersion = 3; |
cornetlin | 1:5a9fdc848911 | 72 | data.clientID.cstring = "mbed-sample"; |
cornetlin | 1:5a9fdc848911 | 73 | data.username.cstring = "testuser"; |
cornetlin | 1:5a9fdc848911 | 74 | data.password.cstring = "testpassword"; |
cornetlin | 1:5a9fdc848911 | 75 | if ((rc = client.connect(data)) != 0) |
cornetlin | 1:5a9fdc848911 | 76 | logMessage("rc from MQTT connect is %d\r\n", rc); |
JunkoNakajima |
0:8d0ccf7acecd | 77 | |
cornetlin | 1:5a9fdc848911 | 78 | if ((rc = client.subscribe(publishtopic, MQTT::QOS2, messageArrived)) != 0) |
cornetlin | 1:5a9fdc848911 | 79 | logMessage("rc from MQTT subscribe is %d\r\n", rc); |
cornetlin | 1:5a9fdc848911 | 80 | if ((rc = client.subscribe(subscribetopic, MQTT::QOS2, messageArrived)) != 0) |
cornetlin | 1:5a9fdc848911 | 81 | logMessage("rc from MQTT subscribe is %d\r\n", rc); |
cornetlin | 1:5a9fdc848911 | 82 | |
JunkoNakajima |
0:8d0ccf7acecd | 83 | |
JunkoNakajima |
0:8d0ccf7acecd | 84 | // Get device health data, send to Treasure Data every 10 seconds |
JunkoNakajima |
0:8d0ccf7acecd | 85 | while(1){ |
cornetlin | 1:5a9fdc848911 | 86 | |
cornetlin | 1:5a9fdc848911 | 87 | printf("\test\n"); |
cornetlin | 1:5a9fdc848911 | 88 | MQTT::Message message; |
JunkoNakajima |
0:8d0ccf7acecd | 89 | |
cornetlin | 1:5a9fdc848911 | 90 | // QoS 0 |
cornetlin | 1:5a9fdc848911 | 91 | char buf[100]; |
cornetlin | 1:5a9fdc848911 | 92 | sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version); |
cornetlin | 1:5a9fdc848911 | 93 | message.qos = MQTT::QOS0; |
cornetlin | 1:5a9fdc848911 | 94 | message.retained = false; |
cornetlin | 1:5a9fdc848911 | 95 | message.dup = false; |
cornetlin | 1:5a9fdc848911 | 96 | message.payload = (void*)buf; |
cornetlin | 1:5a9fdc848911 | 97 | message.payloadlen = strlen(buf)+1; |
cornetlin | 1:5a9fdc848911 | 98 | rc = client.publish(publishtopic, message); |
cornetlin | 1:5a9fdc848911 | 99 | client.yield(100); |
JunkoNakajima |
0:8d0ccf7acecd | 100 | |
cornetlin | 1:5a9fdc848911 | 101 | |
cornetlin | 1:5a9fdc848911 | 102 | //int x = 0; |
cornetlin | 1:5a9fdc848911 | 103 | wait(1); |
JunkoNakajima |
0:8d0ccf7acecd | 104 | |
JunkoNakajima |
0:8d0ccf7acecd | 105 | } |
JunkoNakajima |
0:8d0ccf7acecd | 106 | |
cornetlin | 1:5a9fdc848911 | 107 | // net.disconnect(); |
JunkoNakajima |
0:8d0ccf7acecd | 108 | |
JunkoNakajima |
0:8d0ccf7acecd | 109 | } |