mqtt_lora_thingsboard

Dependencies:   HTS221 VL53L0X BSP_B-L475E-IOT01 MQTT

Files at this revision

API Documentation at this revision

Comitter:
cornetlin
Date:
Sat Jan 04 16:47:24 2020 +0000
Parent:
1:5a9fdc848911
Commit message:
WIFI+MQTT+LORA

Changed in this revision

HTS221.lib Show annotated file Show diff for this revision Revisions of this file
VL53L0X.lib 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_app.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221.lib	Sat Jan 04 16:47:24 2020 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/HTS221/#312ee2694a77
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VL53L0X.lib	Sat Jan 04 16:47:24 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ST/code/VL53L0X/#8ac15bf6d635
--- a/main.cpp	Tue Dec 10 07:13:20 2019 +0000
+++ b/main.cpp	Sat Jan 04 16:47:24 2020 +0000
@@ -9,10 +9,16 @@
 #include "MQTTNetwork.h"
 #include "MQTTmbed.h"
 #include "MQTTClient.h"
-
+#include "HTS221Sensor.h"
+//#define MQTT_HOST               "demo.thingsboard.io"
+#define MQTT_HOST               "192.168.43.14"
+#define MQTT_PORT               1883
+#define MQTT_TOPIC              "Pubtest"
+#include <string>
+#include "VL53L0X.h"
 ISM43362Interface net;
 // WiFiInterface *wifi;
-
+Serial pc(USBTX,USBRX);
 void messageArrived(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
@@ -20,9 +26,17 @@
     logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
     //++arrivedcount;
 }
+static DevI2C devI2c(PB_11,PB_10);
+static DigitalOut shutdown_pin(PC_6);
+static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
+Serial uart(PC_4, PC_5);//TX,RX
 
+int idx=0;
+std::string inputdata;
+char ch1;
 int main(void){
-
+    uart.baud(115200);
+    range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
     int count = 0;
 
     printf("\r\nWiFi+MQTT Example Demo\n");
@@ -50,7 +64,7 @@
     char* subscribetopic = "subscribtest";
 
     logMessage("HelloMQTT: version is %.2f\r\n", version);
-
+    char assess_token[] = "uNYseQgqntIrL7q5F2tL";
     NetworkInterface* network = &net;
     if (!network) {
         return -1;
@@ -60,18 +74,18 @@
 
     MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
 
-    const char* hostname = "192.168.0.120";
-    int port = 1883;
+    const char* hostname = MQTT_HOST;
+    int port = MQTT_PORT;
     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);
 
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    data.MQTTVersion = 3;
+    //data.MQTTVersion = 3;
     data.clientID.cstring = "mbed-sample";
-    data.username.cstring = "testuser";
-    data.password.cstring = "testpassword";
+    data.username.cstring = assess_token;
+    //data.password.cstring = "";
     if ((rc = client.connect(data)) != 0)
         logMessage("rc from MQTT connect is %d\r\n", rc);
 
@@ -81,29 +95,68 @@
         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;
+    
+    printf("successfully connect!\n\n");
+    
+    // Initialize sensors --------------------------------------------------
+
+    //uint8_t id;
+   //DevI2C i2c_2(PB_11, PB_10);
+    //HTS221Sensor hum_temp(&i2c_2);
+
+   // hum_temp.init(NULL);
+    //hum_temp.enable();
+    //hum_temp.read_id(&id);
+   
+ //printf("HTS221  humidity & temperature sensor = 0x%X\r\n", id);
+ //std::string inputdata;
+ //inputdata="{\"PM2.5\":10,\"PM10\":20,\"temperature\":20,\"humidity\":45}";
+  while (1) {
+     
+     //buffer variables
+char ch;
+//if data is ready in the buffer
+while (uart.readable()) {
+//read 1 character
+
+ch = uart.getc();
+//pc.printf("datacomming:%c",ch);
+/////Pm25///////
 
-    // 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);
+if (ch == 's') {
+//so the pointer should be set to the first position
+inputdata.clear();
+//pc.printf("start string");
+}
+//write buffer character to big buffer string
+inputdata += ch;
+//pc.printf("input string:%s",inputdata);
+//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);
+pc.printf("%s",inputdata);
+char msg[inputdata.size()+1];
+            inputdata.copy(msg,inputdata.size()+1);
+            msg[inputdata.size()]='\0';
+            int n=strlen(msg); 
+        void *payload = reinterpret_cast<void*>(msg);
+        size_t payload_len = n;
 
+        printf("publish to: %s %d %s\r\n", MQTT_HOST, MQTT_PORT, MQTT_TOPIC);
 
-    //int x = 0;
-        wait(1);
-
-    }
+        if (client.publish(MQTT_TOPIC, payload, n) < 0) {
+            printf("failed to publish MQTT message");
+        }
+        }
+}
+         
+}
+        
+    
 
    // net.disconnect();
 
-}
+}
\ No newline at end of file
--- a/mbed_app.json	Tue Dec 10 07:13:20 2019 +0000
+++ b/mbed_app.json	Sat Jan 04 16:47:24 2020 +0000
@@ -7,11 +7,11 @@
         },
          "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"cornetlin\""
+            "value": "\"note9\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"tp67650421\""
+            "value": "\"12345678\""
         },
         "api-key":{
             "help":  "REST API Key for Treasure Data",