![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Kuo Yiching
Fork of iot_example by
main.cpp@7:49a823b5a935, 2017-12-13 (annotated)
- Committer:
- zhangyx
- Date:
- Wed Dec 13 10:09:44 2017 +0000
- Revision:
- 7:49a823b5a935
- Parent:
- 5:9a0acd11c445
- Child:
- 8:cd472721e8a8
fix control command
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brainliang | 0:63af4719467f | 1 | /*----------------------------------------------------- |
brainliang | 0:63af4719467f | 2 | File Name : main.cpp |
brainliang | 0:63af4719467f | 3 | Purpose : For esp8266 mbed porting |
brainliang | 0:63af4719467f | 4 | Creation Date : 22-06-2017 |
brainliang | 0:63af4719467f | 5 | Last Modified : |
brainliang | 0:63af4719467f | 6 | Created By : Jeasine Ma [jeasinema[at]gmail[dot]com] |
brainliang | 0:63af4719467f | 7 | -----------------------------------------------------*/ |
brainliang | 0:63af4719467f | 8 | #include <cstdarg> |
brainliang | 0:63af4719467f | 9 | #include <cstring> |
brainliang | 0:63af4719467f | 10 | #include "mbed.h" |
brainliang | 0:63af4719467f | 11 | #include "esp8266.h" |
brainliang | 0:63af4719467f | 12 | |
zhangyx | 7:49a823b5a935 | 13 | Serial ser2usb(PA_2, PA_3, 115200); |
zyx | 3:06080fa094a0 | 14 | DigitalOut LED(PD_2); |
zhangyx | 7:49a823b5a935 | 15 | DigitalIn humansensor(PA_0); |
zhangyx | 7:49a823b5a935 | 16 | |
zhangyx | 7:49a823b5a935 | 17 | int test_main(void){ |
zhangyx | 7:49a823b5a935 | 18 | Serial esp8266(PA_9, PA_10, 9600); |
zhangyx | 7:49a823b5a935 | 19 | while(1){ |
zhangyx | 7:49a823b5a935 | 20 | if(ser2usb.readable()) |
zhangyx | 7:49a823b5a935 | 21 | esp8266.putc(ser2usb.getc()); |
zhangyx | 7:49a823b5a935 | 22 | if(esp8266.readable()) |
zhangyx | 7:49a823b5a935 | 23 | ser2usb.putc(esp8266.getc()); |
zhangyx | 7:49a823b5a935 | 24 | } |
zhangyx | 7:49a823b5a935 | 25 | } |
brainliang | 0:63af4719467f | 26 | |
brainliang | 0:63af4719467f | 27 | int main(void) { |
zhangyx | 7:49a823b5a935 | 28 | |
zhangyx | 1:3b487c4813a2 | 29 | // 选定与 esp8266 相连接的串口,WiFi 名称和密码 |
zhangyx | 1:3b487c4813a2 | 30 | // 参数分别为 TX pin / RX pin / SSID / Password |
zhangyx | 7:49a823b5a935 | 31 | Esp8266 client(PA_9, PA_10, "TSINGHUA.iCenter", "TS.icenter"); |
zyx | 3:06080fa094a0 | 32 | |
zyx | 3:06080fa094a0 | 33 | //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 |
zyx | 3:06080fa094a0 | 34 | //服务器收到声明后,将在网页上显示这些传感器 |
zyx | 3:06080fa094a0 | 35 | const char* sensors[][2] = { |
zhangyx | 7:49a823b5a935 | 36 | //"pressure", "Pa", |
zhangyx | 7:49a823b5a935 | 37 | // "temperature", "C", |
zhangyx | 7:49a823b5a935 | 38 | // "humidity", "%", |
zhangyx | 7:49a823b5a935 | 39 | "huamn","person", |
zyx | 3:06080fa094a0 | 40 | NULL, NULL //最后一行以空指针作为结束标记 |
zyx | 3:06080fa094a0 | 41 | }; |
zyx | 3:06080fa094a0 | 42 | |
zyx | 3:06080fa094a0 | 43 | //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 |
zyx | 3:06080fa094a0 | 44 | //服务器收到声明后,将在网页上显示这些执行器 |
zyx | 3:06080fa094a0 | 45 | const char* actuators[][2] = { |
zhangyx | 5:9a0acd11c445 | 46 | "servo1", "int", |
zyx | 3:06080fa094a0 | 47 | "switch2", "int", |
zyx | 3:06080fa094a0 | 48 | NULL, NULL //最后一行以空指针作为结束标记 |
zyx | 3:06080fa094a0 | 49 | }; |
zyx | 3:06080fa094a0 | 50 | |
zyx | 3:06080fa094a0 | 51 | //连接到服务器 |
zyx | 3:06080fa094a0 | 52 | client.connect_mqtt_broker("tdxls-iot.xicp.net", sensors, actuators); |
zyx | 3:06080fa094a0 | 53 | |
brainliang | 0:63af4719467f | 54 | ser2usb.printf("Initialization done.\r\n"); |
zyx | 3:06080fa094a0 | 55 | |
zyx | 3:06080fa094a0 | 56 | char actuator_name[32], control_value[32]; |
zyx | 3:06080fa094a0 | 57 | float last_report=0; |
zyx | 3:06080fa094a0 | 58 | Timer t;// 定时器用于计量发送传感器数据的时间 |
zyx | 3:06080fa094a0 | 59 | t.start(); |
zyx | 3:06080fa094a0 | 60 | |
brainliang | 0:63af4719467f | 61 | while(1) { |
zyx | 3:06080fa094a0 | 62 | //检查有没有收到新的执行器控制指令 |
zyx | 3:06080fa094a0 | 63 | if(client.get_control_cmd(actuator_name, control_value)){ |
zyx | 3:06080fa094a0 | 64 | ser2usb.printf("Received CMD %s, %s\r\n", actuator_name, control_value); |
zyx | 3:06080fa094a0 | 65 | //判断哪个执行器收到命令 |
zyx | 3:06080fa094a0 | 66 | if(strcmp(actuator_name, "switch1")){ |
zyx | 3:06080fa094a0 | 67 | //do something for switch1 |
zyx | 3:06080fa094a0 | 68 | LED = atoi(control_value); |
zyx | 3:06080fa094a0 | 69 | }else if(strcmp(actuator_name, "switch2")){ |
zyx | 3:06080fa094a0 | 70 | //do something for switch2 |
zyx | 3:06080fa094a0 | 71 | } |
zyx | 3:06080fa094a0 | 72 | } |
zyx | 3:06080fa094a0 | 73 | |
zyx | 3:06080fa094a0 | 74 | if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据 |
zyx | 3:06080fa094a0 | 75 | |
zyx | 3:06080fa094a0 | 76 | //汇报传感器数据,两个参数分别是传感器名字和值 |
zhangyx | 7:49a823b5a935 | 77 | //client.publish_value("pressure", "100000 Pa"); |
zhangyx | 7:49a823b5a935 | 78 | // client.publish_value("temperature", "20 C"); |
zhangyx | 7:49a823b5a935 | 79 | // client.publish_value("humidity", "30 %"); |
zhangyx | 7:49a823b5a935 | 80 | client.publish_value("huamn", "30 %"); |
zhangyx | 5:9a0acd11c445 | 81 | |
zhangyx | 5:9a0acd11c445 | 82 | last_report = t.read(); |
zyx | 3:06080fa094a0 | 83 | } |
brainliang | 0:63af4719467f | 84 | } |
brainliang | 0:63af4719467f | 85 | } |