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