STM32 + W5500 + MQTT

Dependencies:   MQTT WIZnet_Library mbed

Files at this revision

API Documentation at this revision

Comitter:
zhangyx
Date:
Fri Aug 25 15:08:59 2017 +0000
Child:
1:9689429a0a29
Commit message:
STM32 + W5500 + MQTT

Changed in this revision

MQTT.lib Show annotated file Show diff for this revision Revisions of this file
WIZnet_Library.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.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Fri Aug 25 15:08:59 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/zhangyx/code/MQTT/#6de46e8293d9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnet_Library.lib	Fri Aug 25 15:08:59 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/teams/WIZnet/code/WIZnet_Library/#cb8808b47e69
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Aug 25 15:08:59 2017 +0000
@@ -0,0 +1,85 @@
+#include "mbed.h"
+#include "WIZnetInterface.h"
+#include "MQTTSocket.h"
+#include "MQTTClient.h"
+
+DigitalIn BTN(PC_13);
+ //W5500接线 mosi,miso,sclk,cs,reset
+WIZnetInterface wiz(PA_7,PA_6,PA_5,PB_6,PC_7);
+//节点名称任取
+#define NODE_NAME "n_12345"
+ //接在同一子网下的设备MAC地址必须不同
+uint8_t mac_addr[6]={0x50,0x51,0x50,0x00,0x00,0x01};
+
+typedef MQTT::Client<MQTTSocket,Countdown> MClient;
+void meta_report(MClient& client, const char* ns, const char* type, 
+                    const char* payload = NULL, size_t payload_len = 0, 
+                    bool retain = false, MQTT::QoS qos = MQTT::QOS1){
+    char topic[64];
+    sprintf(topic, "/%s/" NODE_NAME "/%s", ns, type);
+    int ret = client.publish(topic, (void*)payload, payload_len, qos, retain);
+    printf("client.publish()=%d\r\n",ret);
+}
+void messageArrived(MQTT::MessageData& md)
+{
+    MQTT::Message &message = md.message;
+    char buf[64];
+    int value, len = sizeof(buf)-1;
+    if(message.payloadlen < len)
+        len = message.payloadlen;
+    memcpy(buf, message.payload, len);
+    buf[len] = '\0';
+    sscanf(buf, "%d", &value);
+    printf("received %d\r\n", value);
+}
+ 
+int main() {
+    int ret;
+    wiz.init(mac_addr);
+    wiz.connect();
+    printf("IP: %s\r\n", wiz.getIPAddress());
+    
+    MQTTSocket sock;
+    MClient client(sock);
+    
+    ret = sock.connect("tdxls-iot.xicp.net",1883);
+    if(ret != 0){
+        printf("failed to connect to TCP server\r\n");
+        return 1;
+    }
+    printf("sock.connect()=%d\r\n",ret);
+    
+    ret = client.connect();
+    if(ret != 0){
+        printf("MQTT connect failed\r\n");
+        return 1;
+    }
+    printf("client.connect()=%d\r\n",ret);
+    
+    const char* actuators = "switch,int\n";
+    const char* sensors = "analog,mV\n";
+
+    ret = client.subscribe("/control/" NODE_NAME "/switch", MQTT::QOS1, messageArrived);    
+    printf("sock.subscribe()=%d\r\n",ret);
+
+    //节点上线消息
+    meta_report(client, "events","online");
+    //报告所有可接受的控制指令
+    meta_report(client, "capability","control", actuators, strlen(actuators), true);
+    //报告所有的传感器
+    meta_report(client, "capability","values", sensors, strlen(sensors), true);
+    
+    bool btn = 0;
+    while(1){
+        bool newBTN = BTN;
+        if(newBTN != btn){
+            char buf[16];
+            int value = newBTN ? 3300 : 0;
+            sprintf(buf, "%d mV", value);
+            meta_report(client, "values","analog",buf,strlen(buf),true);
+            btn = newBTN;
+        }else{
+            client.yield(100);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Aug 25 15:08:59 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed/builds/fd96258d940d
\ No newline at end of file