Laser_SD_W5500
Dependencies: MQTT SDFileSystem WIZnet_Library mbed
Fork of EthW5500 by
main.cpp
- Committer:
- zhangyx
- Date:
- 2017-08-25
- Revision:
- 1:9689429a0a29
- Parent:
- 0:1170747a672f
- Child:
- 2:a50b794b8ede
File content as of revision 1:9689429a0a29:
#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); 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"; 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); } } }