Xiaoyuan Yu
/
Ex_esp8266
for high school students
main.cpp@0:4c6832694c5e, 2018-09-14 (annotated)
- Committer:
- Dennis_Yu
- Date:
- Fri Sep 14 07:55:51 2018 +0000
- Revision:
- 0:4c6832694c5e
required the mbed.h at version of 147
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dennis_Yu | 0:4c6832694c5e | 1 | #include "mbed.h" |
Dennis_Yu | 0:4c6832694c5e | 2 | #include "esp8266.h" |
Dennis_Yu | 0:4c6832694c5e | 3 | /********** |
Dennis_Yu | 0:4c6832694c5e | 4 | * 订阅和发布地址格式 |
Dennis_Yu | 0:4c6832694c5e | 5 | * 订阅主题/control/device/actuators |
Dennis_Yu | 0:4c6832694c5e | 6 | * 发布主题/values/device/sensors |
Dennis_Yu | 0:4c6832694c5e | 7 | **********/ |
Dennis_Yu | 0:4c6832694c5e | 8 | |
Dennis_Yu | 0:4c6832694c5e | 9 | |
Dennis_Yu | 0:4c6832694c5e | 10 | Serial ser2usb(PA_2, PA_3); |
Dennis_Yu | 0:4c6832694c5e | 11 | |
Dennis_Yu | 0:4c6832694c5e | 12 | //声明设备名称,用于订阅和发布消息 |
Dennis_Yu | 0:4c6832694c5e | 13 | const char device[] = "warehouse"; |
Dennis_Yu | 0:4c6832694c5e | 14 | |
Dennis_Yu | 0:4c6832694c5e | 15 | //声明所有传感器, 每行一个, 每个由名字单位两部分组成 |
Dennis_Yu | 0:4c6832694c5e | 16 | const char* sensors[][2] = { |
Dennis_Yu | 0:4c6832694c5e | 17 | "report", "", |
Dennis_Yu | 0:4c6832694c5e | 18 | NULL, NULL //last line must end with empty pointer |
Dennis_Yu | 0:4c6832694c5e | 19 | }; |
Dennis_Yu | 0:4c6832694c5e | 20 | |
Dennis_Yu | 0:4c6832694c5e | 21 | //声明所有执行器, 每行一个, 每个由名字参数类型两部分组成 |
Dennis_Yu | 0:4c6832694c5e | 22 | const char* actuators[][2] = { |
Dennis_Yu | 0:4c6832694c5e | 23 | "command", "", |
Dennis_Yu | 0:4c6832694c5e | 24 | "dat", "", |
Dennis_Yu | 0:4c6832694c5e | 25 | NULL, NULL //last line must end with empty pointer |
Dennis_Yu | 0:4c6832694c5e | 26 | }; |
Dennis_Yu | 0:4c6832694c5e | 27 | |
Dennis_Yu | 0:4c6832694c5e | 28 | |
Dennis_Yu | 0:4c6832694c5e | 29 | int main() |
Dennis_Yu | 0:4c6832694c5e | 30 | { |
Dennis_Yu | 0:4c6832694c5e | 31 | ser2usb.baud(115200); |
Dennis_Yu | 0:4c6832694c5e | 32 | // connect to wifi |
Dennis_Yu | 0:4c6832694c5e | 33 | ser2usb.printf("Initializing...\r\n"); |
Dennis_Yu | 0:4c6832694c5e | 34 | Esp8266 client(PA_9, PA_10, "iot_b827eb8fb527", "7c02b50b");// TX pin / RX pin / SSID / Password |
Dennis_Yu | 0:4c6832694c5e | 35 | |
Dennis_Yu | 0:4c6832694c5e | 36 | // connect to server |
Dennis_Yu | 0:4c6832694c5e | 37 | ser2usb.printf("connecting to mqtt server...\r\n"); |
Dennis_Yu | 0:4c6832694c5e | 38 | client.connect_mqtt_broker("192.168.12.1", device, sensors, actuators); |
Dennis_Yu | 0:4c6832694c5e | 39 | |
Dennis_Yu | 0:4c6832694c5e | 40 | //subscribe topic |
Dennis_Yu | 0:4c6832694c5e | 41 | client.subscribe_control(actuators[0][0]); |
Dennis_Yu | 0:4c6832694c5e | 42 | client.subscribe_control(actuators[1][0]); |
Dennis_Yu | 0:4c6832694c5e | 43 | |
Dennis_Yu | 0:4c6832694c5e | 44 | // done report |
Dennis_Yu | 0:4c6832694c5e | 45 | ser2usb.printf("Initialization done.\r\n"); |
Dennis_Yu | 0:4c6832694c5e | 46 | |
Dennis_Yu | 0:4c6832694c5e | 47 | // declare buff |
Dennis_Yu | 0:4c6832694c5e | 48 | char actuator_name[32], control_value[32]; |
Dennis_Yu | 0:4c6832694c5e | 49 | |
Dennis_Yu | 0:4c6832694c5e | 50 | // main function |
Dennis_Yu | 0:4c6832694c5e | 51 | while (1) |
Dennis_Yu | 0:4c6832694c5e | 52 | { |
Dennis_Yu | 0:4c6832694c5e | 53 | // looking for new command |
Dennis_Yu | 0:4c6832694c5e | 54 | if (client.get_control_cmd(actuator_name, control_value)) |
Dennis_Yu | 0:4c6832694c5e | 55 | { |
Dennis_Yu | 0:4c6832694c5e | 56 | ser2usb.printf("Received %s %s\r\n", actuator_name, control_value); |
Dennis_Yu | 0:4c6832694c5e | 57 | if (strcmp(actuator_name, "command") == 0) |
Dennis_Yu | 0:4c6832694c5e | 58 | { |
Dennis_Yu | 0:4c6832694c5e | 59 | client.publish_value("report", "received_a_command"); |
Dennis_Yu | 0:4c6832694c5e | 60 | } |
Dennis_Yu | 0:4c6832694c5e | 61 | else if (strcmp(actuator_name, "dat") == 0) |
Dennis_Yu | 0:4c6832694c5e | 62 | { |
Dennis_Yu | 0:4c6832694c5e | 63 | client.publish_value("report", "received_a_data"); |
Dennis_Yu | 0:4c6832694c5e | 64 | } |
Dennis_Yu | 0:4c6832694c5e | 65 | } |
Dennis_Yu | 0:4c6832694c5e | 66 | } |
Dennis_Yu | 0:4c6832694c5e | 67 | } |