EthW5500+STM32

Dependencies:   MQTT WIZnet_Library mbed

Fork of EthW5500 by YX ZHANG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WIZnetInterface.h"
00003 #include "MQTTSocket.h"
00004 #include "MQTTClient.h"
00005 #include "ETHW5500.h"
00006 Serial pc(PA_2,PA_3);//串口
00007 //W5500接线 mosi,miso,sclk,cs,reset
00008 WIZnetInterface wiz(PA_7,PA_6,PA_5,PB_5,PB_4);
00009 //WIZnetInterface wiz(PA_7,PA_6,PA_5,PB_6,PC_7);
00010 DigitalIn BTN(PA_4);//按键
00011 //节点名称任取
00012 char *NODE_NAME="n_12345";
00013  //接在同一子网下的设备MAC地址必须不同
00014 uint8_t mac_addr[6]={0x50,0x51,0x50,0x00,0x00,0x01};
00015 char* URL="tdxls-iot.xicp.net"; //服务器地址
00016 MQTTSocket sock;
00017 MClient client(sock);
00018 int main() {
00019    
00020    const char* actuators = "switch,int\n";
00021    const char* sensors = "analog,mV\n";
00022    pc.printf("Initing\n");
00023    Eth_Init(mac_addr);
00024    Eth_ConnetToSever(URL);
00025    Eth_Subscribe("control",NODE_NAME,"switch");
00026     
00027    Eth_Report("event",NODE_NAME,"online",NULL,0,false);
00028    Eth_Report("capability",NODE_NAME,"control",actuators,strlen(actuators),true);
00029    Eth_Report("capability",NODE_NAME,"values",sensors,strlen(sensors),true);
00030    bool btn = 0;
00031    while(1){
00032         bool newBTN = BTN;
00033         if(newBTN != btn){
00034             char buf[16];
00035             int value = newBTN ? 3300 : 0;
00036             sprintf(buf, "%d mV", value);
00037             Eth_Report("values",NODE_NAME,"analog",buf,strlen(buf),true);
00038             btn = newBTN;
00039         }else{
00040             wait(0.1);
00041         }
00042     }
00043 }