![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
STM32 + W5500 + MQTT
Dependencies: MQTT WIZnet_Library mbed
main.cpp@2:a50b794b8ede, 2018-06-04 (annotated)
- Committer:
- zhangyx
- Date:
- Mon Jun 04 15:16:05 2018 +0000
- Revision:
- 2:a50b794b8ede
- Parent:
- 1:9689429a0a29
better API
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zhangyx | 0:1170747a672f | 1 | #include "mbed.h" |
zhangyx | 2:a50b794b8ede | 2 | #include "networking.h" |
zhangyx | 0:1170747a672f | 3 | |
zhangyx | 2:a50b794b8ede | 4 | Serial pc(PA_9,PA_10); |
zhangyx | 0:1170747a672f | 5 | DigitalIn BTN(PC_13); |
zhangyx | 2:a50b794b8ede | 6 | DigitalOut LED(PB_8); |
zhangyx | 0:1170747a672f | 7 | |
zhangyx | 2:a50b794b8ede | 8 | void on_control_cmd(const char* actuator_name, const char* control_value) |
zhangyx | 0:1170747a672f | 9 | { |
zhangyx | 2:a50b794b8ede | 10 | pc.printf("Received CMD %s %s\r\n", actuator_name, control_value); |
zhangyx | 2:a50b794b8ede | 11 | if(strcmp(actuator_name, "led") == 0) |
zhangyx | 2:a50b794b8ede | 12 | LED = atoi(control_value); |
zhangyx | 0:1170747a672f | 13 | } |
zhangyx | 2:a50b794b8ede | 14 | |
zhangyx | 0:1170747a672f | 15 | int main() { |
zhangyx | 0:1170747a672f | 16 | |
zhangyx | 0:1170747a672f | 17 | MQTTSocket sock; |
zhangyx | 0:1170747a672f | 18 | MClient client(sock); |
zhangyx | 0:1170747a672f | 19 | |
zhangyx | 2:a50b794b8ede | 20 | //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 |
zhangyx | 2:a50b794b8ede | 21 | const char* sensors[][2] = { |
zhangyx | 2:a50b794b8ede | 22 | "test", " ", |
zhangyx | 2:a50b794b8ede | 23 | "button", "V", |
zhangyx | 2:a50b794b8ede | 24 | NULL, NULL //最后一行以空指针作为结束标记 |
zhangyx | 2:a50b794b8ede | 25 | }; |
zhangyx | 0:1170747a672f | 26 | |
zhangyx | 2:a50b794b8ede | 27 | //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 |
zhangyx | 2:a50b794b8ede | 28 | const char* actuators[][2] = { |
zhangyx | 2:a50b794b8ede | 29 | "led", "int", |
zhangyx | 2:a50b794b8ede | 30 | NULL, NULL //最后一行以空指针作为结束标记 |
zhangyx | 2:a50b794b8ede | 31 | }; |
zhangyx | 2:a50b794b8ede | 32 | networking_init(sock, client, "tdxls-iot.xicp.net", sensors, actuators, on_control_cmd); |
zhangyx | 0:1170747a672f | 33 | |
zhangyx | 0:1170747a672f | 34 | bool btn = 0; |
zhangyx | 0:1170747a672f | 35 | while(1){ |
zhangyx | 0:1170747a672f | 36 | bool newBTN = BTN; |
zhangyx | 0:1170747a672f | 37 | if(newBTN != btn){ |
zhangyx | 0:1170747a672f | 38 | char buf[16]; |
zhangyx | 2:a50b794b8ede | 39 | int value = (bool)newBTN; |
zhangyx | 2:a50b794b8ede | 40 | |
zhangyx | 0:1170747a672f | 41 | sprintf(buf, "%d mV", value); |
zhangyx | 2:a50b794b8ede | 42 | publish_value(client,"button",buf); |
zhangyx | 2:a50b794b8ede | 43 | |
zhangyx | 0:1170747a672f | 44 | btn = newBTN; |
zhangyx | 0:1170747a672f | 45 | }else{ |
zhangyx | 2:a50b794b8ede | 46 | client.yield(1000); |
zhangyx | 2:a50b794b8ede | 47 | publish_value(client,"test","hello world"); |
zhangyx | 0:1170747a672f | 48 | } |
zhangyx | 0:1170747a672f | 49 | } |
zhangyx | 0:1170747a672f | 50 | } |