EthW5500+STM32

Dependencies:   MQTT WIZnet_Library mbed

Fork of EthW5500 by YX ZHANG

Files at this revision

API Documentation at this revision

Comitter:
AlexQian
Date:
Fri Apr 27 06:50:55 2018 +0000
Parent:
4:10f83daba9ea
Commit message:
updata

Changed in this revision

ETHW5500.cpp Show annotated file Show diff for this revision Revisions of this file
ETHW5500.h 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
diff -r 10f83daba9ea -r 23424c038538 ETHW5500.cpp
--- a/ETHW5500.cpp	Fri Apr 20 07:28:56 2018 +0000
+++ b/ETHW5500.cpp	Fri Apr 27 06:50:55 2018 +0000
@@ -1,11 +1,15 @@
 #include "ETHW5500.h"
-
+#include "WIZnetInterface.h"
+#include "MQTTSocket.h"
+#include "MQTTClient.h"
 //W5500接线 mosi,miso,sclk,cs,reset
 //WIZnetInterface wiz(PA_7,PA_6,PA_5,PB_6,PC_7);
 extern WIZnetInterface wiz;
 extern Serial pc;
-void meta_report(MClient& client, const char *topic, 
-                    const char* payload = NULL, size_t payload_len = 0, 
+extern MQTTSocket sock;
+extern MClient client;
+void meta_report( const char *topic, 
+                    const char* payload = NULL, size_t payload_len =    0, 
                     bool retain = false, MQTT::QoS qos = MQTT::QOS1){
     int ret = client.publish(topic, (void*)payload, payload_len, qos, retain);
     pc.printf("client.publish()=%d\r\n",ret);
@@ -22,18 +26,20 @@
     sscanf(buf, "%d", &value);
     pc.printf("received %d\r\n", value);
 }
-MQTTSocket sock;
-MClient client(sock);
-int Eth_Init(uint8_t *mac_addr)
+
+void Eth_Init(uint8_t *mac_addr)
 {
-    int ret;
     wiz.init(mac_addr);
     pc.printf("DHCP...\r\n");
     wiz.connect();
     pc.printf("IP: %s\r\n", wiz.getIPAddress());
-    
+}
+
+int Eth_ConnetToSever(char* URL)
+{
+    int ret;
     int timecount=1;
-    ret = sock.connect("tdxls-iot.xicp.net",1883);
+    ret = sock.connect(URL,1883);
     //pc.printf("%d-th Server connetion attemp\n",timecount);
     while(ret!=0){
         timecount++;
@@ -71,7 +77,7 @@
         char topic[64];
         sprintf(topic, "/%s/%s/%s", ns, NODE_NAME, type);
         //printf("topic=%s\r\n",topic);
-        meta_report(client, topic,payload,payload_len,retain);
+        meta_report(topic,payload,payload_len,retain);
 }
 
  
diff -r 10f83daba9ea -r 23424c038538 ETHW5500.h
--- a/ETHW5500.h	Fri Apr 20 07:28:56 2018 +0000
+++ b/ETHW5500.h	Fri Apr 27 06:50:55 2018 +0000
@@ -5,8 +5,9 @@
 #include "MQTTSocket.h"
 #include "MQTTClient.h"
 typedef MQTT::Client<MQTTSocket,Countdown> MClient;
-int Eth_Init(uint8_t *mar_addr);
-int Eth_ConnectToSer(char *URL);
+
+void Eth_Init(uint8_t *mar_addr);
+int Eth_ConnetToSever(char* URL);
 int Eth_Subscribe(const char* ns,const char *NODE_NAME, const char* type);
 void Eth_Report(const char* ns,const char *NODE_NAME, const char* type,const char* payload , size_t payload_len , 
                     bool retain );
diff -r 10f83daba9ea -r 23424c038538 main.cpp
--- a/main.cpp	Fri Apr 20 07:28:56 2018 +0000
+++ b/main.cpp	Fri Apr 27 06:50:55 2018 +0000
@@ -6,25 +6,29 @@
 Serial pc(PA_2,PA_3);//串口
 //W5500接线 mosi,miso,sclk,cs,reset
 WIZnetInterface wiz(PA_7,PA_6,PA_5,PB_5,PB_4);
+//WIZnetInterface wiz(PA_7,PA_6,PA_5,PB_6,PC_7);
 DigitalIn BTN(PA_4);//按键
 //节点名称任取
 char *NODE_NAME="n_12345";
  //接在同一子网下的设备MAC地址必须不同
 uint8_t mac_addr[6]={0x50,0x51,0x50,0x00,0x00,0x01};
-
+char* URL="tdxls-iot.xicp.net"; //服务器地址
+MQTTSocket sock;
+MClient client(sock);
 int main() {
-        const char* actuators = "switch,int\n";
-    const char* sensors = "analog,mV\n";
-        pc.printf("Initing\n");
-        Eth_Init(mac_addr);
+   
+   const char* actuators = "switch,int\n";
+   const char* sensors = "analog,mV\n";
+   pc.printf("Initing\n");
+   Eth_Init(mac_addr);
+   Eth_ConnetToSever(URL);
+   Eth_Subscribe("control",NODE_NAME,"switch");
     
-    Eth_Subscribe("control",NODE_NAME,"switch");
-    
-    Eth_Report("event",NODE_NAME,"online",NULL,0,false);
-        Eth_Report("capability",NODE_NAME,"control",actuators,strlen(actuators),true);
-        Eth_Report("capability",NODE_NAME,"values",sensors,strlen(sensors),true);
-    bool btn = 0;
-    while(1){
+   Eth_Report("event",NODE_NAME,"online",NULL,0,false);
+   Eth_Report("capability",NODE_NAME,"control",actuators,strlen(actuators),true);
+   Eth_Report("capability",NODE_NAME,"values",sensors,strlen(sensors),true);
+   bool btn = 0;
+   while(1){
         bool newBTN = BTN;
         if(newBTN != btn){
             char buf[16];