mqtt_lora_thingsboard

Dependencies:   HTS221 VL53L0X BSP_B-L475E-IOT01 MQTT

Files at this revision

API Documentation at this revision

Comitter:
cornetlin
Date:
Tue Dec 10 07:13:20 2019 +0000
Parent:
0:8d0ccf7acecd
Child:
2:0fbb5fc452de
Child:
3:a21eb3abb874
Commit message:
L475VG-IOT01A+WIFI+MQTT

Changed in this revision

BSP_B-L475E-IOT01.lib Show annotated file Show diff for this revision Revisions of this file
MQTT.lib Show annotated file Show diff for this revision Revisions of this file
MQTTNetwork.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os-treasuredata-rest.lib Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/BSP_B-L475E-IOT01.lib	Wed Feb 27 18:17:05 2019 +0900
+++ b/BSP_B-L475E-IOT01.lib	Tue Dec 10 07:13:20 2019 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/teams/ST/code/BSP_B-L475E-IOT01/#0c70bc6d2dc0
+https://os.mbed.com/teams/ST/code/BSP_B-L475E-IOT01/#bfe8272ced90
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Tue Dec 10 07:13:20 2019 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/teams/mqtt/code/MQTT/#9cff7b6bbd01
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTTNetwork.h	Tue Dec 10 07:13:20 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_
--- a/main.cpp	Wed Feb 27 18:17:05 2019 +0900
+++ b/main.cpp	Tue Dec 10 07:13:20 2019 +0000
@@ -1,41 +1,31 @@
-/* WiFi Example
- * Copyright (c) 2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+/* WiFi+MQTT Example
+  */
 
 #include "mbed.h"
 #include "TCPSocket.h"
 #include "wifi-ism43362/ISM43362Interface.h"
-#include "treasure-data-rest.h"
-// Sensors drivers present in the BSP library
-#include "stm32l475e_iot01_tsensor.h"
-#include "stm32l475e_iot01_hsensor.h"
-#include "stm32l475e_iot01_psensor.h"
-#include "stm32l475e_iot01_magneto.h"
-#include "stm32l475e_iot01_gyro.h"
-#include "stm32l475e_iot01_accelero.h"
-
-#define BUFF_SIZE   200
+#define logMessage printf
+#define MQTTCLIENT_QOS2 1
+#include "MQTTNetwork.h"
+#include "MQTTmbed.h"
+#include "MQTTClient.h"
 
 ISM43362Interface net;
 // WiFiInterface *wifi;
 
+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){
 
     int count = 0;
 
-    printf("\r\nTreasure Data REST API Demo\n");
+    printf("\r\nWiFi+MQTT Example Demo\n");
 
     // Connect to Wifi
     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
@@ -52,160 +42,68 @@
     printf("Gateway: %s\n", net.get_gateway());
     printf("RSSI: %d\n\n", net.get_rssi());
 
-    // Create Treasure data objects (Network, Database, Table, APIKey)
-    TreasureData_RESTAPI* heap  = new TreasureData_RESTAPI(&net,"iot_test","heap_info", MBED_CONF_APP_API_KEY);
-    TreasureData_RESTAPI* cpu   = new TreasureData_RESTAPI(&net,"iot_test","cpu_info",  MBED_CONF_APP_API_KEY);
-    TreasureData_RESTAPI* stack = new TreasureData_RESTAPI(&net,"iot_test","stack_info",MBED_CONF_APP_API_KEY);
-    TreasureData_RESTAPI* sys   = new TreasureData_RESTAPI(&net,"iot_test","sys_info",  MBED_CONF_APP_API_KEY);
-    TreasureData_RESTAPI* sensor   = new TreasureData_RESTAPI(&net,"iot_test","sensor_info",  MBED_CONF_APP_API_KEY);
+    printf("\Wifi Example Done,MQTT Example Start\n");
+    
+    // MQTT Example Start
+    float version = 0.6;
+    char* publishtopic = "publishtest";
+    char* subscribetopic = "subscribtest";
+
+    logMessage("HelloMQTT: version is %.2f\r\n", version);
 
+    NetworkInterface* network = &net;
+    if (!network) {
+        return -1;
+    }
 
-    // Device Information Objects
-    mbed_stats_cpu_t    cpuinfo;
-    mbed_stats_heap_t   heapinfo;
-    mbed_stats_stack_t  stackinfo;
-    mbed_stats_sys_t    sysinfo;
+    MQTTNetwork mqttNetwork(network);
+
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
 
-    // Buffers to create strings in
-    char cpu_buff  [BUFF_SIZE] = {0};
-    char heap_buff [BUFF_SIZE] = {0};
-    char stack_buff[BUFF_SIZE] = {0};
-    char sys_buff  [BUFF_SIZE] = {0};
-    char sensor_buff  [BUFF_SIZE] = {0};
-    
-    // Initiate sensor variables
-    float sensor_value_temp = 0;
-    float sensor_value_humid = 0;
-    float sensor_value_pressure = 0;
+    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);
 
