MQTT-STM32

Dependencies:   MbedJSONValue wifi-ism43362 LSM6DSL HTS221 MQTT

Files at this revision

API Documentation at this revision

Comitter:
jingege
Date:
Wed Sep 23 09:12:20 2020 +0000
Commit message:
stm32-MQTT

Changed in this revision

HTS221.lib Show annotated file Show diff for this revision Revisions of this file
LSM6DSL.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
MbedJSONValue.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-os.lib Show annotated file Show diff for this revision Revisions of this file
wifi-ism43362.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 775536f6b40b HTS221.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTS221.lib	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ST/code/HTS221/#ccf7f36492ae
diff -r 000000000000 -r 775536f6b40b LSM6DSL.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM6DSL.lib	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ST/code/LSM6DSL/#77ec4781b110
diff -r 000000000000 -r 775536f6b40b MQTT.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/mqtt/code/MQTT/#9cff7b6bbd01
diff -r 000000000000 -r 775536f6b40b MQTTNetwork.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTTNetwork.h	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,42 @@
+#ifndef _MQTTNETWORK_H_
+#define _MQTTNETWORK_H_
+
+#include "NetworkInterface.h"
+
+class MQTTNetwork {
+public:
+    MQTTNetwork(NetworkStack* 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) {
+        int ret = socket->open(network);
+        printf("ret ===%d\n\n\n",ret);
+        ret = socket->connect(hostname, port);
+        printf("ret ======== %d\n\n\n", ret);
+        return ret;
+    }
+
+    int disconnect() {
+        return socket->close();
+    }
+
+private:
+    NetworkStack* network;
+    TCPSocket* socket;
+};
+
+#endif // _MQTTNETWORK_H_
diff -r 000000000000 -r 775536f6b40b MbedJSONValue.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 000000000000 -r 775536f6b40b main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,167 @@
+#include "mbed.h"
+#include <string>
+#include "ISM43362Interface.h"
+#include "LSM6DSLSensor.h"
+#include "HTS221Sensor.h"
+#include "MQTTNetwork.h"
+#include "MQTTClient.h"
+#include "MQTTmbed.h"
+#include "MbedJSONValue.h"
+char* hostname = "192.168.22.165";
+int port = 1883;
+#define QOS      MQTT::QOS0
+DigitalOut led(LED3);
+int main()
+{   
+    Serial pc(SERIAL_TX, SERIAL_RX);
+    char exchangebuf[50];
+    ThisThread::sleep_for(10000);
+    printf("start!");
+    DevI2C I2C(PB_11, PB_10);
+    LSM6DSLSensor Lsm(&I2C, 0xD4);
+    HTS221Sensor Hts(&I2C, 0xBE);
+    void* init;
+    int ret;
+    ret = Hts.init(init);
+    if (0 == ret)
+    {
+        printf("init_hts succeed!!\n "); 
+    }
+    else
+    {
+        printf("init_hts failed!!\n");
+        return -1; 
+    }
+    ret = Hts.enable();
+    if (0 == ret)
+    {
+        printf("enable_hts succeed!!\n "); 
+    }
+    else
+    {
+        printf("enable_hts failed!!\n");
+        return -1; 
+    }
+    ret = Lsm.init(init);
+    if(0 == ret)
+    {
+        printf("init succeed!!\n"); 
+    }
+    else
+    {
+        printf("init failed!!\n"); 
+        return -1;
+    }
+    ret = Lsm.enable_x();
+     if(0 == ret)
+    {
+        printf("enable succeed!!\n"); 
+    }
+    else
+    {
+        printf("enable failed!!\n");
+        return -1; 
+    }
+    ret = Lsm.set_x_odr(400.0);
+     if(0 == ret)
+    {
+        printf("set_x_odr succeed!!\n"); 
+    }
+    else
+    {
+        printf("set_x_odr failed!!\n");
+        return -1; 
+    }   
+    ret = Lsm.set_x_fs(8.0);
+    if(0 == ret)
+    {
+        printf("set_x_fs succeed!!\n"); 
+    }
+    else
+    {
+        printf("set_x_fs failed!!\n");
+        return -1; 
+    }  
+    ISM43362Interface net(PC_12,PC_11,PC_10,PE_0,PE_8,PE_1,PB_13);
+    ret = net.connect("Google_2.4G","abcd1234",NSAPI_SECURITY_NONE);
+    if(0 == ret)
+    {
+           printf("connect wifi succeed ip = %s\n",net.get_ip_address());    
+    }
+    else
+    {
+        printf("connect wifi failed!!!");
+        return -1;
+    }
+    NetworkStack* network = &net;
+    MQTTNetwork mqttNetwork(network);
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+    ret = mqttNetwork.connect(hostname, port);
+    client.connect();
+    if (0 == ret)
+    {
+        printf("connect succeed!!\n");
+    }
+    else
+    {
+        printf("connect failed!!\n");
+    }  
+    MQTT::Message message;
+    message.qos = QOS;
+    message.retained = false;
+    message.dup = false;
+    string str;
+    while(1)
+    {       
+        memset(exchangebuf, 0, sizeof(exchangebuf));
+        float hum,temp;
+        ret = Hts.get_temperature(&temp);
+        if(ret != 0)
+        {
+            printf("get_temperature failed!\n"); 
+        }
+        MbedJSONValue value;
+        value["temp"] = (int)temp;
+        str = value.serialize();
+        message.payload = (void*)str.c_str();
+        message.payloadlen = str.size();
+        int rc = client.publish("temp", message);
+        printf("rc ==== %d",rc);
+        ret = Hts.get_humidity(&hum);
+        if(ret != 0)
+        {
+            printf("get_humidity failed!\n"); 
+        }
+        printf("hum = %.2f temp = %.2f\n", hum, temp); 
+        str.clear();
+        MbedJSONValue value_hum;
+        value_hum["hum"] = (int)hum;
+        str = value_hum.serialize();
+        message.payload = (void*)str.c_str();
+        message.payloadlen = str.size();
+        rc = client.publish("hum", message);
+        printf("rc ==== %d",rc);
+        str.clear();
+        int32_t buf[3];
+        ret = Lsm.get_x_axes(buf);
+        if(0 == ret)
+        {
+            printf("get_x_axes succeed!!\n"); 
+        }
+        else
+        {
+            printf("get_x_axes failed!!\n");
+            return -1; 
+        } 
+        printf("x = %d y = %d z = %d\n",buf[0]/1000,buf[1]/1000,buf[2]/1000);
+        MbedJSONValue value_xyz;
+        value_xyz["x"] = buf[0]/1000;
+        value_xyz["y"] = buf[1]/1000;
+        value_xyz["z"] = buf[2]/1000;
+        str = value_xyz.serialize();
+        message.payload = (void*)str.c_str();
+        message.payloadlen = str.size();
+        rc = client.publish("xyz", message);
+        ThisThread::sleep_for(10);
+    }  
+}
diff -r 000000000000 -r 775536f6b40b mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#b81aeff1a3e171c6421984faa2cc18d0e35746c0
diff -r 000000000000 -r 775536f6b40b wifi-ism43362.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wifi-ism43362.lib	Wed Sep 23 09:12:20 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/Farnell24-IOT-Team/code/wifi-ism43362/#766454e296c3