send temp data to thingsboard through mqtt using lpc1768

Dependencies:   C12832 LM75B MQTT

Files at this revision

API Documentation at this revision

Comitter:
daklowprofile
Date:
Wed Jul 04 07:27:48 2018 +0000
Commit message:
initial working code

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
LM75B.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
easy-connect.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
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/C12832.lib	Wed Jul 04 07:27:48 2018 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/teams/components/code/C12832/#03069e3deaa4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Wed Jul 04 07:27:48 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/chris/code/LM75B/#6a70c9303bbe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Wed Jul 04 07:27:48 2018 +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	Wed Jul 04 07:27:48 2018 +0000
@@ -0,0 +1,38 @@
+#ifndef _MQTTNETWORK_H_
+#define _MQTTNETWORK_H_
+
+#include "NetworkInterface.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_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/easy-connect.lib	Wed Jul 04 07:27:48 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/easy-connect/#cb933fb19cda0a733a64d6b71d271fb6bdaf9e6d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jul 04 07:27:48 2018 +0000
@@ -0,0 +1,85 @@
+#define USE_LCD 0
+
+#if USE_LCD
+#include "C12832.h"
+
+// the actual pins are defined in mbed_app.json and can be overridden per target
+C12832 lcd(LCD_MOSI, LCD_SCK, LCD_MISO, LCD_A0, LCD_NCS);
+
+#define logMessage lcd.cls();lcd.printf
+
+#else
+
+#define logMessage printf
+
+#endif
+
+#define MQTTCLIENT_QOS2 1
+
+#include "easy-connect.h"
+#include "MQTTNetwork.h"
+#include "MQTTmbed.h"
+#include "MQTTClient.h"
+#include "LM75B.h"
+
+LM75B tmp(p28,p27);
+
+
+int arrivedcount = 0;
+
+
+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(int argc, char* argv[])
+{
+    float version = 0.6;
+    char* topic = "v1/devices/me/telemetry";
+
+    logMessage("HelloMQTT: version is %.2f\r\n", version);
+
+    NetworkInterface* network = easy_connect(true);
+    if (!network) {
+        return -1;
+    }
+
+    MQTTNetwork mqttNetwork(network);
+
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+
+    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[] = "aq8Xr1qkEzo1ANb4VQHa";
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+
+    data.username.cstring = assess_token;
+ 
+    if ((rc = client.connect(data)) != 0)
+        logMessage("rc from MQTT connect is %d\r\n", rc);
+
+    if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
+        logMessage("rc from MQTT subscribe is %d\r\n", rc);
+
+    MQTT::Message message;
+
+  
+    while(1){
+    char buf[256];    
+    int n = snprintf(buf, sizeof(buf), "{\"temperature\":%f, \"active\": false}", tmp.read());
+    message.payload = reinterpret_cast<void*>(buf);
+    message.payloadlen = n;
+    rc = client.publish(topic, message);
+    wait(5);
+    }
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Wed Jul 04 07:27:48 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#62f8b922b420626514fd4690107aff4188469833
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Wed Jul 04 07:27:48 2018 +0000
@@ -0,0 +1,69 @@
+{
+    "config": {
+        "network-interface":{
+            "help": "options are ETHERNET, WIFI_ESP8266, WIFI_ODIN, WIFI_RTW, MESH_LOWPAN_ND, MESH_THREAD, CELLULAR_ONBOARD",
+            "value": "ETHERNET"
+        },
+        "mesh_radio_type": {
+            "help": "options are ATMEL, MCR20",
+            "value": "ATMEL"
+        },
+        "esp8266-tx": {
+            "help": "Pin used as TX (connects to ESP8266 RX)",
+            "value": "D1"
+        },
+        "esp8266-rx": {
+            "help": "Pin used as RX (connects to ESP8266 TX)",
+            "value": "D0"
+        },
+        "esp8266-ssid": {
+            "value": "\"SSID\""
+        },
+        "esp8266-password": {
+            "value": "\"Password\""
+        },
+        "esp8266-debug": {
+            "value": true
+        },
+        "lcd-mosi": {
+            "value": "D11",
+            "macro_name": "LCD_MOSI"
+        },
+        "lcd-sck": {
+            "value": "D13",
+            "macro_name": "LCD_SCK"
+        },
+        "lcd-miso": {
+            "value": "D12",
+            "macro_name": "LCD_MISO"
+        },
+        "lcd-a0": {
+            "value": "D7",
+            "macro_name": "LCD_A0"
+        },
+        "lcd-ncs": {
+            "value": "D10",
+            "macro_name": "LCD_NCS"
+        }
+    },
+    "target_overrides": {
+        "*": {
+            "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"],
+            "mbed-mesh-api.6lowpan-nd-channel-page": 0,
+            "mbed-mesh-api.6lowpan-nd-channel": 12,
+            "mbed-trace.enable": 0
+        },
+        "HEXIWEAR": {
+            "esp8266-tx": "PTD3",
+            "esp8266-rx": "PTD2"
+        },
+        "NUCLEO_F401RE": {
+            "esp8266-tx": "D8",
+            "esp8266-rx": "D2"
+        },
+        "NUCLEO_F411RE": {
+            "esp8266-tx": "D8",
+            "esp8266-rx": "D2"
+        }
+    }
+}