init

Dependencies:   mbed

Fork of iot_example by YX ZHANG

Committer:
zhangyx
Date:
Tue Nov 07 03:15:44 2017 +0000
Revision:
1:3b487c4813a2
Parent:
0:63af4719467f
Child:
2:b10925c474fc
new class interface

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
brainliang 0:63af4719467f 32 void cb1() { //将USB串口上得到的消息通过WIFI模块发出去
zhangyx 1:3b487c4813a2 33 //ser2esp8266.putc(getchar());
brainliang 0:63af4719467f 34 }
brainliang 0:63af4719467f 35 #endif
brainliang 0:63af4719467f 36
brainliang 0:63af4719467f 37 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
brainliang 0:63af4719467f 42
brainliang 0:63af4719467f 43 // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符
brainliang 0:63af4719467f 44 // 数据格式约定: #token+data
zhangyx 1:3b487c4813a2 45 void Esp8266::esp8266_rxCallback() {
brainliang 0:63af4719467f 46 char in = ser2esp8266.getc();
brainliang 0:63af4719467f 47 enum{STATE_WAIT, STATE_TOKEN, STATE_PARAM};
brainliang 0:63af4719467f 48 static uint8_t state = STATE_WAIT;
brainliang 0:63af4719467f 49 static int tokenLen, paramLen;
brainliang 0:63af4719467f 50 switch(state){
brainliang 0:63af4719467f 51 case STATE_WAIT:
brainliang 0:63af4719467f 52 if(in == '#'){
brainliang 0:63af4719467f 53 tokenLen = 0;
brainliang 0:63af4719467f 54 state = STATE_TOKEN;
brainliang 0:63af4719467f 55 }
brainliang 0:63af4719467f 56 break;
brainliang 0:63af4719467f 57 case STATE_TOKEN:
brainliang 0:63af4719467f 58 if(in == '+' || in == '\r' || in == '\n'){
brainliang 0:63af4719467f 59 esp_tokenBuf[tokenLen] = '\0';
brainliang 0:63af4719467f 60 if(in == '+'){
brainliang 0:63af4719467f 61 paramLen = 0;
brainliang 0:63af4719467f 62 state = STATE_PARAM;
brainliang 0:63af4719467f 63 }else{
brainliang 0:63af4719467f 64 //gotResponse(tokenBuf, NULL);
brainliang 0:63af4719467f 65 //memcpy(esp_token, esp_tokenBuf, tokenLen);
brainliang 0:63af4719467f 66 //esp_token[tokenLen] = '\0';
brainliang 0:63af4719467f 67 esp_buf_ready = true;
brainliang 0:63af4719467f 68 state = STATE_WAIT;
brainliang 0:63af4719467f 69 }
brainliang 0:63af4719467f 70 }else if(tokenLen+1 < sizeof(esp_tokenBuf)){
brainliang 0:63af4719467f 71 esp_tokenBuf[tokenLen++] = in;
brainliang 0:63af4719467f 72 }
brainliang 0:63af4719467f 73 break;
brainliang 0:63af4719467f 74 case STATE_PARAM:
brainliang 0:63af4719467f 75 if(in == '\r' || in == '\n'){
brainliang 0:63af4719467f 76 esp_paramBuf[paramLen] = '\0';
brainliang 0:63af4719467f 77 //gotResponse(tokenBuf, paramBuf);
brainliang 0:63af4719467f 78 //memcpy(esp_token, esp_tokenBuf, tokenLen);
brainliang 0:63af4719467f 79 //memcpy(esp_param, esp_paramBuf, paramLen);
brainliang 0:63af4719467f 80 //esp_token[tokenLen] = '\0';
brainliang 0:63af4719467f 81 //esp_param[paramLen] = '\0';
brainliang 0:63af4719467f 82 //ser2usb.putc('?');
brainliang 0:63af4719467f 83 esp_buf_ready = true;
brainliang 0:63af4719467f 84 state = STATE_WAIT;
brainliang 0:63af4719467f 85 return;
brainliang 0:63af4719467f 86 }else if(paramLen+1 < sizeof(esp_paramBuf)){
brainliang 0:63af4719467f 87 esp_paramBuf[paramLen++] = in;
brainliang 0:63af4719467f 88 }
brainliang 0:63af4719467f 89 break;
brainliang 0:63af4719467f 90 }
brainliang 0:63af4719467f 91 }
brainliang 0:63af4719467f 92
brainliang 0:63af4719467f 93
zhangyx 1:3b487c4813a2 94 Esp8266::Esp8266(PinName TX, PinName RX, const char *wifi_ssid, const char *wifi_passwd) //定义类的函数
zhangyx 1:3b487c4813a2 95 : network_start(false), mqtt_start(false), esp_buf_ready(false), ser2esp8266(TX, RX)
brainliang 0:63af4719467f 96 {
brainliang 0:63af4719467f 97 // serial to esp8266 init
brainliang 0:63af4719467f 98 ser2esp8266.baud(ser_baud);
brainliang 0:63af4719467f 99 #ifndef DEBUG
brainliang 0:63af4719467f 100 //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq);
brainliang 0:63af4719467f 101 #endif
zhangyx 1:3b487c4813a2 102 //if (mode == 0) { // client mode
brainliang 0:63af4719467f 103 this->reset();
zhangyx 1:3b487c4813a2 104 this->connect_wifi(wifi_ssid, wifi_passwd);
brainliang 0:63af4719467f 105 this->weblogin();
zhangyx 1:3b487c4813a2 106 //} else {
zhangyx 1:3b487c4813a2 107 //
zhangyx 1:3b487c4813a2 108 //}
brainliang 0:63af4719467f 109 }
brainliang 0:63af4719467f 110
brainliang 0:63af4719467f 111 bool Esp8266::reset() { //定义类的函数
brainliang 0:63af4719467f 112 ESP_CMD("node.restart()");
brainliang 0:63af4719467f 113 wait(2); // 延迟2s
brainliang 0:63af4719467f 114 return true;
brainliang 0:63af4719467f 115 }
brainliang 0:63af4719467f 116
zhangyx 1:3b487c4813a2 117 bool Esp8266::connect_wifi(const char *wifi_ssid, const char *wifi_passwd) { //定义类的函数
brainliang 0:63af4719467f 118 ESP_CMD("wifi.setmode(wifi.STATION)");
brainliang 0:63af4719467f 119 ESP_CMD("wifi.sta.config([[%s]],[[%s]])", wifi_ssid, wifi_passwd);
brainliang 0:63af4719467f 120 wait(2);
brainliang 0:63af4719467f 121 // set auto autoconnect
brainliang 0:63af4719467f 122 ESP_CMD("wifi.sta.autoconnect(1)");
brainliang 0:63af4719467f 123 ESP_CMD("print('\\035wifi+'..wifi.sta.status())");
brainliang 0:63af4719467f 124 network_start = true;
brainliang 0:63af4719467f 125 return true;
brainliang 0:63af4719467f 126 }
brainliang 0:63af4719467f 127
brainliang 0:63af4719467f 128 bool Esp8266::weblogin() { //定义类的函数
brainliang 0:63af4719467f 129 // not implemented yet
brainliang 0:63af4719467f 130 return true;
brainliang 0:63af4719467f 131 }
brainliang 0:63af4719467f 132
brainliang 0:63af4719467f 133 bool Esp8266::connect_mqtt_broker(char *ip) { //定义类的函数
brainliang 0:63af4719467f 134 ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)");
brainliang 0:63af4719467f 135 ESP_CMD("m:connect(\"%s\")", ip);
brainliang 0:63af4719467f 136 wait(1);
brainliang 0:63af4719467f 137 // subscribe all the topic
brainliang 0:63af4719467f 138 mqtt_start = true;
brainliang 0:63af4719467f 139 return true;
brainliang 0:63af4719467f 140 }
brainliang 0:63af4719467f 141
brainliang 0:63af4719467f 142 bool Esp8266::publish(char *topic, char *data, int size) { //定义类的函数
brainliang 0:63af4719467f 143 //if (mqtt_start) {
brainliang 0:63af4719467f 144 ESP_CMD("m:publish(\"%s\",\"%s\",0,0)", topic, data);
brainliang 0:63af4719467f 145 //}
brainliang 0:63af4719467f 146 wait(0.1);
brainliang 0:63af4719467f 147 return true;
brainliang 0:63af4719467f 148 }
brainliang 0:63af4719467f 149
brainliang 0:63af4719467f 150 bool Esp8266::subscribe_poll(char *topic, char *data, int size) { //定义类的函数
brainliang 0:63af4719467f 151 //if (mqtt_start) {
brainliang 0:63af4719467f 152 ESP_CMD("m:subscribe(\"%s\", 0)", topic);
brainliang 0:63af4719467f 153 wait(0.1);
brainliang 0:63af4719467f 154 //}
brainliang 0:63af4719467f 155 ESP_CMD("m:on(\"message\", function(conn, topic, data)");
brainliang 0:63af4719467f 156 ESP_CMD("if topic == \"%s\" then", topic);
brainliang 0:63af4719467f 157 ESP_CMD("print(\"\\035\"..topic..\"+\"..data)");
brainliang 0:63af4719467f 158 ESP_CMD("end");
brainliang 0:63af4719467f 159 ESP_CMD("end)");
brainliang 0:63af4719467f 160
brainliang 0:63af4719467f 161 ser2esp8266.attach(simple_callback, Serial::RxIrq);
brainliang 0:63af4719467f 162 //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq);
brainliang 0:63af4719467f 163
brainliang 0:63af4719467f 164 while (0) {
brainliang 0:63af4719467f 165 while(!esp_buf_ready);
brainliang 0:63af4719467f 166 esp_buf_ready = false;
brainliang 0:63af4719467f 167
brainliang 0:63af4719467f 168 if (strcmp(topic, esp_tokenBuf) == 0) {
brainliang 0:63af4719467f 169
brainliang 0:63af4719467f 170 if (size == -1) {
brainliang 0:63af4719467f 171 //uint8_t len = strlen(esp_param);
brainliang 0:63af4719467f 172 //memcpy(data, esp_param, len);
brainliang 0:63af4719467f 173 //data[len] = '\0';
brainliang 0:63af4719467f 174 } else {
brainliang 0:63af4719467f 175 //memcpy(data, esp_param, size);
brainliang 0:63af4719467f 176 //data[size] = '\0';
brainliang 0:63af4719467f 177 }
brainliang 0:63af4719467f 178 break;
brainliang 0:63af4719467f 179 }
brainliang 0:63af4719467f 180 }
brainliang 0:63af4719467f 181
brainliang 0:63af4719467f 182 ESP_CMD("m:unsubscribe(\"%s\")", topic);
brainliang 0:63af4719467f 183 return true;
brainliang 0:63af4719467f 184 }