王铭轩 仓库

Dependencies:   mbed

Fork of SBY_sw4stm32_nucleo_f103rb by w mx

Committer:
xmwmx
Date:
Fri Aug 03 06:22:21 2018 +0000
Revision:
3:55a34662204f
Parent:
2:a779f481c401
?????

Who changed what in which revision?

UserRevisionLine numberNew 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 3:55a34662204f 101 received=1;
xmwmx 3:55a34662204f 102 ser2usb.printf("received \r\n");
xmwmx 3:55a34662204f 103 x[y]=the_command;
xmwmx 3:55a34662204f 104 y++;
xmwmx 3:55a34662204f 105 ser2usb.printf("get %s|%d\r\n",control_value,y);
xmwmx 3:55a34662204f 106 client.publish_value("report", "received.");
xmwmx 3:55a34662204f 107 /*
xmwmx 2:a779f481c401 108 else
xmwmx 2:a779f481c401 109 {
xmwmx 2:a779f481c401 110 //received=1;
xmwmx 2:a779f481c401 111 //client.publish_value("report", "received.");
xmwmx 2:a779f481c401 112 x[y]=the_command;
xmwmx 2:a779f481c401 113 y++;
xmwmx 2:a779f481c401 114 ser2usb.printf("get %s|%d\r\n",control_value,y);
xmwmx 2:a779f481c401 115 }
xmwmx 3:55a34662204f 116 */
xmwmx 2:a779f481c401 117 }
xmwmx 2:a779f481c401 118 /*
xmwmx 0:7725181a2a09 119 ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value);
xmwmx 0:7725181a2a09 120 //判断哪个执行器收到命令
xmwmx 0:7725181a2a09 121 state = atoi(control_value);
xmwmx 0:7725181a2a09 122 switch(state)
xmwmx 0:7725181a2a09 123 {
xmwmx 0:7725181a2a09 124 case 1:
xmwmx 0:7725181a2a09 125 open(0,0);
xmwmx 2:a779f481c401 126 client.publish_value("report", "opened");
xmwmx 0:7725181a2a09 127 break;
xmwmx 0:7725181a2a09 128 case 2:
xmwmx 0:7725181a2a09 129 open(0,1);
xmwmx 2:a779f481c401 130 client.publish_value("report", "opened");
xmwmx 0:7725181a2a09 131 break;
xmwmx 0:7725181a2a09 132 case 3:
xmwmx 0:7725181a2a09 133 open(0,2);
xmwmx 2:a779f481c401 134 client.publish_value("report", "opened");
xmwmx 0:7725181a2a09 135 break;
xmwmx 0:7725181a2a09 136 case 4:
xmwmx 0:7725181a2a09 137 open(1,0);
xmwmx 2:a779f481c401 138 client.publish_value("report", "opened");
xmwmx 0:7725181a2a09 139 break;
xmwmx 0:7725181a2a09 140 case 5:
xmwmx 0:7725181a2a09 141 open(1,1);
xmwmx 2:a779f481c401 142 client.publish_value("report", "opened");
xmwmx 0:7725181a2a09 143 break;
xmwmx 0:7725181a2a09 144 case 6:
xmwmx 1:4d8def597e29 145 open(1,2);
xmwmx 2:a779f481c401 146 client.publish_value("report", "opened");
xmwmx 0:7725181a2a09 147 default:
xmwmx 0:7725181a2a09 148 break;
xmwmx 0:7725181a2a09 149 }
xmwmx 2:a779f481c401 150 }
xmwmx 2:a779f481c401 151 */
xmwmx 1:4d8def597e29 152 }
xmwmx 1:4d8def597e29 153
xmwmx 2:a779f481c401 154 if(!ready)
xmwmx 2:a779f481c401 155 {
xmwmx 2:a779f481c401 156 client.publish_value( "report", "ready.");
xmwmx 2:a779f481c401 157 //ser2usb.printf("send ready.%d\r\n",flagw);
xmwmx 2:a779f481c401 158 ser2usb.printf("ready?\r\n");
xmwmx 2:a779f481c401 159
xmwmx 2:a779f481c401 160 }
xmwmx 3:55a34662204f 161 if(!start&&ready&&!received)
xmwmx 2:a779f481c401 162 {
xmwmx 2:a779f481c401 163 ser2usb.printf("send start.\r\n");
xmwmx 2:a779f481c401 164 client.publish_value("report", "wait data.");
xmwmx 2:a779f481c401 165 }
xmwmx 2:a779f481c401 166 if(start)
xmwmx 2:a779f481c401 167 {
xmwmx 3:55a34662204f 168 ser2usb.printf("starting\r\n");
xmwmx 2:a779f481c401 169 for(int i=0;x[i];i++)
xmwmx 2:a779f481c401 170 {
xmwmx 3:55a34662204f 171 state = x[i];
xmwmx 2:a779f481c401 172 //state = y;
xmwmx 3:55a34662204f 173 ser2usb.printf("starting %d\r\n",state);
xmwmx 2:a779f481c401 174 switch(state)
xmwmx 2:a779f481c401 175 {
xmwmx 2:a779f481c401 176 case 1:
xmwmx 2:a779f481c401 177 open(0,0);
xmwmx 2:a779f481c401 178 //client.publish_value("report", "opened");
xmwmx 2:a779f481c401 179 break;
xmwmx 2:a779f481c401 180 case 2:
xmwmx 2:a779f481c401 181 open(0,1);
xmwmx 2:a779f481c401 182 //client.publish_value("report", "opened");
xmwmx 2:a779f481c401 183 break;
xmwmx 2:a779f481c401 184 case 3:
xmwmx 2:a779f481c401 185 open(0,2);
xmwmx 2:a779f481c401 186 //client.publish_value("report", "opened");
xmwmx 2:a779f481c401 187 break;
xmwmx 2:a779f481c401 188 case 4:
xmwmx 2:a779f481c401 189 open(1,0);
xmwmx 2:a779f481c401 190 //client.publish_value("report", "opened");
xmwmx 2:a779f481c401 191 break;
xmwmx 2:a779f481c401 192 case 5:
xmwmx 2:a779f481c401 193 open(1,1);
xmwmx 2:a779f481c401 194 //client.publish_value("report", "opened");
xmwmx 2:a779f481c401 195 break;
xmwmx 2:a779f481c401 196 case 6:
xmwmx 2:a779f481c401 197 open(1,2);
xmwmx 2:a779f481c401 198 //client.publish_value("report", "opened");
xmwmx 2:a779f481c401 199 default:
xmwmx 2:a779f481c401 200 break;
xmwmx 2:a779f481c401 201 }
xmwmx 2:a779f481c401 202 }
xmwmx 2:a779f481c401 203 client.publish_value("report", "done.");
xmwmx 2:a779f481c401 204 memset(x,0,sizeof(x));
xmwmx 2:a779f481c401 205 y=0;
xmwmx 2:a779f481c401 206 ready=0;
xmwmx 2:a779f481c401 207 received=0;
xmwmx 2:a779f481c401 208 start=0;
xmwmx 2:a779f481c401 209 done=0;
xmwmx 2:a779f481c401 210 }
xmwmx 3:55a34662204f 211 wait(0.01);
xmwmx 2:a779f481c401 212 }
xmwmx 0:7725181a2a09 213 }
xmwmx 0:7725181a2a09 214
xmwmx 0:7725181a2a09 215 void SetUp()
xmwmx 0:7725181a2a09 216 {
xmwmx 0:7725181a2a09 217 for(int i=0; i<2; i++)
xmwmx 0:7725181a2a09 218 {
xmwmx 0:7725181a2a09 219 red[i].write(0);
xmwmx 0:7725181a2a09 220 }
xmwmx 0:7725181a2a09 221
xmwmx 0:7725181a2a09 222 for(int i=0; i<3; i++)
xmwmx 0:7725181a2a09 223 {
xmwmx 0:7725181a2a09 224 black[i].write(0);
xmwmx 0:7725181a2a09 225 }
xmwmx 0:7725181a2a09 226
xmwmx 0:7725181a2a09 227 }
xmwmx 0:7725181a2a09 228 void open(int row,int col)
xmwmx 0:7725181a2a09 229 {
xmwmx 0:7725181a2a09 230 red[row].write(1);
xmwmx 0:7725181a2a09 231 black[col].write(1);
xmwmx 0:7725181a2a09 232 wait(0.2);
xmwmx 0:7725181a2a09 233 SetUp();
xmwmx 0:7725181a2a09 234 }
xmwmx 0:7725181a2a09 235