Laser_SD_W5500

Dependencies:   MQTT SDFileSystem WIZnet_Library mbed

Fork of EthW5500 by YX ZHANG

Revision:
2:a50b794b8ede
Parent:
1:9689429a0a29
Child:
4:ae6f380a5b41
--- a/main.cpp	Fri Aug 25 15:46:28 2017 +0000
+++ b/main.cpp	Mon Jun 04 15:16:05 2018 +0000
@@ -1,86 +1,50 @@
 #include "mbed.h"
-#include "WIZnetInterface.h"
-#include "MQTTSocket.h"
-#include "MQTTClient.h"
+#include "networking.h"
 
+Serial pc(PA_9,PA_10);
 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};
+DigitalOut LED(PB_8);
 
-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)
+void on_control_cmd(const char* actuator_name, const char* control_value)
 {
-    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);
+    pc.printf("Received CMD %s %s\r\n", actuator_name, control_value);
+    if(strcmp(actuator_name, "led") == 0)
+        LED = atoi(control_value);
 }
- 
+
 int main() {
-    int ret;
-    wiz.init(mac_addr);
-    printf("DHCP...\r\n");
-    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";
+    //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
+    const char* sensors[][2] = {
+        "test", " ",
+        "button", "V",
+        NULL, NULL //最后一行以空指针作为结束标记
+    };
 
-    ret = client.subscribe("/control/" NODE_NAME "/switch", MQTT::QOS1, messageArrived);    
-    printf("sock.subscribe()=%d\r\n",ret);
+    //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
+    const char* actuators[][2] = {
+        "led", "int",
+        NULL, NULL //最后一行以空指针作为结束标记
+    };
+    networking_init(sock, client, "tdxls-iot.xicp.net", sensors, actuators, on_control_cmd);
 
-    //节点上线消息
-    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;
+            int value = (bool)newBTN;
+            
             sprintf(buf, "%d mV", value);
-            meta_report(client, "values","analog",buf,strlen(buf),true);
+            publish_value(client,"button",buf);
+            
             btn = newBTN;
         }else{
-            client.yield(100);
+            client.yield(1000);
+            publish_value(client,"test","hello world");
         }
     }
 }