2020_09_25_mqtt

Dependencies:   HTS221 VL53L0X BSP_B-L475E-IOT01 MQTT

Files at this revision

API Documentation at this revision

Comitter:
PINGTING
Date:
Tue Feb 23 11:14:09 2021 +0000
Parent:
2:a21eb3abb874
Commit message:
2021_02_23

Changed in this revision

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
diff -r a21eb3abb874 -r 1da698331ec8 main.cpp
--- a/main.cpp	Sat Jan 04 16:47:24 2020 +0000
+++ b/main.cpp	Tue Feb 23 11:14:09 2021 +0000
@@ -1,5 +1,4 @@
-/* WiFi+MQTT Example
-  */
+/* WiFi+MQTT Example*/
 
 #include "mbed.h"
 #include "TCPSocket.h"
@@ -10,12 +9,12 @@
 #include "MQTTmbed.h"
 #include "MQTTClient.h"
 #include "HTS221Sensor.h"
-//#define MQTT_HOST               "demo.thingsboard.io"
-#define MQTT_HOST               "192.168.43.14"
+//#define MQTT_HOST              "demo.thingsboard.io"
+#define MQTT_HOST               "172.20.10.6"
 #define MQTT_PORT               1883
-#define MQTT_TOPIC              "Pubtest"
+#define MQTT_TOPIC              "Voltage"
 #include <string>
-#include "VL53L0X.h"
+//#include "VL53L0X.h"
 ISM43362Interface net;
 // WiFiInterface *wifi;
 Serial pc(USBTX,USBRX);
@@ -28,15 +27,15 @@
 }
 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
+//static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
+Serial uart(PA_0, PA_1);//TX,RX
 
 int idx=0;
 std::string inputdata;
 char ch1;
 int main(void){
     uart.baud(115200);
-    range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
+    //range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
     int count = 0;
 
     printf("\r\nWiFi+MQTT Example Demo\n");
@@ -59,12 +58,12 @@
     printf("\Wifi Example Done,MQTT Example Start\n");
     
     // MQTT Example Start
-    float version = 0.6;
+    //float version = 0.6;
     char* publishtopic = "publishtest";
     char* subscribetopic = "subscribtest";
 
-    logMessage("HelloMQTT: version is %.2f\r\n", version);
-    char assess_token[] = "uNYseQgqntIrL7q5F2tL";
+    //logMessage("HelloMQTT: version is %.2f\r\n", version);
+    //char assess_token[] = "uNYseQgqntIrL7q5F2tL";
     NetworkInterface* network = &net;
     if (!network) {
         return -1;
@@ -84,7 +83,7 @@
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
     //data.MQTTVersion = 3;
     data.clientID.cstring = "mbed-sample";
-    data.username.cstring = assess_token;
+    //data.username.cstring = assess_token;
     //data.password.cstring = "";
     if ((rc = client.connect(data)) != 0)
         logMessage("rc from MQTT connect is %d\r\n", rc);
@@ -98,61 +97,34 @@
     
     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}";
+// Initialize sensors --------------------------------------------------
+    
+    std::string inputdata;
   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///////
-
-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];
+      char ch;
+      while (uart.readable()) {
+     ch = uart.getc();
+      if (ch == 's') {
+      inputdata.clear();
+      }
+     inputdata += ch;
+     if (ch == '#') {
+    inputdata.erase(0,1);
+    inputdata.erase(inputdata.length()-1,1);
+    pc.printf("%s\r\n",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);
+            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);
 
-        if (client.publish(MQTT_TOPIC, payload, n) < 0) {
-            printf("failed to publish MQTT message");
+            if(client.publish(MQTT_TOPIC, payload, n) < 0) {
+               printf("failed to publish MQTT message");
+            }
         }
-        }
-}
-         
+   }        
 }
         
     
diff -r a21eb3abb874 -r 1da698331ec8 mbed_app.json
--- a/mbed_app.json	Sat Jan 04 16:47:24 2020 +0000
+++ b/mbed_app.json	Tue Feb 23 11:14:09 2021 +0000
@@ -7,11 +7,11 @@
         },
          "wifi-ssid": {
             "help": "WiFi SSID",
-            "value": "\"note9\""
+            "value": "\"Pananpi\""
         },
         "wifi-password": {
             "help": "WiFi Password",
-            "value": "\"12345678\""
+            "value": "\"iloveyourmother\""
         },
         "api-key":{
             "help":  "REST API Key for Treasure Data",