mqtt_lora_thingsboard
Dependencies: HTS221 VL53L0X BSP_B-L475E-IOT01 MQTT
Diff: main.cpp
- Revision:
- 2:0fbb5fc452de
- Parent:
- 1:5a9fdc848911
--- a/main.cpp Tue Dec 10 07:13:20 2019 +0000 +++ b/main.cpp Fri Sep 11 03:46:17 2020 +0000 @@ -9,6 +9,167 @@ #include "MQTTNetwork.h" #include "MQTTmbed.h" #include "MQTTClient.h" +//#include <string> +//Serial pc(USBTX, USBRX); +//Serial uart(PA_0, PA_1); +//string inputdata; +ISM43362Interface net; + + +// WiFiInterface *wifi; +/*void newData() +{ + //buffer variables + char ch; + //if data is ready in the buffer + while (uart.readable()) { + //read 1 character + //pc.printf("datacomming"); + ch = uart.getc(); + //if character is ‘s’ than it is the start of a sentence + if (ch == 'p') { + //so the pointer should be set to the first position + inputdata.clear(); + } + //write buffer character to big buffer string + inputdata += ch; + //if the character is # than the end of the sentence is reached and some stuff has to be done + if (ch == '#') { + //remove start and stop characters + inputdata.erase(0,1); + inputdata.erase(inputdata.length()-1,1); + printf("input data =%s\n\r",inputdata); + } + } +}*/ +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; +} + +int main(void){ + //pc.baud(19200); + // uart.attach(&newData); + int count = 0; + + printf("\r\nWiFi+MQTT Example Demo\n"); + + // Connect to Wifi + printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); + int ret = net.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); + if (ret != 0) { + printf("\nConnection error: %d\n", ret); + return -1; + } + + printf("Success\n\n"); + printf("MAC: %s\n", net.get_mac_address()); + printf("IP: %s\n", net.get_ip_address()); + printf("Netmask: %s\n", net.get_netmask()); + printf("Gateway: %s\n", net.get_gateway()); + printf("RSSI: %d\n\n", net.get_rssi()); + + printf("\Wifi Example Done,MQTT Example Start\n"); + + // MQTT Example Start + float version = 0.6; + char* publishtopic = "v1/devices/me/telemetry"; + char* subscribetopic = "subscribtest"; + char* publishtopic1 = "publisher"; + logMessage("HelloMQTT: version is %.2f\r\n", version); + + NetworkInterface* network = &net; + if (!network) { + return -1; + } + + MQTTNetwork mqttNetwork(network); + + MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); + + //const char* hostname = "192.168.0.157"; + const char* hostname = "demo.thingsboard.io"; + 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); + char assess_token[] = "Cb5N7YKqwf6Uf76GPI2p"; + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.username.cstring = assess_token; + 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(publishtopic, MQTT::QOS2, messageArrived)) != 0) + logMessage("rc from MQTT subscribe is %d\r\n", rc); + if ((rc = client.subscribe(subscribetopic, MQTT::QOS2, messageArrived)) != 0) + logMessage("rc from MQTT subscribe is %d\r\n", rc); + + + // Get device health data, send to Treasure Data every 10 seconds + while(1){ + + printf("\test\n"); + // MQTT::Message message; + // MQTT::Message message1; + // QoS 0 + char buf[100]; + // char buf1[30]="{\'test\':\'hello\'}"; + + int n = snprintf(buf, sizeof(buf),"{\"temperature\": %f}",version); + void *payload = reinterpret_cast<void*>(buf); + size_t payload_len = n; + //sprintf(buf, "{\"temperature\":%f}", version); + /* message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + message.payload = (void*)buf; + message.payloadlen = strlen(buf)+1; + message1.qos = MQTT::QOS0; + message1.retained = false; + message1.dup = false; + message1.payload = (void*)buf1; + message1.payloadlen = strlen(buf1)+1;*/ + + //message.payload = (void*)buf1; + rc = client.publish(publishtopic, payload,n); + //rc = client.publish(publishtopic1, message1); + //client.yield(5); + //uart.printf("s12345#\r\n"); + + //int x = 0; + wait(1); + + } + + // net.disconnect(); + +} + + + + + + + +/* WiFi+MQTT Example + */ + +/*#include "mbed.h" +#include "TCPSocket.h" +#include "wifi-ism43362/ISM43362Interface.h" +#define logMessage printf +#define MQTTCLIENT_QOS2 1 +#include "MQTTNetwork.h" +#include "MQTTmbed.h" +#include "MQTTClient.h" ISM43362Interface net; // WiFiInterface *wifi; @@ -46,9 +207,9 @@ // MQTT Example Start float version = 0.6; - char* publishtopic = "publishtest"; + char* publishtopic = "v1/devices/me/telemetry"; char* subscribetopic = "subscribtest"; - + char* publishtopic1 = "publisher"; logMessage("HelloMQTT: version is %.2f\r\n", version); NetworkInterface* network = &net; @@ -60,17 +221,19 @@ MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork); - const char* hostname = "192.168.0.120"; + //const char* hostname = "192.168.0.157"; + const char* hostname = "demo.thingsboard.io"; 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); - + char assess_token[] = "Cb5N7YKqwf6Uf76GPI2p"; MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.username.cstring = assess_token; data.MQTTVersion = 3; data.clientID.cstring = "mbed-sample"; - data.username.cstring = "testuser"; + //data.username.cstring = "testuser"; data.password.cstring = "testpassword"; if ((rc = client.connect(data)) != 0) logMessage("rc from MQTT connect is %d\r\n", rc); @@ -85,18 +248,31 @@ while(1){ printf("\test\n"); - MQTT::Message message; - + // MQTT::Message message; + // MQTT::Message message1; // QoS 0 char buf[100]; - sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version); - message.qos = MQTT::QOS0; + // char buf1[30]="{\'test\':\'hello\'}"; + + int n = snprintf(buf, sizeof(buf),"{\"temperature\": %f}",version); + void *payload = reinterpret_cast<void*>(buf); + size_t payload_len = n; + //sprintf(buf, "{\"temperature\":%f}", version); + /* message.qos = MQTT::QOS0; message.retained = false; message.dup = false; message.payload = (void*)buf; message.payloadlen = strlen(buf)+1; - rc = client.publish(publishtopic, message); - client.yield(100); + message1.qos = MQTT::QOS0; + message1.retained = false; + message1.dup = false; + message1.payload = (void*)buf1; + message1.payloadlen = strlen(buf1)+1;*/ + + //message.payload = (void*)buf1; +/* rc = client.publish(publishtopic, payload,n); + //rc = client.publish(publishtopic1, message1); + //client.yield(5); //int x = 0; @@ -106,4 +282,4 @@ // net.disconnect(); -} +}*/