init

Dependencies:   mbed

Fork of 仓库 by Keegan Hu

Committer:
zhangyx
Date:
Tue Nov 07 04:09:57 2017 +0000
Revision:
2:b10925c474fc
Parent:
1:3b487c4813a2
Child:
3:06080fa094a0
changes for IoT node

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zhangyx 1:3b487c4813a2 1 /***************************************************************
zhangyx 1:3b487c4813a2 2 功能 : ESP8266物联车接口函数
zhangyx 1:3b487c4813a2 3 作者 : 马晓健
zhangyx 1:3b487c4813a2 4 邮箱 : jeasinema[at]gmail[dot]com
zhangyx 1:3b487c4813a2 5 声明 :
zhangyx 1:3b487c4813a2 6 本程序仅供学习与交流使用,如需他用,须联系作者
zhangyx 1:3b487c4813a2 7 本程序可以随意更改,但须保留本信息页
zhangyx 1:3b487c4813a2 8 All rights reserved
zhangyx 1:3b487c4813a2 9 2017.6.16
zhangyx 1:3b487c4813a2 10 ***************************************************************/
zhangyx 1:3b487c4813a2 11
brainliang 0:63af4719467f 12 #include "esp8266.h"
brainliang 0:63af4719467f 13
brainliang 0:63af4719467f 14 #include <cstdarg>
brainliang 0:63af4719467f 15 #include <cstring>
brainliang 0:63af4719467f 16 #include "mbed.h"
brainliang 0:63af4719467f 17
brainliang 0:63af4719467f 18
brainliang 0:63af4719467f 19 //#define DEBUG
brainliang 0:63af4719467f 20
zhangyx 1:3b487c4813a2 21 static int ser_baud = 9600;
brainliang 0:63af4719467f 22
brainliang 0:63af4719467f 23 //定义了一个调试的宏,C语言语法
brainliang 0:63af4719467f 24 #define ESP_CMD(format, ...) do{\
brainliang 0:63af4719467f 25 ser2esp8266.printf("\r"); \
brainliang 0:63af4719467f 26 ser2esp8266.printf(format "\r", ##__VA_ARGS__);\
brainliang 0:63af4719467f 27 wait(0.3);\
brainliang 0:63af4719467f 28 }while(0)
brainliang 0:63af4719467f 29
brainliang 0:63af4719467f 30
brainliang 0:63af4719467f 31 #ifdef DEBUG
zhangyx 2:b10925c474fc 32 static void cb1() { //将USB串口上得到的消息通过WIFI模块发出去
zhangyx 1:3b487c4813a2 33 //ser2esp8266.putc(getchar());
brainliang 0:63af4719467f 34 }
brainliang 0:63af4719467f 35 #endif
brainliang 0:63af4719467f 36
zhangyx 2:b10925c474fc 37 static void simple_callback() { //将WIFI模块得到的消息通过USB串口打印到电脑上
zhangyx 1:3b487c4813a2 38 // putchar(ser2esp8266.getc());
brainliang 0:63af4719467f 39 }
brainliang 0:63af4719467f 40 //#endif
brainliang 0:63af4719467f 41
zhangyx 2:b10925c474fc 42 void Esp8266::gotResponse(char *token, char *param)
zhangyx 2:b10925c474fc 43 {
zhangyx 2:b10925c474fc 44 printf("gotResponse %s %s\r\n", token, param);
zhangyx 2:b10925c474fc 45 if(strcmp(token, "connected") == 0)
zhangyx 2:b10925c474fc 46 mqtt_start = true;
zhangyx 2:b10925c474fc 47 else if(strcmp(token, "wifi") == 0){
zhangyx 2:b10925c474fc 48 if(*param == '5')
zhangyx 2:b10925c474fc 49 network_start = true;
zhangyx 2:b10925c474fc 50 }
zhangyx 2:b10925c474fc 51 }
brainliang 0:63af4719467f 52
brainliang 0:63af4719467f 53 // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符
brainliang 0:63af4719467f 54 // 数据格式约定: #token+data
zhangyx 1:3b487c4813a2 55 void Esp8266::esp8266_rxCallback() {
brainliang 0:63af4719467f 56 char in = ser2esp8266.getc();
brainliang 0:63af4719467f 57 enum{STATE_WAIT, STATE_TOKEN, STATE_PARAM};
brainliang 0:63af4719467f 58 static uint8_t state = STATE_WAIT;
brainliang 0:63af4719467f 59 static int tokenLen, paramLen;
brainliang 0:63af4719467f 60 switch(state){
brainliang 0:63af4719467f 61 case STATE_WAIT:
brainliang 0:63af4719467f 62 if(in == '#'){
brainliang 0:63af4719467f 63 tokenLen = 0;
brainliang 0:63af4719467f 64 state = STATE_TOKEN;
brainliang 0:63af4719467f 65 }
brainliang 0:63af4719467f 66 break;
brainliang 0:63af4719467f 67 case STATE_TOKEN:
brainliang 0:63af4719467f 68 if(in == '+' || in == '\r' || in == '\n'){
brainliang 0:63af4719467f 69 esp_tokenBuf[tokenLen] = '\0';
brainliang 0:63af4719467f 70 if(in == '+'){
brainliang 0:63af4719467f 71 paramLen = 0;
brainliang 0:63af4719467f 72 state = STATE_PARAM;
brainliang 0:63af4719467f 73 }else{
zhangyx 2:b10925c474fc 74 gotResponse(esp_tokenBuf, NULL);
brainliang 0:63af4719467f 75 //memcpy(esp_token, esp_tokenBuf, tokenLen);
brainliang 0:63af4719467f 76 //esp_token[tokenLen] = '\0';
brainliang 0:63af4719467f 77 esp_buf_ready = true;
brainliang 0:63af4719467f 78 state = STATE_WAIT;
brainliang 0:63af4719467f 79 }
brainliang 0:63af4719467f 80 }else if(tokenLen+1 < sizeof(esp_tokenBuf)){
brainliang 0:63af4719467f 81 esp_tokenBuf[tokenLen++] = in;
brainliang 0:63af4719467f 82 }
brainliang 0:63af4719467f 83 break;
brainliang 0:63af4719467f 84 case STATE_PARAM:
brainliang 0:63af4719467f 85 if(in == '\r' || in == '\n'){
brainliang 0:63af4719467f 86 esp_paramBuf[paramLen] = '\0';
zhangyx 2:b10925c474fc 87 gotResponse(esp_tokenBuf, esp_paramBuf);
brainliang 0:63af4719467f 88 //memcpy(esp_token, esp_tokenBuf, tokenLen);
brainliang 0:63af4719467f 89 //memcpy(esp_param, esp_paramBuf, paramLen);
brainliang 0:63af4719467f 90 //esp_token[tokenLen] = '\0';
brainliang 0:63af4719467f 91 //esp_param[paramLen] = '\0';
brainliang 0:63af4719467f 92 //ser2usb.putc('?');
brainliang 0:63af4719467f 93 esp_buf_ready = true;
brainliang 0:63af4719467f 94 state = STATE_WAIT;
brainliang 0:63af4719467f 95 }else if(paramLen+1 < sizeof(esp_paramBuf)){
brainliang 0:63af4719467f 96 esp_paramBuf[paramLen++] = in;
brainliang 0:63af4719467f 97 }
brainliang 0:63af4719467f 98 break;
brainliang 0:63af4719467f 99 }
brainliang 0:63af4719467f 100 }
brainliang 0:63af4719467f 101
brainliang 0:63af4719467f 102
zhangyx 1:3b487c4813a2 103 Esp8266::Esp8266(PinName TX, PinName RX, const char *wifi_ssid, const char *wifi_passwd) //定义类的函数
zhangyx 1:3b487c4813a2 104 : network_start(false), mqtt_start(false), esp_buf_ready(false), ser2esp8266(TX, RX)
brainliang 0:63af4719467f 105 {
brainliang 0:63af4719467f 106 // serial to esp8266 init
brainliang 0:63af4719467f 107 ser2esp8266.baud(ser_baud);
brainliang 0:63af4719467f 108 #ifndef DEBUG
zhangyx 2:b10925c474fc 109 ser2esp8266.attach(callback(this,&Esp8266::esp8266_rxCallback), Serial::RxIrq);
brainliang 0:63af4719467f 110 #endif
zhangyx 1:3b487c4813a2 111 //if (mode == 0) { // client mode
brainliang 0:63af4719467f 112 this->reset();
zhangyx 1:3b487c4813a2 113 this->connect_wifi(wifi_ssid, wifi_passwd);
zhangyx 2:b10925c474fc 114 while(!is_connected()){
zhangyx 2:b10925c474fc 115 wait(0.5);
zhangyx 2:b10925c474fc 116 }
brainliang 0:63af4719467f 117 this->weblogin();
zhangyx 1:3b487c4813a2 118 //} else {
zhangyx 1:3b487c4813a2 119 //
zhangyx 1:3b487c4813a2 120 //}
brainliang 0:63af4719467f 121 }
brainliang 0:63af4719467f 122
brainliang 0:63af4719467f 123 bool Esp8266::reset() { //定义类的函数
brainliang 0:63af4719467f 124 ESP_CMD("node.restart()");
brainliang 0:63af4719467f 125 wait(2); // 延迟2s
brainliang 0:63af4719467f 126 return true;
brainliang 0:63af4719467f 127 }
brainliang 0:63af4719467f 128
zhangyx 1:3b487c4813a2 129 bool Esp8266::connect_wifi(const char *wifi_ssid, const char *wifi_passwd) { //定义类的函数
brainliang 0:63af4719467f 130 ESP_CMD("wifi.setmode(wifi.STATION)");
brainliang 0:63af4719467f 131 ESP_CMD("wifi.sta.config([[%s]],[[%s]])", wifi_ssid, wifi_passwd);
brainliang 0:63af4719467f 132 wait(2);
brainliang 0:63af4719467f 133 // set auto autoconnect
brainliang 0:63af4719467f 134 ESP_CMD("wifi.sta.autoconnect(1)");
zhangyx 2:b10925c474fc 135 return true;
zhangyx 2:b10925c474fc 136 }
zhangyx 2:b10925c474fc 137
zhangyx 2:b10925c474fc 138 bool Esp8266::is_connected()
zhangyx 2:b10925c474fc 139 {
brainliang 0:63af4719467f 140 ESP_CMD("print('\\035wifi+'..wifi.sta.status())");
zhangyx 2:b10925c474fc 141 wait(0.4);
zhangyx 2:b10925c474fc 142 return network_start;
brainliang 0:63af4719467f 143 }
brainliang 0:63af4719467f 144
brainliang 0:63af4719467f 145 bool Esp8266::weblogin() { //定义类的函数
brainliang 0:63af4719467f 146 // not implemented yet
brainliang 0:63af4719467f 147 return true;
brainliang 0:63af4719467f 148 }
brainliang 0:63af4719467f 149
brainliang 0:63af4719467f 150 bool Esp8266::connect_mqtt_broker(char *ip) { //定义类的函数
brainliang 0:63af4719467f 151 ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)");
zhangyx 2:b10925c474fc 152 ESP_CMD("m:connect(\"%s\",1883,0,function(conn)print (\"\\035connected\"); end)", ip);
zhangyx 2:b10925c474fc 153
zhangyx 2:b10925c474fc 154 do{
zhangyx 2:b10925c474fc 155 wait(0.5);
zhangyx 2:b10925c474fc 156 }while(!mqtt_start);
brainliang 0:63af4719467f 157 return true;
brainliang 0:63af4719467f 158 }
brainliang 0:63af4719467f 159
brainliang 0:63af4719467f 160 bool Esp8266::publish(char *topic, char *data, int size) { //定义类的函数
brainliang 0:63af4719467f 161 //if (mqtt_start) {
zhangyx 2:b10925c474fc 162 ESP_CMD("m:publish('/values/i_'..node.chipid()..'/%s',\"%s\",0,0)", topic, data);
brainliang 0:63af4719467f 163 //}
brainliang 0:63af4719467f 164 wait(0.1);
brainliang 0:63af4719467f 165 return true;
brainliang 0:63af4719467f 166 }
brainliang 0:63af4719467f 167
brainliang 0:63af4719467f 168 bool Esp8266::subscribe_poll(char *topic, char *data, int size) { //定义类的函数
brainliang 0:63af4719467f 169 //if (mqtt_start) {
brainliang 0:63af4719467f 170 ESP_CMD("m:subscribe(\"%s\", 0)", topic);
brainliang 0:63af4719467f 171 wait(0.1);
brainliang 0:63af4719467f 172 //}
brainliang 0:63af4719467f 173 ESP_CMD("m:on(\"message\", function(conn, topic, data)");
brainliang 0:63af4719467f 174 ESP_CMD("if topic == \"%s\" then", topic);
brainliang 0:63af4719467f 175 ESP_CMD("print(\"\\035\"..topic..\"+\"..data)");
brainliang 0:63af4719467f 176 ESP_CMD("end");
brainliang 0:63af4719467f 177 ESP_CMD("end)");
brainliang 0:63af4719467f 178
brainliang 0:63af4719467f 179 ESP_CMD("m:unsubscribe(\"%s\")", topic);
brainliang 0:63af4719467f 180 return true;
brainliang 0:63af4719467f 181 }