Laser_SD_W5500
Dependencies: MQTT SDFileSystem WIZnet_Library mbed
Fork of EthW5500 by
main.cpp@1:9689429a0a29, 2017-08-25 (annotated)
- Committer:
- zhangyx
- Date:
- Fri Aug 25 15:46:28 2017 +0000
- Revision:
- 1:9689429a0a29
- Parent:
- 0:1170747a672f
- Child:
- 2:a50b794b8ede
print DHCP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zhangyx | 0:1170747a672f | 1 | #include "mbed.h" |
zhangyx | 0:1170747a672f | 2 | #include "WIZnetInterface.h" |
zhangyx | 0:1170747a672f | 3 | #include "MQTTSocket.h" |
zhangyx | 0:1170747a672f | 4 | #include "MQTTClient.h" |
zhangyx | 0:1170747a672f | 5 | |
zhangyx | 0:1170747a672f | 6 | DigitalIn BTN(PC_13); |
zhangyx | 0:1170747a672f | 7 | //W5500接线 mosi,miso,sclk,cs,reset |
zhangyx | 0:1170747a672f | 8 | WIZnetInterface wiz(PA_7,PA_6,PA_5,PB_6,PC_7); |
zhangyx | 0:1170747a672f | 9 | //节点名称任取 |
zhangyx | 0:1170747a672f | 10 | #define NODE_NAME "n_12345" |
zhangyx | 0:1170747a672f | 11 | //接在同一子网下的设备MAC地址必须不同 |
zhangyx | 0:1170747a672f | 12 | uint8_t mac_addr[6]={0x50,0x51,0x50,0x00,0x00,0x01}; |
zhangyx | 0:1170747a672f | 13 | |
zhangyx | 0:1170747a672f | 14 | typedef MQTT::Client<MQTTSocket,Countdown> MClient; |
zhangyx | 0:1170747a672f | 15 | void meta_report(MClient& client, const char* ns, const char* type, |
zhangyx | 0:1170747a672f | 16 | const char* payload = NULL, size_t payload_len = 0, |
zhangyx | 0:1170747a672f | 17 | bool retain = false, MQTT::QoS qos = MQTT::QOS1){ |
zhangyx | 0:1170747a672f | 18 | char topic[64]; |
zhangyx | 0:1170747a672f | 19 | sprintf(topic, "/%s/" NODE_NAME "/%s", ns, type); |
zhangyx | 0:1170747a672f | 20 | int ret = client.publish(topic, (void*)payload, payload_len, qos, retain); |
zhangyx | 0:1170747a672f | 21 | printf("client.publish()=%d\r\n",ret); |
zhangyx | 0:1170747a672f | 22 | } |
zhangyx | 0:1170747a672f | 23 | void messageArrived(MQTT::MessageData& md) |
zhangyx | 0:1170747a672f | 24 | { |
zhangyx | 0:1170747a672f | 25 | MQTT::Message &message = md.message; |
zhangyx | 0:1170747a672f | 26 | char buf[64]; |
zhangyx | 0:1170747a672f | 27 | int value, len = sizeof(buf)-1; |
zhangyx | 0:1170747a672f | 28 | if(message.payloadlen < len) |
zhangyx | 0:1170747a672f | 29 | len = message.payloadlen; |
zhangyx | 0:1170747a672f | 30 | memcpy(buf, message.payload, len); |
zhangyx | 0:1170747a672f | 31 | buf[len] = '\0'; |
zhangyx | 0:1170747a672f | 32 | sscanf(buf, "%d", &value); |
zhangyx | 0:1170747a672f | 33 | printf("received %d\r\n", value); |
zhangyx | 0:1170747a672f | 34 | } |
zhangyx | 0:1170747a672f | 35 | |
zhangyx | 0:1170747a672f | 36 | int main() { |
zhangyx | 0:1170747a672f | 37 | int ret; |
zhangyx | 0:1170747a672f | 38 | wiz.init(mac_addr); |
zhangyx | 1:9689429a0a29 | 39 | printf("DHCP...\r\n"); |
zhangyx | 0:1170747a672f | 40 | wiz.connect(); |
zhangyx | 0:1170747a672f | 41 | printf("IP: %s\r\n", wiz.getIPAddress()); |
zhangyx | 0:1170747a672f | 42 | |
zhangyx | 0:1170747a672f | 43 | MQTTSocket sock; |
zhangyx | 0:1170747a672f | 44 | MClient client(sock); |
zhangyx | 0:1170747a672f | 45 | |
zhangyx | 0:1170747a672f | 46 | ret = sock.connect("tdxls-iot.xicp.net",1883); |
zhangyx | 0:1170747a672f | 47 | if(ret != 0){ |
zhangyx | 0:1170747a672f | 48 | printf("failed to connect to TCP server\r\n"); |
zhangyx | 0:1170747a672f | 49 | return 1; |
zhangyx | 0:1170747a672f | 50 | } |
zhangyx | 0:1170747a672f | 51 | printf("sock.connect()=%d\r\n",ret); |
zhangyx | 0:1170747a672f | 52 | |
zhangyx | 0:1170747a672f | 53 | ret = client.connect(); |
zhangyx | 0:1170747a672f | 54 | if(ret != 0){ |
zhangyx | 0:1170747a672f | 55 | printf("MQTT connect failed\r\n"); |
zhangyx | 0:1170747a672f | 56 | return 1; |
zhangyx | 0:1170747a672f | 57 | } |
zhangyx | 0:1170747a672f | 58 | printf("client.connect()=%d\r\n",ret); |
zhangyx | 0:1170747a672f | 59 | |
zhangyx | 0:1170747a672f | 60 | const char* actuators = "switch,int\n"; |
zhangyx | 0:1170747a672f | 61 | const char* sensors = "analog,mV\n"; |
zhangyx | 0:1170747a672f | 62 | |
zhangyx | 0:1170747a672f | 63 | ret = client.subscribe("/control/" NODE_NAME "/switch", MQTT::QOS1, messageArrived); |
zhangyx | 0:1170747a672f | 64 | printf("sock.subscribe()=%d\r\n",ret); |
zhangyx | 0:1170747a672f | 65 | |
zhangyx | 0:1170747a672f | 66 | //节点上线消息 |
zhangyx | 0:1170747a672f | 67 | meta_report(client, "events","online"); |
zhangyx | 0:1170747a672f | 68 | //报告所有可接受的控制指令 |
zhangyx | 0:1170747a672f | 69 | meta_report(client, "capability","control", actuators, strlen(actuators), true); |
zhangyx | 0:1170747a672f | 70 | //报告所有的传感器 |
zhangyx | 0:1170747a672f | 71 | meta_report(client, "capability","values", sensors, strlen(sensors), true); |
zhangyx | 0:1170747a672f | 72 | |
zhangyx | 0:1170747a672f | 73 | bool btn = 0; |
zhangyx | 0:1170747a672f | 74 | while(1){ |
zhangyx | 0:1170747a672f | 75 | bool newBTN = BTN; |
zhangyx | 0:1170747a672f | 76 | if(newBTN != btn){ |
zhangyx | 0:1170747a672f | 77 | char buf[16]; |
zhangyx | 0:1170747a672f | 78 | int value = newBTN ? 3300 : 0; |
zhangyx | 0:1170747a672f | 79 | sprintf(buf, "%d mV", value); |
zhangyx | 0:1170747a672f | 80 | meta_report(client, "values","analog",buf,strlen(buf),true); |
zhangyx | 0:1170747a672f | 81 | btn = newBTN; |
zhangyx | 0:1170747a672f | 82 | }else{ |
zhangyx | 0:1170747a672f | 83 | client.yield(100); |
zhangyx | 0:1170747a672f | 84 | } |
zhangyx | 0:1170747a672f | 85 | } |
zhangyx | 0:1170747a672f | 86 | } |