Kuo Yiching

Dependencies:   mbed

Fork of iot_example by YX ZHANG

Committer:
jasonkuo52
Date:
Wed Dec 20 08:46:09 2017 +0000
Revision:
8:cd472721e8a8
Parent:
7:49a823b5a935
Kuo Yiching

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);
jasonkuo52 8:cd472721e8a8 14 //DigitalOut LED(PD_2);
jasonkuo52 8:cd472721e8a8 15 //DigitalIn humansensor(PA_0);
jasonkuo52 8:cd472721e8a8 16 PwmOut mypwm19(PB_4);
jasonkuo52 8:cd472721e8a8 17 PwmOut mypwm20(PB_5);
jasonkuo52 8:cd472721e8a8 18 PwmOut mypwm21(PB_13);
jasonkuo52 8:cd472721e8a8 19 PwmOut mypwm22(PB_3);
jasonkuo52 8:cd472721e8a8 20 //DigitalIn mybutton(PA_12);
jasonkuo52 8:cd472721e8a8 21 DigitalOut myled(PB_14);
zhangyx 7:49a823b5a935 22
zhangyx 7:49a823b5a935 23 int test_main(void){
zhangyx 7:49a823b5a935 24 Serial esp8266(PA_9, PA_10, 9600);
zhangyx 7:49a823b5a935 25 while(1){
zhangyx 7:49a823b5a935 26 if(ser2usb.readable())
zhangyx 7:49a823b5a935 27 esp8266.putc(ser2usb.getc());
zhangyx 7:49a823b5a935 28 if(esp8266.readable())
zhangyx 7:49a823b5a935 29 ser2usb.putc(esp8266.getc());
zhangyx 7:49a823b5a935 30 }
zhangyx 7:49a823b5a935 31 }
brainliang 0:63af4719467f 32
brainliang 0:63af4719467f 33 int main(void) {
jasonkuo52 8:cd472721e8a8 34 myled = 0;
jasonkuo52 8:cd472721e8a8 35
zhangyx 1:3b487c4813a2 36 // 选定与 esp8266 相连接的串口,WiFi 名称和密码
zhangyx 1:3b487c4813a2 37 // 参数分别为 TX pin / RX pin / SSID / Password
jasonkuo52 8:cd472721e8a8 38 ser2usb.printf("Reset.\r\n");
zhangyx 7:49a823b5a935 39 Esp8266 client(PA_9, PA_10, "TSINGHUA.iCenter", "TS.icenter");
zyx 3:06080fa094a0 40
zyx 3:06080fa094a0 41 //声明所有的传感器,每行一个,每个由名字、单位两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 42 //服务器收到声明后,将在网页上显示这些传感器
zyx 3:06080fa094a0 43 const char* sensors[][2] = {
zhangyx 7:49a823b5a935 44 //"pressure", "Pa",
zhangyx 7:49a823b5a935 45 // "temperature", "C",
zhangyx 7:49a823b5a935 46 // "humidity", "%",
zhangyx 7:49a823b5a935 47 "huamn","person",
zyx 3:06080fa094a0 48 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 49 };
zyx 3:06080fa094a0 50
zyx 3:06080fa094a0 51 //声明所有的执行器,每行一个,每个由名字、参数类型两部分组成,最后一行必须为空指针作为结尾
zyx 3:06080fa094a0 52 //服务器收到声明后,将在网页上显示这些执行器
zyx 3:06080fa094a0 53 const char* actuators[][2] = {
jasonkuo52 8:cd472721e8a8 54 "servo19", "int",
jasonkuo52 8:cd472721e8a8 55 "servo20", "int",
jasonkuo52 8:cd472721e8a8 56 "servo21", "int",
jasonkuo52 8:cd472721e8a8 57 "servo22", "int",
jasonkuo52 8:cd472721e8a8 58 "LED01", "int",
jasonkuo52 8:cd472721e8a8 59 //"switch2", "int",
zyx 3:06080fa094a0 60 NULL, NULL //最后一行以空指针作为结束标记
zyx 3:06080fa094a0 61 };
zyx 3:06080fa094a0 62
zyx 3:06080fa094a0 63 //连接到服务器
zyx 3:06080fa094a0 64 client.connect_mqtt_broker("tdxls-iot.xicp.net", sensors, actuators);
zyx 3:06080fa094a0 65
brainliang 0:63af4719467f 66 ser2usb.printf("Initialization done.\r\n");
zyx 3:06080fa094a0 67
zyx 3:06080fa094a0 68 char actuator_name[32], control_value[32];
zyx 3:06080fa094a0 69 float last_report=0;
zyx 3:06080fa094a0 70 Timer t;// 定时器用于计量发送传感器数据的时间
zyx 3:06080fa094a0 71 t.start();
zyx 3:06080fa094a0 72
brainliang 0:63af4719467f 73 while(1) {
zyx 3:06080fa094a0 74 //检查有没有收到新的执行器控制指令
zyx 3:06080fa094a0 75 if(client.get_control_cmd(actuator_name, control_value)){
zyx 3:06080fa094a0 76 ser2usb.printf("Received CMD %s, %s\r\n", actuator_name, control_value);
zyx 3:06080fa094a0 77 //判断哪个执行器收到命令
jasonkuo52 8:cd472721e8a8 78 if (!strcmp(actuator_name, "servo19")){
jasonkuo52 8:cd472721e8a8 79 //LED = atoi(control_value);
jasonkuo52 8:cd472721e8a8 80 if (!strcmp(control_value, "1")){
jasonkuo52 8:cd472721e8a8 81 mypwm19.pulsewidth(0.001);
jasonkuo52 8:cd472721e8a8 82 }else{
jasonkuo52 8:cd472721e8a8 83 mypwm19.pulsewidth(0.002);
jasonkuo52 8:cd472721e8a8 84 }
jasonkuo52 8:cd472721e8a8 85
jasonkuo52 8:cd472721e8a8 86 }else if (!strcmp(actuator_name, "servo20")){
jasonkuo52 8:cd472721e8a8 87 if (!strcmp(control_value, "1")){
jasonkuo52 8:cd472721e8a8 88 mypwm20.pulsewidth(0.001);
jasonkuo52 8:cd472721e8a8 89 }else{
jasonkuo52 8:cd472721e8a8 90 mypwm20.pulsewidth(0.002);
jasonkuo52 8:cd472721e8a8 91 }
jasonkuo52 8:cd472721e8a8 92 }else if (!strcmp(actuator_name, "servo21")){
jasonkuo52 8:cd472721e8a8 93 if (!strcmp(control_value, "1")){
jasonkuo52 8:cd472721e8a8 94 mypwm21.pulsewidth(0.001);
jasonkuo52 8:cd472721e8a8 95 }else{
jasonkuo52 8:cd472721e8a8 96 mypwm21.pulsewidth(0.002);
jasonkuo52 8:cd472721e8a8 97 }
jasonkuo52 8:cd472721e8a8 98
jasonkuo52 8:cd472721e8a8 99 }else if (!strcmp(actuator_name, "servo22")){
jasonkuo52 8:cd472721e8a8 100 if (!strcmp(control_value, "1")){
jasonkuo52 8:cd472721e8a8 101 mypwm22.pulsewidth(0.001);
jasonkuo52 8:cd472721e8a8 102 }else{
jasonkuo52 8:cd472721e8a8 103 mypwm22.pulsewidth(0.002);
jasonkuo52 8:cd472721e8a8 104 }
zyx 3:06080fa094a0 105 //do something for switch2
jasonkuo52 8:cd472721e8a8 106 }else if (!strcmp(actuator_name, "LED01")){
jasonkuo52 8:cd472721e8a8 107 if (!strcmp(control_value, "1")){
jasonkuo52 8:cd472721e8a8 108 myled = 1;
jasonkuo52 8:cd472721e8a8 109 }else{
jasonkuo52 8:cd472721e8a8 110 myled = 0;
jasonkuo52 8:cd472721e8a8 111 }
zyx 3:06080fa094a0 112 }
jasonkuo52 8:cd472721e8a8 113 }else{
jasonkuo52 8:cd472721e8a8 114 //ser2usb.printf("Not Received CMD\r\n");
jasonkuo52 8:cd472721e8a8 115
zyx 3:06080fa094a0 116 }
zyx 3:06080fa094a0 117
jasonkuo52 8:cd472721e8a8 118 /*if(t.read() - last_report > 1){ // 每1s发送汇报一次传感器数据
zyx 3:06080fa094a0 119
zyx 3:06080fa094a0 120 //汇报传感器数据,两个参数分别是传感器名字和值
zhangyx 7:49a823b5a935 121 //client.publish_value("pressure", "100000 Pa");
zhangyx 7:49a823b5a935 122 // client.publish_value("temperature", "20 C");
zhangyx 7:49a823b5a935 123 // client.publish_value("humidity", "30 %");
zhangyx 7:49a823b5a935 124 client.publish_value("huamn", "30 %");
zhangyx 5:9a0acd11c445 125
zhangyx 5:9a0acd11c445 126 last_report = t.read();
jasonkuo52 8:cd472721e8a8 127 }*/
brainliang 0:63af4719467f 128 }
brainliang 0:63af4719467f 129 }