sads

Dependencies:   mbed

Fork of smart_drawer by Xiaoyuan Yu

Committer:
plao
Date:
Mon Jul 23 06:22:33 2018 +0000
Revision:
1:4ffc39e81583
Parent:
0:ef75cadf684d
dfs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Dennis_Yu 0:ef75cadf684d 1 #include "mbed.h"
plao 1:4ffc39e81583 2 #include "esp8266.h"
plao 1:4ffc39e81583 3 #include <cstdarg>
plao 1:4ffc39e81583 4 #include <cstring>
plao 1:4ffc39e81583 5 #include <stdint.h>
plao 1:4ffc39e81583 6
plao 1:4ffc39e81583 7
plao 1:4ffc39e81583 8
plao 1:4ffc39e81583 9
plao 1:4ffc39e81583 10
plao 1:4ffc39e81583 11
Dennis_Yu 0:ef75cadf684d 12
Dennis_Yu 0:ef75cadf684d 13 //DigitalOut myled(LED1);
Dennis_Yu 0:ef75cadf684d 14
Dennis_Yu 0:ef75cadf684d 15 //DigitalOut(PB_3);
Dennis_Yu 0:ef75cadf684d 16 //DIgitalOut(PB_12);
Dennis_Yu 0:ef75cadf684d 17
Dennis_Yu 0:ef75cadf684d 18 //DigitalOut(PA_12);
Dennis_Yu 0:ef75cadf684d 19 //DigitalOut(PA_11);
Dennis_Yu 0:ef75cadf684d 20 //DigitalOut(PB_3);
plao 1:4ffc39e81583 21 Serial ser2usb(PA_2, PA_3, 115200);
plao 1:4ffc39e81583 22 int state = 0;
plao 1:4ffc39e81583 23 void Open(int row, int col);
plao 1:4ffc39e81583 24
plao 1:4ffc39e81583 25
Dennis_Yu 0:ef75cadf684d 26
Dennis_Yu 0:ef75cadf684d 27 DigitalOut red[2]=
Dennis_Yu 0:ef75cadf684d 28 {
plao 1:4ffc39e81583 29 PB_13,PB_12,
Dennis_Yu 0:ef75cadf684d 30 };
Dennis_Yu 0:ef75cadf684d 31
Dennis_Yu 0:ef75cadf684d 32 DigitalOut black[3]=
Dennis_Yu 0:ef75cadf684d 33 {
Dennis_Yu 0:ef75cadf684d 34 PA_12,PA_11,PB_3,
Dennis_Yu 0:ef75cadf684d 35 };
Dennis_Yu 0:ef75cadf684d 36
Dennis_Yu 0:ef75cadf684d 37
Dennis_Yu 0:ef75cadf684d 38 /***************************************
Dennis_Yu 0:ef75cadf684d 39 The SetUp function initial all pins.
Dennis_Yu 0:ef75cadf684d 40 ****************************************/
plao 1:4ffc39e81583 41 void SetUp()
Dennis_Yu 0:ef75cadf684d 42 {
Dennis_Yu 0:ef75cadf684d 43 for(int i=0; i<2; i++)
Dennis_Yu 0:ef75cadf684d 44 {
plao 1:4ffc39e81583 45 red[i].write(0);
Dennis_Yu 0:ef75cadf684d 46 }
Dennis_Yu 0:ef75cadf684d 47
Dennis_Yu 0:ef75cadf684d 48 for(int i=0; i<3; i++)
Dennis_Yu 0:ef75cadf684d 49 {
plao 1:4ffc39e81583 50 black[i].write(0);
Dennis_Yu 0:ef75cadf684d 51 }
Dennis_Yu 0:ef75cadf684d 52
plao 1:4ffc39e81583 53
Dennis_Yu 0:ef75cadf684d 54 }
Dennis_Yu 0:ef75cadf684d 55
plao 1:4ffc39e81583 56 /**************************************/
plao 1:4ffc39e81583 57 int main()
Dennis_Yu 0:ef75cadf684d 58 {
plao 1:4ffc39e81583 59 SetUp();
plao 1:4ffc39e81583 60 wait(3);
plao 1:4ffc39e81583 61
plao 1:4ffc39e81583 62
plao 1:4ffc39e81583 63 ser2usb.printf("starting\r\n");
plao 1:4ffc39e81583 64
plao 1:4ffc39e81583 65 // 选定与 esp8266 相连接的串口,WiFi 名称和密码
plao 1:4ffc39e81583 66 Esp8266 client(PA_9, PA_10, "iot_b827eb8fb527", "7c02b50b"); // 参数分别为 TX pin / RX pin / SSID / Password
plao 1:4ffc39e81583 67
plao 1:4ffc39e81583 68 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
plao 1:4ffc39e81583 69 const char *sensors[][2] = {
plao 1:4ffc39e81583 70 "opendrawer", "",
plao 1:4ffc39e81583 71 NULL, NULL //最后一行以空指针作为结束标记
plao 1:4ffc39e81583 72 };
plao 1:4ffc39e81583 73
plao 1:4ffc39e81583 74 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
plao 1:4ffc39e81583 75 const char *actuators[][2] = {
plao 1:4ffc39e81583 76 "do", "int",
plao 1:4ffc39e81583 77 NULL, NULL //最后一行以空指针作为结束标记
plao 1:4ffc39e81583 78 };
plao 1:4ffc39e81583 79 ser2usb.printf("connecting...\r\n");
plao 1:4ffc39e81583 80
plao 1:4ffc39e81583 81 //连接到服务器
plao 1:4ffc39e81583 82 client.connect_mqtt_broker("192.168.12.1", "smart_drawer", sensors, actuators);
Dennis_Yu 0:ef75cadf684d 83
plao 1:4ffc39e81583 84 ser2usb.printf("Initialization done.\r\n");
plao 1:4ffc39e81583 85 char actuator_name[32], control_value[32];
plao 1:4ffc39e81583 86 Timer t; // 定时器用于计量发送传感器数据的时间
plao 1:4ffc39e81583 87 t.start();
plao 1:4ffc39e81583 88 client.publish_value("do", "drawerinit");
plao 1:4ffc39e81583 89 while (1)
Dennis_Yu 0:ef75cadf684d 90 {
plao 1:4ffc39e81583 91 //检查有没有收到新的执行器控制指令
plao 1:4ffc39e81583 92 if (client.get_control_cmd(actuator_name, control_value))
Dennis_Yu 0:ef75cadf684d 93 {
plao 1:4ffc39e81583 94 ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value);
plao 1:4ffc39e81583 95 //判断哪个执行器收到命令
plao 1:4ffc39e81583 96 state = atoi(control_value);
plao 1:4ffc39e81583 97 switch (state)
plao 1:4ffc39e81583 98 {
plao 1:4ffc39e81583 99 case 1: // white to black
plao 1:4ffc39e81583 100
plao 1:4ffc39e81583 101 Open(0,0);
plao 1:4ffc39e81583 102 wait(0.2);
plao 1:4ffc39e81583 103 client.publish_value("do", "drawerfinish");
plao 1:4ffc39e81583 104 break;
plao 1:4ffc39e81583 105 case 2: // white to black
plao 1:4ffc39e81583 106
plao 1:4ffc39e81583 107 Open(0,1);
plao 1:4ffc39e81583 108 wait(0.2);
plao 1:4ffc39e81583 109 client.publish_value("do", "drawerfinish");
plao 1:4ffc39e81583 110 break;
plao 1:4ffc39e81583 111 case 3: // white to black
plao 1:4ffc39e81583 112
plao 1:4ffc39e81583 113 Open(0,2);
plao 1:4ffc39e81583 114 wait(0.2);
plao 1:4ffc39e81583 115 client.publish_value("do", "drawerfinish");
plao 1:4ffc39e81583 116 break;
plao 1:4ffc39e81583 117 case 4: // white to black
plao 1:4ffc39e81583 118
plao 1:4ffc39e81583 119 Open(1,0);
plao 1:4ffc39e81583 120 wait(0.2);
plao 1:4ffc39e81583 121 client.publish_value("do", "drawerfinish");
plao 1:4ffc39e81583 122 break;
plao 1:4ffc39e81583 123 case 5: // white to black
plao 1:4ffc39e81583 124
plao 1:4ffc39e81583 125 Open(1,2);
plao 1:4ffc39e81583 126 wait(0.2);
plao 1:4ffc39e81583 127 client.publish_value("do", "drawerfinish");
plao 1:4ffc39e81583 128 break;
plao 1:4ffc39e81583 129 case 6: // white to black
plao 1:4ffc39e81583 130
plao 1:4ffc39e81583 131 Open(1,2);
plao 1:4ffc39e81583 132 wait(0.2);
plao 1:4ffc39e81583 133 client.publish_value("do", "drawerfinish");
plao 1:4ffc39e81583 134 break;
plao 1:4ffc39e81583 135 default:
plao 1:4ffc39e81583 136 break;
plao 1:4ffc39e81583 137 }
Dennis_Yu 0:ef75cadf684d 138 }
Dennis_Yu 0:ef75cadf684d 139 }
Dennis_Yu 0:ef75cadf684d 140 }
plao 1:4ffc39e81583 141
plao 1:4ffc39e81583 142
plao 1:4ffc39e81583 143
plao 1:4ffc39e81583 144 void Open(int row,int col)
plao 1:4ffc39e81583 145 {
plao 1:4ffc39e81583 146 red[row].write(1);
plao 1:4ffc39e81583 147 black[col].write(1);
plao 1:4ffc39e81583 148 wait(0.2);
plao 1:4ffc39e81583 149 SetUp();
plao 1:4ffc39e81583 150 }
plao 1:4ffc39e81583 151
plao 1:4ffc39e81583 152
plao 1:4ffc39e81583 153
plao 1:4ffc39e81583 154
plao 1:4ffc39e81583 155