init

Dependencies:   mbed

Fork of 仓库 by Keegan Hu

Committer:
zhangyx
Date:
Mon Mar 19 03:32:21 2018 +0000
Revision:
8:d5e340a59c71
Parent:
7:49a823b5a935
Child:
9:238780dda8af
demo with pi;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brainliang 0:63af4719467f 1 /*-----------------------------------------------------
brainliang 0:63af4719467f 2 File Name : main.cpp
brainliang 0:63af4719467f 3 Purpose : For esp8266 mbed porting
brainliang 0:63af4719467f 4 Creation Date : 22-06-2017
brainliang 0:63af4719467f 5 Last Modified :
brainliang 0:63af4719467f 6 Created By : Jeasine Ma [jeasinema[at]gmail[dot]com]
brainliang 0:63af4719467f 7 -----------------------------------------------------*/
brainliang 0:63af4719467f 8 #include <cstdarg>
brainliang 0:63af4719467f 9 #include <cstring>
brainliang 0:63af4719467f 10 #include "mbed.h"
brainliang 0:63af4719467f 11 #include "esp8266.h"
brainliang 0:63af4719467f 12
zhangyx 7:49a823b5a935 13 Serial ser2usb(PA_2, PA_3, 115200);
zhangyx 8:d5e340a59c71 14 DigitalIn ref(PA_12);
zhangyx 8:d5e340a59c71 15 DigitalOut LED(PC_13);
brainliang 0:63af4719467f 16
brainliang 0:63af4719467f 17 int main(void) {
zhangyx 8:d5e340a59c71 18 ser2usb.printf("starting\r\n");
zhangyx 7:49a823b5a935 19
zhangyx 1:3b487c4813a2 20 // 选定与 esp8266 相连接的串口,WiFi 名称和密码
zhangyx 8:d5e340a59c71 21 Esp8266 client(PA_9, PA_10, "iot_b827eb91bf24", "b827ebc4ea71");// 参数分别为 TX pin / RX pin / SSID / Password
zyx 3:06080fa094a0 22
zyx 3:06080fa094a0 23 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 24 const char* sensors[][2] = {
zhangyx 8:d5e340a59c71 25 "light", "lx",
zhangyx 8:d5e340a59c71 26 "reflection"," ",
zyx 3:06080fa094a0 27 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 28 };
zyx 3:06080fa094a0 29
zyx 3:06080fa094a0 30 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 31 const char* actuators[][2] = {
zhangyx 8:d5e340a59c71 32 "led", "int",
zyx 3:06080fa094a0 33 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 34 };
zhangyx 8:d5e340a59c71 35 ser2usb.printf("connecting...\r\n");
zyx 3:06080fa094a0 36
zyx 3:06080fa094a0 37 //连接到服务器
zhangyx 8:d5e340a59c71 38 client.connect_mqtt_broker("192.168.12.1", "alpha", sensors, actuators);
zyx 3:06080fa094a0 39
brainliang 0:63af4719467f 40 ser2usb.printf("Initialization done.\r\n");
zyx 3:06080fa094a0 41
zyx 3:06080fa094a0 42 char actuator_name[32], control_value[32];
zhangyx 8:d5e340a59c71 43 bool ref_last;
zyx 3:06080fa094a0 44 float last_report=0;
zyx 3:06080fa094a0 45 Timer t;// 定时器用于计量发送传感器数据的时间
zyx 3:06080fa094a0 46 t.start();
zyx 3:06080fa094a0 47
brainliang 0:63af4719467f 48 while(1) {
zyx 3:06080fa094a0 49 //检查有没有收到新的执行器控制指令
zyx 3:06080fa094a0 50 if(client.get_control_cmd(actuator_name, control_value)){
zhangyx 8:d5e340a59c71 51 ser2usb.printf("Received CMD %s %s\r\n", actuator_name, control_value);
zyx 3:06080fa094a0 52 //判断哪个执行器收到命令
zhangyx 8:d5e340a59c71 53 if(strcmp(actuator_name, "led")==0){
zyx 3:06080fa094a0 54 LED = atoi(control_value);
zyx 3:06080fa094a0 55 }
zyx 3:06080fa094a0 56 }
zhangyx 8:d5e340a59c71 57
zhangyx 8:d5e340a59c71 58 bool reflection = ref;
zhangyx 8:d5e340a59c71 59 if(reflection != ref_last){ //仅在传感器发生变化时汇报数据
zhangyx 8:d5e340a59c71 60
zhangyx 8:d5e340a59c71 61 ref_last = reflection;
zhangyx 8:d5e340a59c71 62
zhangyx 8:d5e340a59c71 63 char val[4];
zhangyx 8:d5e340a59c71 64 sprintf(val, "%d", (int)reflection);
zhangyx 8:d5e340a59c71 65
zhangyx 8:d5e340a59c71 66 //汇报传感器数据,两个参数分别是传感器名字和值
zhangyx 8:d5e340a59c71 67 client.publish_value("reflection", val);
zhangyx 8:d5e340a59c71 68 }
zyx 3:06080fa094a0 69
zyx 3:06080fa094a0 70 if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据
zyx 3:06080fa094a0 71
zyx 3:06080fa094a0 72 //汇报传感器数据,两个参数分别是传感器名字和值
zhangyx 8:d5e340a59c71 73 //client.publish_value("light", "30 lx");
zhangyx 5:9a0acd11c445 74
zhangyx 5:9a0acd11c445 75 last_report = t.read();
zyx 3:06080fa094a0 76 }
brainliang 0:63af4719467f 77 }
brainliang 0:63af4719467f 78 }