init

Dependencies:   mbed

Fork of iot_example by YX ZHANG

Committer:
zyx
Date:
Mon Nov 13 20:25:48 2017 +0800
Revision:
3:06080fa094a0
Parent:
2:b10925c474fc
Child:
4:46e5e96a6233
complete demo for IoT

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 3:06080fa094a0 19 // Esp8266 client(PA_2, PA_3, "TSINGHUA.iCenter", "TS.icenter");
zyx 3:06080fa094a0 20 Esp8266 client(PA_2, PA_3, "cqtest_mi", "wifipassword1611");
zyx 3:06080fa094a0 21
zyx 3:06080fa094a0 22 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 23 //服务器收到声明后,将在网页上显示这些传感器
zyx 3:06080fa094a0 24 const char* sensors[][2] = {
zyx 3:06080fa094a0 25 "pressure", "Pa",
zyx 3:06080fa094a0 26 "temperature", "C",
zyx 3:06080fa094a0 27 "humidity", "%",
zyx 3:06080fa094a0 28 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 29 };
zyx 3:06080fa094a0 30
zyx 3:06080fa094a0 31 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 32 //服务器收到声明后,将在网页上显示这些执行器
zyx 3:06080fa094a0 33 const char* actuators[][2] = {
zyx 3:06080fa094a0 34 "switch1", "int",
zyx 3:06080fa094a0 35 "switch2", "int",
zyx 3:06080fa094a0 36 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 37 };
zyx 3:06080fa094a0 38
zyx 3:06080fa094a0 39 //连接到服务器
zyx 3:06080fa094a0 40 client.connect_mqtt_broker("tdxls-iot.xicp.net", sensors, actuators);
zyx 3:06080fa094a0 41
brainliang 0:63af4719467f 42 ser2usb.printf("Initialization done.\r\n");
zyx 3:06080fa094a0 43
zyx 3:06080fa094a0 44 char actuator_name[32], control_value[32];
zyx 3:06080fa094a0 45 float last_report=0;
zyx 3:06080fa094a0 46 Timer t;// 定时器用于计量发送传感器数据的时间
zyx 3:06080fa094a0 47 t.start();
zyx 3:06080fa094a0 48
brainliang 0:63af4719467f 49 while(1) {
zyx 3:06080fa094a0 50 //检查有没有收到新的执行器控制指令
zyx 3:06080fa094a0 51 if(client.get_control_cmd(actuator_name, control_value)){
zyx 3:06080fa094a0 52 ser2usb.printf("Received CMD %s, %s\r\n", actuator_name, control_value);
zyx 3:06080fa094a0 53 //判断哪个执行器收到命令
zyx 3:06080fa094a0 54 if(strcmp(actuator_name, "switch1")){
zyx 3:06080fa094a0 55 //do something for switch1
zyx 3:06080fa094a0 56 LED = atoi(control_value);
zyx 3:06080fa094a0 57 }else if(strcmp(actuator_name, "switch2")){
zyx 3:06080fa094a0 58 //do something for switch2
zyx 3:06080fa094a0 59 }
zyx 3:06080fa094a0 60 }
zyx 3:06080fa094a0 61
zyx 3:06080fa094a0 62 if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据
zyx 3:06080fa094a0 63
zyx 3:06080fa094a0 64 //汇报传感器数据,两个参数分别是传感器名字和值
zyx 3:06080fa094a0 65 client.publish_value("pressure", "100000 Pa");
zyx 3:06080fa094a0 66 client.publish_value("temperature", "20 C");
zyx 3:06080fa094a0 67 client.publish_value("humidity", "30 %");
zyx 3:06080fa094a0 68 }
brainliang 0:63af4719467f 69 }
brainliang 0:63af4719467f 70 }