init

Dependencies:   mbed

Fork of 仓库 by Keegan Hu

Committer:
zhangyx
Date:
Wed Nov 15 06:12:24 2017 +0000
Revision:
5:9a0acd11c445
Parent:
4:46e5e96a6233
Child:
7:49a823b5a935
restore timer value

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
zyx 3:06080fa094a0 13 Serial ser2usb(PA_9, PA_10, 115200);
zyx 3:06080fa094a0 14 DigitalOut LED(PD_2);
brainliang 0:63af4719467f 15
brainliang 0:63af4719467f 16 int main(void) {
zhangyx 1:3b487c4813a2 17 // 选定与 esp8266 相连接的串口,WiFi 名称和密码
zhangyx 1:3b487c4813a2 18 // 参数分别为 TX pin / RX pin / SSID / Password
zyx 4:46e5e96a6233 19 Esp8266 client(PA_2, PA_3, "TSINGHUA.iCenter", "TS.icenter");
zyx 3:06080fa094a0 20
zyx 3:06080fa094a0 21 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 22 //服务器收到声明后,将在网页上显示这些传感器
zyx 3:06080fa094a0 23 const char* sensors[][2] = {
zyx 3:06080fa094a0 24 "pressure", "Pa",
zyx 3:06080fa094a0 25 "temperature", "C",
zyx 3:06080fa094a0 26 "humidity", "%",
zyx 3:06080fa094a0 27 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 28 };
zyx 3:06080fa094a0 29
zyx 3:06080fa094a0 30 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 31 //服务器收到声明后,将在网页上显示这些执行器
zyx 3:06080fa094a0 32 const char* actuators[][2] = {
zhangyx 5:9a0acd11c445 33 "servo1", "int",
zyx 3:06080fa094a0 34 "switch2", "int",
zyx 3:06080fa094a0 35 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 36 };
zyx 3:06080fa094a0 37
zyx 3:06080fa094a0 38 //连接到服务器
zyx 3:06080fa094a0 39 client.connect_mqtt_broker("tdxls-iot.xicp.net", sensors, actuators);
zyx 3:06080fa094a0 40
brainliang 0:63af4719467f 41 ser2usb.printf("Initialization done.\r\n");
zyx 3:06080fa094a0 42
zyx 3:06080fa094a0 43 char actuator_name[32], control_value[32];
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)){
zyx 3:06080fa094a0 51 ser2usb.printf("Received CMD %s, %s\r\n", actuator_name, control_value);
zyx 3:06080fa094a0 52 //判断哪个执行器收到命令
zyx 3:06080fa094a0 53 if(strcmp(actuator_name, "switch1")){
zyx 3:06080fa094a0 54 //do something for switch1
zyx 3:06080fa094a0 55 LED = atoi(control_value);
zyx 3:06080fa094a0 56 }else if(strcmp(actuator_name, "switch2")){
zyx 3:06080fa094a0 57 //do something for switch2
zyx 3:06080fa094a0 58 }
zyx 3:06080fa094a0 59 }
zyx 3:06080fa094a0 60
zyx 3:06080fa094a0 61 if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据
zyx 3:06080fa094a0 62
zyx 3:06080fa094a0 63 //汇报传感器数据,两个参数分别是传感器名字和值
zyx 3:06080fa094a0 64 client.publish_value("pressure", "100000 Pa");
zyx 3:06080fa094a0 65 client.publish_value("temperature", "20 C");
zyx 3:06080fa094a0 66 client.publish_value("humidity", "30 %");
zhangyx 5:9a0acd11c445 67
zhangyx 5:9a0acd11c445 68 last_report = t.read();
zyx 3:06080fa094a0 69 }
brainliang 0:63af4719467f 70 }
brainliang 0:63af4719467f 71 }