-    int16_t pDataXYZ[3] = {0};
-    float pGyroDataXYZ[3] = {0};
- 
-    BSP_TSENSOR_Init();
-    BSP_HSENSOR_Init();
-    BSP_PSENSOR_Init();
-         
-    BSP_MAGNETO_Init();
-    BSP_GYRO_Init();
-    BSP_ACCELERO_Init();
+    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);
 
-    int x = 0;
+    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){
-        {
-            
-            
-            // Collect local data
-            mbed_stats_cpu_get(  &cpuinfo);
-
-            // Construct strings to send
-            x = sprintf(cpu_buff,"{\"uptime\":\"%d\",\"idle_time\":\"%d\",\"sleep_time\":\"%d\",\"deep_sleep_time\":\"%d\"}",
-                                    cpuinfo.uptime,
-                                    cpuinfo.idle_time,
-                                    cpuinfo.sleep_time,
-                                    cpuinfo.deep_sleep_time);
-            cpu_buff[x]=0; // null terminate the string
-
-            // Send data to Treasure data
-            printf("\r\n Sending CPU Data: '%s'\r\n",cpu_buff);
-            cpu->sendData(cpu_buff,strlen(cpu_buff));
-        }
-        {
-            // Collect local data
-            mbed_stats_heap_get(  &heapinfo);
-
-            // Construct strings to send
-            x=sprintf(heap_buff,"{\"current_size\":\"%d\",\"max_size\":\"%d\",\"total_size\":\"%d\",\"reserved_size\":\"%d\",\"alloc_cnt\":\"%d\",\"alloc_fail_cnt\":\"%d\"}",
-                                heapinfo.current_size,
-                                heapinfo.max_size,
-                                heapinfo.total_size,
-                                heapinfo.reserved_size,
-                                heapinfo.alloc_cnt,
-                                heapinfo.alloc_fail_cnt);
-            heap_buff[x]=0; // null terminate the string
-
-            // Send data to Treasure data
-            printf("\r\n Sending Heap Data: '%s'\r\n",heap_buff);
-            heap->sendData(heap_buff,strlen(heap_buff));
-        }
-        {
-            // Collect local data
-            mbed_stats_stack_get(  &stackinfo);
-
-            // Construct strings to send
-            x=sprintf(stack_buff,"{\"thread_id\":\"%d\",\"max_size\":\"%d\",\"reserved_size\":\"%d\",\"stack_cnt\":\"%d\"}",
-                                stackinfo.thread_id,
-                                stackinfo.max_size,
-                                stackinfo.reserved_size,
-                                stackinfo.stack_cnt);
-            stack_buff[x]=0; // null terminate the string
+        
+        printf("\test\n");
+         MQTT::Message message;
 
-            // Send data to Treasure data
-            printf("\r\n Sending Stack Data: '%s'\r\n",stack_buff);
-            stack->sendData(stack_buff,strlen(stack_buff));
-        }
-        {
-            // Collect local data
-            mbed_stats_sys_get(  &sysinfo);
-
-            // Construct strings to send
-            x=sprintf(sys_buff,"{\"os_version\":\"%d\",\"cpu_id\":\"%d\",\"compiler_id\":\"%d\",\"compiler_version\":\"%d\"}",
-                                sysinfo.os_version,
-                                sysinfo.cpu_id,
-                                sysinfo.compiler_id,
-                                sysinfo.compiler_version);
-            sys_buff[x]=0; // null terminate the string
+    // 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(publishtopic, message);
+    client.yield(100);
 
-            // Send data to Treasure data
-            printf("\r\n Sending System Data: '%s'\r\n",sys_buff);
-            sys->sendData(sys_buff,strlen(sys_buff));
-        }
-        {
-            sensor_value_temp = BSP_TSENSOR_ReadTemp();
-            printf("\nTEMPERATURE = %.2f degC\n", sensor_value_temp);
-     
-            sensor_value_humid = BSP_HSENSOR_ReadHumidity();
-            printf("HUMIDITY    = %.2f %%\n", sensor_value_humid);
-     
-            sensor_value_pressure = BSP_PSENSOR_ReadPressure();
-            printf("PRESSURE is = %.2f mBar\n", sensor_value_pressure);
-            
-//            BSP_MAGNETO_GetXYZ(pDataXYZ);
-//            printf("\nMAGNETO_X = %d\n", pDataXYZ[0]);
-//            printf("MAGNETO_Y = %d\n", pDataXYZ[1]);
-//            printf("MAGNETO_Z = %d\n", pDataXYZ[2]);
-//     
-//            BSP_GYRO_GetXYZ(pGyroDataXYZ);
-//            printf("\nGYRO_X = %.2f\n", pGyroDataXYZ[0]);
-//            printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]);
-//            printf("GYRO_Z = %.2f\n", pGyroDataXYZ[2]);
-//     
-//            BSP_ACCELERO_AccGetXYZ(pDataXYZ);
-//            printf("\nACCELERO_X = %d\n", pDataXYZ[0]);
-//            printf("ACCELERO_Y = %d\n", pDataXYZ[1]);
-//            printf("ACCELERO_Z = %d\n", pDataXYZ[2]);
-            
-            // Construct strings to send
-            x=sprintf(sensor_buff,"{\"temperature\":\"%f\",\"humidity\":\"%f\",\"pressure\":\"%f\"}",
-                                sensor_value_temp,
-                                sensor_value_humid,
-                                sensor_value_pressure
-                                );
-            sensor_buff[x]=0; // null terminate the string
-     
-            // Send data to Treasure data
-            printf("\r\n Sending System Data: '%s'\r\n",sensor_buff);
-            sensor->sendData(sensor_buff,strlen(sensor_buff));
-            
-        }
-        wait(10);
+
+    //int x = 0;
+        wait(1);
 
     }
 
-    net.disconnect();
-
-    printf("\nDone, x=%d\n",x);
-
+   // net.disconnect();
 
 }
--- a/mbed-os-treasuredata-rest.lib	Wed Feb 27 18:17:05 2019 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://github.com/BlackstoneEngineering/mbed-os-treasuredata-rest/#5a43dff08d74c093268f2840b73fbd364abc981e
--- a/mbed_app.json	Wed Feb 27 18:17:05 2019 +0900
+++ b/mbed_app.json	Tue Dec 10 07:13:20 2019 +0000
@@ -7,11 +7,11 @@
         },
          "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"SSID\""
+            "value": "\"cornetlin\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"PASSWORD\""
+            "value": "\"tp67650421\""
         },
         "api-key":{
             "help":  "REST API Key for Treasure Data",