王铭轩 仓库
Fork of SBY_sw4stm32_nucleo_f103rb by
SBY/main.cpp@1:4d8def597e29, 2018-07-20 (annotated)
- Committer:
- xmwmx
- Date:
- Fri Jul 20 08:20:41 2018 +0000
- Revision:
- 1:4d8def597e29
- Parent:
- 0:7725181a2a09
- Child:
- 2:a779f481c401
first useful edition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xmwmx | 0:7725181a2a09 | 1 | #include <cstdarg> |
xmwmx | 0:7725181a2a09 | 2 | #include <cstring> |
xmwmx | 0:7725181a2a09 | 3 | #include "esp8266.h" |
xmwmx | 0:7725181a2a09 | 4 | #include "mbed.h" |
xmwmx | 0:7725181a2a09 | 5 | |
xmwmx | 0:7725181a2a09 | 6 | |
xmwmx | 1:4d8def597e29 | 7 | |
xmwmx | 0:7725181a2a09 | 8 | Serial ser2usb(PA_2, PA_3, 115200); |
xmwmx | 0:7725181a2a09 | 9 | int state = 0; |
xmwmx | 0:7725181a2a09 | 10 | void open(int row,int col); |
xmwmx | 0:7725181a2a09 | 11 | void SetUp(); |
xmwmx | 0:7725181a2a09 | 12 | |
xmwmx | 0:7725181a2a09 | 13 | DigitalOut red[2]= |
xmwmx | 0:7725181a2a09 | 14 | { |
xmwmx | 0:7725181a2a09 | 15 | PB_13,PB_12, |
xmwmx | 0:7725181a2a09 | 16 | }; |
xmwmx | 0:7725181a2a09 | 17 | |
xmwmx | 0:7725181a2a09 | 18 | DigitalOut black[3]= |
xmwmx | 0:7725181a2a09 | 19 | { |
xmwmx | 0:7725181a2a09 | 20 | PA_12,PA_11,PB_3, |
xmwmx | 0:7725181a2a09 | 21 | }; |
xmwmx | 0:7725181a2a09 | 22 | |
xmwmx | 0:7725181a2a09 | 23 | |
xmwmx | 0:7725181a2a09 | 24 | int main() |
xmwmx | 0:7725181a2a09 | 25 | { |
xmwmx | 0:7725181a2a09 | 26 | SetUp(); |
xmwmx | 1:4d8def597e29 | 27 | wait(2); |
xmwmx | 1:4d8def597e29 | 28 | |
xmwmx | 0:7725181a2a09 | 29 | |
xmwmx | 0:7725181a2a09 | 30 | |
xmwmx | 0:7725181a2a09 | 31 | |
xmwmx | 0:7725181a2a09 | 32 | //ser2usb.printf("starting\r\n"); |
xmwmx | 0:7725181a2a09 | 33 | |
xmwmx | 0:7725181a2a09 | 34 | // 选定与 esp8266 相连接的串口,WiFi 名称和密码 |
xmwmx | 0:7725181a2a09 | 35 | Esp8266 client(PA_9, PA_10, "iot_b827eb8fb527", "7c02b50b");// 参数分别为 TX pin / RX pin / SSID / Password |
xmwmx | 0:7725181a2a09 | 36 | |
xmwmx | 0:7725181a2a09 | 37 | //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 |
xmwmx | 0:7725181a2a09 | 38 | const char* sensors[][2] = { |
xmwmx | 0:7725181a2a09 | 39 | "actionok", "", |
xmwmx | 0:7725181a2a09 | 40 | NULL, NULL //最后一行以空指针作为结束标记 |
xmwmx | 0:7725181a2a09 | 41 | }; |
xmwmx | 0:7725181a2a09 | 42 | |
xmwmx | 0:7725181a2a09 | 43 | //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 |
xmwmx | 0:7725181a2a09 | 44 | const char* actuators[][2] = { |
xmwmx | 1:4d8def597e29 | 45 | "do", "int", |
xmwmx | 0:7725181a2a09 | 46 | NULL, NULL //最后一行以空指针作为结束标记 |
xmwmx | 0:7725181a2a09 | 47 | }; |
xmwmx | 0:7725181a2a09 | 48 | //ser2usb.printf("connecting...\r\n"); |
xmwmx | 0:7725181a2a09 | 49 | |
xmwmx | 0:7725181a2a09 | 50 | //连接到服务器 |
xmwmx | 1:4d8def597e29 | 51 | client.connect_mqtt_broker("192.168.12.1", "car", sensors, actuators); |
xmwmx | 0:7725181a2a09 | 52 | |
xmwmx | 0:7725181a2a09 | 53 | ser2usb.printf("Initialization done.\r\n"); |
xmwmx | 0:7725181a2a09 | 54 | char actuator_name[32], control_value[32]; |
xmwmx | 0:7725181a2a09 | 55 | Timer t;// 定时器用于计量发送传感器数据的时间 |
xmwmx | 0:7725181a2a09 | 56 | t.start(); |
xmwmx | 1:4d8def597e29 | 57 | client.publish_value("do", "carinit"); |
xmwmx | 1:4d8def597e29 | 58 | |
xmwmx | 0:7725181a2a09 | 59 | while(1) |
xmwmx | 0:7725181a2a09 | 60 | { |
xmwmx | 0:7725181a2a09 | 61 | //检查有没有收到新的执行器控制指令 |
xmwmx | 0:7725181a2a09 | 62 | if(client.get_control_cmd(actuator_name, control_value)) |
xmwmx | 0:7725181a2a09 | 63 | { |
xmwmx | 0:7725181a2a09 | 64 | ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value); |
xmwmx | 0:7725181a2a09 | 65 | //判断哪个执行器收到命令 |
xmwmx | 0:7725181a2a09 | 66 | state = atoi(control_value); |
xmwmx | 0:7725181a2a09 | 67 | switch(state) |
xmwmx | 0:7725181a2a09 | 68 | { |
xmwmx | 0:7725181a2a09 | 69 | case 1: |
xmwmx | 0:7725181a2a09 | 70 | open(0,0); |
xmwmx | 1:4d8def597e29 | 71 | client.publish_value("do", "carfinish"); |
xmwmx | 0:7725181a2a09 | 72 | break; |
xmwmx | 0:7725181a2a09 | 73 | case 2: |
xmwmx | 0:7725181a2a09 | 74 | open(0,1); |
xmwmx | 1:4d8def597e29 | 75 | client.publish_value("do", "carfinish"); |
xmwmx | 0:7725181a2a09 | 76 | break; |
xmwmx | 0:7725181a2a09 | 77 | case 3: |
xmwmx | 0:7725181a2a09 | 78 | open(0,2); |
xmwmx | 1:4d8def597e29 | 79 | client.publish_value("do", "carfinish"); |
xmwmx | 0:7725181a2a09 | 80 | break; |
xmwmx | 0:7725181a2a09 | 81 | case 4: |
xmwmx | 0:7725181a2a09 | 82 | open(1,0); |
xmwmx | 1:4d8def597e29 | 83 | client.publish_value("do", "carfinish"); |
xmwmx | 0:7725181a2a09 | 84 | break; |
xmwmx | 0:7725181a2a09 | 85 | case 5: |
xmwmx | 0:7725181a2a09 | 86 | open(1,1); |
xmwmx | 1:4d8def597e29 | 87 | client.publish_value("do", "carfinish"); |
xmwmx | 0:7725181a2a09 | 88 | break; |
xmwmx | 0:7725181a2a09 | 89 | case 6: |
xmwmx | 1:4d8def597e29 | 90 | open(1,2); |
xmwmx | 1:4d8def597e29 | 91 | client.publish_value("do", "carfinish"); |
xmwmx | 0:7725181a2a09 | 92 | default: |
xmwmx | 0:7725181a2a09 | 93 | break; |
xmwmx | 0:7725181a2a09 | 94 | } |
xmwmx | 0:7725181a2a09 | 95 | } |
xmwmx | 1:4d8def597e29 | 96 | } |
xmwmx | 1:4d8def597e29 | 97 | |
xmwmx | 1:4d8def597e29 | 98 | |
xmwmx | 1:4d8def597e29 | 99 | |
xmwmx | 0:7725181a2a09 | 100 | } |
xmwmx | 0:7725181a2a09 | 101 | |
xmwmx | 0:7725181a2a09 | 102 | void SetUp() |
xmwmx | 0:7725181a2a09 | 103 | { |
xmwmx | 0:7725181a2a09 | 104 | for(int i=0; i<2; i++) |
xmwmx | 0:7725181a2a09 | 105 | { |
xmwmx | 0:7725181a2a09 | 106 | red[i].write(0); |
xmwmx | 0:7725181a2a09 | 107 | } |
xmwmx | 0:7725181a2a09 | 108 | |
xmwmx | 0:7725181a2a09 | 109 | for(int i=0; i<3; i++) |
xmwmx | 0:7725181a2a09 | 110 | { |
xmwmx | 0:7725181a2a09 | 111 | black[i].write(0); |
xmwmx | 0:7725181a2a09 | 112 | } |
xmwmx | 0:7725181a2a09 | 113 | |
xmwmx | 0:7725181a2a09 | 114 | |
xmwmx | 0:7725181a2a09 | 115 | } |
xmwmx | 0:7725181a2a09 | 116 | void open(int row,int col) |
xmwmx | 0:7725181a2a09 | 117 | { |
xmwmx | 0:7725181a2a09 | 118 | red[row].write(1); |
xmwmx | 0:7725181a2a09 | 119 | black[col].write(1); |
xmwmx | 0:7725181a2a09 | 120 | wait(0.2); |
xmwmx | 0:7725181a2a09 | 121 | SetUp(); |
xmwmx | 0:7725181a2a09 | 122 | } |
xmwmx | 0:7725181a2a09 | 123 |