王铭轩 仓库
Fork of SBY_sw4stm32_nucleo_f103rb by
SBY/main.cpp@2:a779f481c401, 2018-08-02 (annotated)
- Committer:
- xmwmx
- Date:
- Thu Aug 02 13:02:11 2018 +0000
- Revision:
- 2:a779f481c401
- Parent:
- 1:4d8def597e29
- Child:
- 3:55a34662204f
warehouse
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 | 2:a779f481c401 | 8 | Serial ser2usb(PB_10, PB_11,115200); |
xmwmx | 0:7725181a2a09 | 9 | DigitalOut red[2]= |
xmwmx | 0:7725181a2a09 | 10 | { |
xmwmx | 0:7725181a2a09 | 11 | PB_13,PB_12, |
xmwmx | 0:7725181a2a09 | 12 | }; |
xmwmx | 0:7725181a2a09 | 13 | |
xmwmx | 0:7725181a2a09 | 14 | DigitalOut black[3]= |
xmwmx | 0:7725181a2a09 | 15 | { |
xmwmx | 0:7725181a2a09 | 16 | PA_12,PA_11,PB_3, |
xmwmx | 0:7725181a2a09 | 17 | }; |
xmwmx | 0:7725181a2a09 | 18 | |
xmwmx | 2:a779f481c401 | 19 | int state = 0; |
xmwmx | 2:a779f481c401 | 20 | int x[256]; |
xmwmx | 2:a779f481c401 | 21 | int y=0; |
xmwmx | 2:a779f481c401 | 22 | bool ready,received,start,done; |
xmwmx | 2:a779f481c401 | 23 | |
xmwmx | 2:a779f481c401 | 24 | void open(int row,int col); |
xmwmx | 2:a779f481c401 | 25 | void SetUp(); |
xmwmx | 2:a779f481c401 | 26 | |
xmwmx | 0:7725181a2a09 | 27 | |
xmwmx | 0:7725181a2a09 | 28 | int main() |
xmwmx | 0:7725181a2a09 | 29 | { |
xmwmx | 0:7725181a2a09 | 30 | SetUp(); |
xmwmx | 2:a779f481c401 | 31 | //wait(2); |
xmwmx | 1:4d8def597e29 | 32 | |
xmwmx | 0:7725181a2a09 | 33 | |
xmwmx | 0:7725181a2a09 | 34 | |
xmwmx | 2:a779f481c401 | 35 | ser2usb.printf("starting\r\n"); |
xmwmx | 0:7725181a2a09 | 36 | |
xmwmx | 0:7725181a2a09 | 37 | // 选定与 esp8266 相连接的串口,WiFi 名称和密码 |
xmwmx | 0:7725181a2a09 | 38 | Esp8266 client(PA_9, PA_10, "iot_b827eb8fb527", "7c02b50b");// 参数分别为 TX pin / RX pin / SSID / Password |
xmwmx | 0:7725181a2a09 | 39 | |
xmwmx | 0:7725181a2a09 | 40 | //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾 |
xmwmx | 0:7725181a2a09 | 41 | const char* sensors[][2] = { |
xmwmx | 2:a779f481c401 | 42 | "", "", |
xmwmx | 0:7725181a2a09 | 43 | NULL, NULL //最后一行以空指针作为结束标记 |
xmwmx | 0:7725181a2a09 | 44 | }; |
xmwmx | 0:7725181a2a09 | 45 | |
xmwmx | 0:7725181a2a09 | 46 | //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾 |
xmwmx | 0:7725181a2a09 | 47 | const char* actuators[][2] = { |
xmwmx | 2:a779f481c401 | 48 | "command", "", |
xmwmx | 2:a779f481c401 | 49 | "dat", "", |
xmwmx | 0:7725181a2a09 | 50 | NULL, NULL //最后一行以空指针作为结束标记 |
xmwmx | 0:7725181a2a09 | 51 | }; |
xmwmx | 2:a779f481c401 | 52 | ser2usb.printf("connecting...\r\n"); |
xmwmx | 0:7725181a2a09 | 53 | |
xmwmx | 0:7725181a2a09 | 54 | //连接到服务器 |
xmwmx | 2:a779f481c401 | 55 | client.connect_mqtt_broker("192.168.12.1", "warehouse", sensors, actuators); |
xmwmx | 0:7725181a2a09 | 56 | |
xmwmx | 2:a779f481c401 | 57 | client.subscribe_control(actuators[0][0]); |
xmwmx | 2:a779f481c401 | 58 | client.subscribe_control(actuators[1][0]); |
xmwmx | 2:a779f481c401 | 59 | |
xmwmx | 0:7725181a2a09 | 60 | ser2usb.printf("Initialization done.\r\n"); |
xmwmx | 0:7725181a2a09 | 61 | char actuator_name[32], control_value[32]; |
xmwmx | 0:7725181a2a09 | 62 | Timer t;// 定时器用于计量发送传感器数据的时间 |
xmwmx | 0:7725181a2a09 | 63 | t.start(); |
xmwmx | 2:a779f481c401 | 64 | //client.publish_value("report", "carinit"); |
xmwmx | 2:a779f481c401 | 65 | |
xmwmx | 2:a779f481c401 | 66 | |
xmwmx | 1:4d8def597e29 | 67 | |
xmwmx | 0:7725181a2a09 | 68 | while(1) |
xmwmx | 0:7725181a2a09 | 69 | { |
xmwmx | 2:a779f481c401 | 70 | |
xmwmx | 0:7725181a2a09 | 71 | //检查有没有收到新的执行器控制指令 |
xmwmx | 0:7725181a2a09 | 72 | if(client.get_control_cmd(actuator_name, control_value)) |
xmwmx | 0:7725181a2a09 | 73 | { |
xmwmx | 2:a779f481c401 | 74 | ser2usb.printf("Received %s %s\r\n", actuator_name, control_value); |
xmwmx | 2:a779f481c401 | 75 | if (strcmp(actuator_name, "command") == 0) |
xmwmx | 2:a779f481c401 | 76 | { |
xmwmx | 2:a779f481c401 | 77 | ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value); |
xmwmx | 2:a779f481c401 | 78 | int the_command = atoi(control_value); |
xmwmx | 2:a779f481c401 | 79 | // |
xmwmx | 2:a779f481c401 | 80 | if(the_command==1) |
xmwmx | 2:a779f481c401 | 81 | { |
xmwmx | 2:a779f481c401 | 82 | ready=1; |
xmwmx | 2:a779f481c401 | 83 | ser2usb.printf("get ready\r\n"); |
xmwmx | 2:a779f481c401 | 84 | } |
xmwmx | 2:a779f481c401 | 85 | else if(the_command==2) |
xmwmx | 2:a779f481c401 | 86 | { |
xmwmx | 2:a779f481c401 | 87 | start=1; |
xmwmx | 2:a779f481c401 | 88 | ser2usb.printf("get start\r\n"); |
xmwmx | 2:a779f481c401 | 89 | } |
xmwmx | 2:a779f481c401 | 90 | else if(the_command==3) |
xmwmx | 2:a779f481c401 | 91 | { |
xmwmx | 2:a779f481c401 | 92 | done=1; |
xmwmx | 2:a779f481c401 | 93 | ser2usb.printf("get done\r\n"); |
xmwmx | 2:a779f481c401 | 94 | } |
xmwmx | 2:a779f481c401 | 95 | // |
xmwmx | 2:a779f481c401 | 96 | |
xmwmx | 2:a779f481c401 | 97 | } |
xmwmx | 2:a779f481c401 | 98 | else if (strcmp(actuator_name, "dat") == 0) |
xmwmx | 2:a779f481c401 | 99 | { |
xmwmx | 2:a779f481c401 | 100 | int the_command = atoi(control_value); |
xmwmx | 2:a779f481c401 | 101 | if(the_command==7) |
xmwmx | 2:a779f481c401 | 102 | { |
xmwmx | 2:a779f481c401 | 103 | received=1; |
xmwmx | 2:a779f481c401 | 104 | ser2usb.printf("received 7\r\n"); |
xmwmx | 2:a779f481c401 | 105 | client.publish_value("report", "received."); |
xmwmx | 2:a779f481c401 | 106 | } |
xmwmx | 2:a779f481c401 | 107 | else |
xmwmx | 2:a779f481c401 | 108 | { |
xmwmx | 2:a779f481c401 | 109 | //received=1; |
xmwmx | 2:a779f481c401 | 110 | //client.publish_value("report", "received."); |
xmwmx | 2:a779f481c401 | 111 | x[y]=the_command; |
xmwmx | 2:a779f481c401 | 112 | y++; |
xmwmx | 2:a779f481c401 | 113 | ser2usb.printf("get %s|%d\r\n",control_value,y); |
xmwmx | 2:a779f481c401 | 114 | } |
xmwmx | 2:a779f481c401 | 115 | } |
xmwmx | 2:a779f481c401 | 116 | /* |
xmwmx | 0:7725181a2a09 | 117 | ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value); |
xmwmx | 0:7725181a2a09 | 118 | //判断哪个执行器收到命令 |
xmwmx | 0:7725181a2a09 | 119 | state = atoi(control_value); |
xmwmx | 0:7725181a2a09 | 120 | switch(state) |
xmwmx | 0:7725181a2a09 | 121 | { |
xmwmx | 0:7725181a2a09 | 122 | case 1: |
xmwmx | 0:7725181a2a09 | 123 | open(0,0); |
xmwmx | 2:a779f481c401 | 124 | client.publish_value("report", "opened"); |
xmwmx | 0:7725181a2a09 | 125 | break; |
xmwmx | 0:7725181a2a09 | 126 | case 2: |
xmwmx | 0:7725181a2a09 | 127 | open(0,1); |
xmwmx | 2:a779f481c401 | 128 | client.publish_value("report", "opened"); |
xmwmx | 0:7725181a2a09 | 129 | break; |
xmwmx | 0:7725181a2a09 | 130 | case 3: |
xmwmx | 0:7725181a2a09 | 131 | open(0,2); |
xmwmx | 2:a779f481c401 | 132 | client.publish_value("report", "opened"); |
xmwmx | 0:7725181a2a09 | 133 | break; |
xmwmx | 0:7725181a2a09 | 134 | case 4: |
xmwmx | 0:7725181a2a09 | 135 | open(1,0); |
xmwmx | 2:a779f481c401 | 136 | client.publish_value("report", "opened"); |
xmwmx | 0:7725181a2a09 | 137 | break; |
xmwmx | 0:7725181a2a09 | 138 | case 5: |
xmwmx | 0:7725181a2a09 | 139 | open(1,1); |
xmwmx | 2:a779f481c401 | 140 | client.publish_value("report", "opened"); |
xmwmx | 0:7725181a2a09 | 141 | break; |
xmwmx | 0:7725181a2a09 | 142 | case 6: |
xmwmx | 1:4d8def597e29 | 143 | open(1,2); |
xmwmx | 2:a779f481c401 | 144 | client.publish_value("report", "opened"); |
xmwmx | 0:7725181a2a09 | 145 | default: |
xmwmx | 0:7725181a2a09 | 146 | break; |
xmwmx | 0:7725181a2a09 | 147 | } |
xmwmx | 2:a779f481c401 | 148 | } |
xmwmx | 2:a779f481c401 | 149 | */ |
xmwmx | 1:4d8def597e29 | 150 | } |
xmwmx | 1:4d8def597e29 | 151 | |
xmwmx | 2:a779f481c401 | 152 | if(!ready) |
xmwmx | 2:a779f481c401 | 153 | { |
xmwmx | 2:a779f481c401 | 154 | client.publish_value( "report", "ready."); |
xmwmx | 2:a779f481c401 | 155 | //ser2usb.printf("send ready.%d\r\n",flagw); |
xmwmx | 2:a779f481c401 | 156 | ser2usb.printf("ready?\r\n"); |
xmwmx | 2:a779f481c401 | 157 | |
xmwmx | 2:a779f481c401 | 158 | } |
xmwmx | 2:a779f481c401 | 159 | if(!start&&ready) |
xmwmx | 2:a779f481c401 | 160 | { |
xmwmx | 2:a779f481c401 | 161 | ser2usb.printf("send start.\r\n"); |
xmwmx | 2:a779f481c401 | 162 | client.publish_value("report", "wait data."); |
xmwmx | 2:a779f481c401 | 163 | } |
xmwmx | 2:a779f481c401 | 164 | if(start) |
xmwmx | 2:a779f481c401 | 165 | { |
xmwmx | 2:a779f481c401 | 166 | for(int i=0;x[i];i++) |
xmwmx | 2:a779f481c401 | 167 | { |
xmwmx | 2:a779f481c401 | 168 | state = int(x[i]); |
xmwmx | 2:a779f481c401 | 169 | //state = y; |
xmwmx | 2:a779f481c401 | 170 | switch(state) |
xmwmx | 2:a779f481c401 | 171 | { |
xmwmx | 2:a779f481c401 | 172 | case 1: |
xmwmx | 2:a779f481c401 | 173 | open(0,0); |
xmwmx | 2:a779f481c401 | 174 | //client.publish_value("report", "opened"); |
xmwmx | 2:a779f481c401 | 175 | break; |
xmwmx | 2:a779f481c401 | 176 | case 2: |
xmwmx | 2:a779f481c401 | 177 | open(0,1); |
xmwmx | 2:a779f481c401 | 178 | //client.publish_value("report", "opened"); |
xmwmx | 2:a779f481c401 | 179 | break; |
xmwmx | 2:a779f481c401 | 180 | case 3: |
xmwmx | 2:a779f481c401 | 181 | open(0,2); |
xmwmx | 2:a779f481c401 | 182 | //client.publish_value("report", "opened"); |
xmwmx | 2:a779f481c401 | 183 | break; |
xmwmx | 2:a779f481c401 | 184 | case 4: |
xmwmx | 2:a779f481c401 | 185 | open(1,0); |
xmwmx | 2:a779f481c401 | 186 | //client.publish_value("report", "opened"); |
xmwmx | 2:a779f481c401 | 187 | break; |
xmwmx | 2:a779f481c401 | 188 | case 5: |
xmwmx | 2:a779f481c401 | 189 | open(1,1); |
xmwmx | 2:a779f481c401 | 190 | //client.publish_value("report", "opened"); |
xmwmx | 2:a779f481c401 | 191 | break; |
xmwmx | 2:a779f481c401 | 192 | case 6: |
xmwmx | 2:a779f481c401 | 193 | open(1,2); |
xmwmx | 2:a779f481c401 | 194 | //client.publish_value("report", "opened"); |
xmwmx | 2:a779f481c401 | 195 | default: |
xmwmx | 2:a779f481c401 | 196 | break; |
xmwmx | 2:a779f481c401 | 197 | } |
xmwmx | 2:a779f481c401 | 198 | } |
xmwmx | 2:a779f481c401 | 199 | client.publish_value("report", "done."); |
xmwmx | 2:a779f481c401 | 200 | memset(x,0,sizeof(x)); |
xmwmx | 2:a779f481c401 | 201 | y=0; |
xmwmx | 2:a779f481c401 | 202 | ready=0; |
xmwmx | 2:a779f481c401 | 203 | received=0; |
xmwmx | 2:a779f481c401 | 204 | start=0; |
xmwmx | 2:a779f481c401 | 205 | done=0; |
xmwmx | 2:a779f481c401 | 206 | } |
xmwmx | 1:4d8def597e29 | 207 | |
xmwmx | 2:a779f481c401 | 208 | } |
xmwmx | 0:7725181a2a09 | 209 | } |
xmwmx | 0:7725181a2a09 | 210 | |
xmwmx | 0:7725181a2a09 | 211 | void SetUp() |
xmwmx | 0:7725181a2a09 | 212 | { |
xmwmx | 0:7725181a2a09 | 213 | for(int i=0; i<2; i++) |
xmwmx | 0:7725181a2a09 | 214 | { |
xmwmx | 0:7725181a2a09 | 215 | red[i].write(0); |
xmwmx | 0:7725181a2a09 | 216 | } |
xmwmx | 0:7725181a2a09 | 217 | |
xmwmx | 0:7725181a2a09 | 218 | for(int i=0; i<3; i++) |
xmwmx | 0:7725181a2a09 | 219 | { |
xmwmx | 0:7725181a2a09 | 220 | black[i].write(0); |
xmwmx | 0:7725181a2a09 | 221 | } |
xmwmx | 0:7725181a2a09 | 222 | |
xmwmx | 0:7725181a2a09 | 223 | } |
xmwmx | 0:7725181a2a09 | 224 | void open(int row,int col) |
xmwmx | 0:7725181a2a09 | 225 | { |
xmwmx | 0:7725181a2a09 | 226 | red[row].write(1); |
xmwmx | 0:7725181a2a09 | 227 | black[col].write(1); |
xmwmx | 0:7725181a2a09 | 228 | wait(0.2); |
xmwmx | 0:7725181a2a09 | 229 | SetUp(); |
xmwmx | 0:7725181a2a09 | 230 | } |
xmwmx | 0:7725181a2a09 | 231 |