* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了

Dependencies:   mbed

Committer:
lixianyu
Date:
Fri Jun 24 02:06:43 2016 +0000
Revision:
1:e34100dd6532
Parent:
0:740c1eb2df13
?Arduino??????????0~255??????LPC824????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:740c1eb2df13 1 /**
lixianyu 0:740c1eb2df13 2 * @file BlynkSimpleShieldEsp8266.h
lixianyu 0:740c1eb2df13 3 * @author Volodymyr Shymanskyy
lixianyu 0:740c1eb2df13 4 * @license This project is released under the MIT License (MIT)
lixianyu 0:740c1eb2df13 5 * @copyright Copyright (c) 2015 Volodymyr Shymanskyy
lixianyu 0:740c1eb2df13 6 * @date Jun 2015
lixianyu 0:740c1eb2df13 7 * @brief
lixianyu 0:740c1eb2df13 8 *
lixianyu 0:740c1eb2df13 9 */
lixianyu 0:740c1eb2df13 10
lixianyu 0:740c1eb2df13 11 #ifndef BlynkSimpleShieldEsp8266_h
lixianyu 0:740c1eb2df13 12 #define BlynkSimpleShieldEsp8266_h
lixianyu 0:740c1eb2df13 13
lixianyu 0:740c1eb2df13 14 #ifdef ESP8266
lixianyu 0:740c1eb2df13 15 #error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.
lixianyu 0:740c1eb2df13 16 #endif
lixianyu 0:740c1eb2df13 17
lixianyu 0:740c1eb2df13 18 //extern Timer g_Timer;
lixianyu 0:740c1eb2df13 19
lixianyu 0:740c1eb2df13 20 #ifndef BLYNK_INFO_CONNECTION
lixianyu 0:740c1eb2df13 21 #define BLYNK_INFO_CONNECTION "ESP8266"
lixianyu 0:740c1eb2df13 22 #endif
lixianyu 0:740c1eb2df13 23
lixianyu 0:740c1eb2df13 24
lixianyu 0:740c1eb2df13 25 extern Serial pc;
lixianyu 0:740c1eb2df13 26 //#define LOG_ESP8266
lixianyu 0:740c1eb2df13 27 #ifdef LOG_ESP8266
lixianyu 0:740c1eb2df13 28 #define ESP8266_LOG pc.printf
lixianyu 0:740c1eb2df13 29 #define LOG_ENTER ESP8266_LOG("Enter %s\r\n", __func__);
lixianyu 0:740c1eb2df13 30 #else
lixianyu 0:740c1eb2df13 31 #define ESP8266_LOG(...)
lixianyu 0:740c1eb2df13 32 #define LOG_ENTER
lixianyu 0:740c1eb2df13 33 #endif
lixianyu 0:740c1eb2df13 34
lixianyu 0:740c1eb2df13 35 #define BLYNK_MICRODUINO
lixianyu 0:740c1eb2df13 36 //#define BLYNK_SEND_ATOMIC
lixianyu 0:740c1eb2df13 37
lixianyu 0:740c1eb2df13 38 // TODO: Remove this hotfix
lixianyu 0:740c1eb2df13 39 //#define BLYNK_NO_INFO
lixianyu 0:740c1eb2df13 40
lixianyu 0:740c1eb2df13 41 #include <BlynkApiMbed.h>
lixianyu 0:740c1eb2df13 42 #include <Blynk/BlynkProtocol.h>
lixianyu 0:740c1eb2df13 43 #include <utility/BlynkFifo.h>
lixianyu 0:740c1eb2df13 44 #include <ESP8266_Lib.h>
lixianyu 0:740c1eb2df13 45
lixianyu 0:740c1eb2df13 46 class BlynkTransportShieldEsp8266
lixianyu 0:740c1eb2df13 47 {
lixianyu 0:740c1eb2df13 48 static void onData(uint8_t mux_id, uint32_t len, void* ptr) {
lixianyu 0:740c1eb2df13 49 ((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len);
lixianyu 0:740c1eb2df13 50 }
lixianyu 0:740c1eb2df13 51
lixianyu 0:740c1eb2df13 52 void onData(uint8_t mux_id, uint32_t len) {
lixianyu 0:740c1eb2df13 53 //BLYNK_LOG2("Got ", len);
lixianyu 0:740c1eb2df13 54 ESP8266_LOG("onData(),mux_id=%d,len=%u\r\n", mux_id,len);
lixianyu 0:740c1eb2df13 55 while (len) {
lixianyu 0:740c1eb2df13 56 if (client->getUart()->readable()) {
lixianyu 0:740c1eb2df13 57 uint8_t b = client->getUart()->getc();
lixianyu 0:740c1eb2df13 58 if(!buffer.push(b)) {
lixianyu 0:740c1eb2df13 59 BLYNK_LOG1(BLYNK_F("Buffer overflow"));
lixianyu 0:740c1eb2df13 60 }
lixianyu 0:740c1eb2df13 61 len--;
lixianyu 0:740c1eb2df13 62 }
lixianyu 0:740c1eb2df13 63 }
lixianyu 0:740c1eb2df13 64 }
lixianyu 0:740c1eb2df13 65
lixianyu 0:740c1eb2df13 66 public:
lixianyu 0:740c1eb2df13 67 BlynkTransportShieldEsp8266()
lixianyu 0:740c1eb2df13 68 : client(NULL)
lixianyu 0:740c1eb2df13 69 , status(false)
lixianyu 0:740c1eb2df13 70 , domain(NULL)
lixianyu 0:740c1eb2df13 71 , port(0)
lixianyu 0:740c1eb2df13 72 {}
lixianyu 0:740c1eb2df13 73
lixianyu 0:740c1eb2df13 74 void begin_domain(ESP8266* esp8266, const char* d, uint16_t p) {
lixianyu 0:740c1eb2df13 75 LOG_ENTER;
lixianyu 0:740c1eb2df13 76 client = esp8266;
lixianyu 0:740c1eb2df13 77 client->setOnData(onData, this);
lixianyu 0:740c1eb2df13 78 domain = d;
lixianyu 0:740c1eb2df13 79 port = p;
lixianyu 0:740c1eb2df13 80 }
lixianyu 0:740c1eb2df13 81
lixianyu 0:740c1eb2df13 82 bool connect() {
lixianyu 0:740c1eb2df13 83 uint32_t startTime = g_BlynkTimer.read_ms();
lixianyu 0:740c1eb2df13 84 ESP8266_LOG("connect(), domain = %s, port = %d\r\n", domain, port);
lixianyu 0:740c1eb2df13 85 pc.printf("connect(), domain = %s, port = %d, startTime=%u\r\n", domain, port, startTime);
lixianyu 0:740c1eb2df13 86 if (!domain || !port)
lixianyu 0:740c1eb2df13 87 return false;
lixianyu 0:740c1eb2df13 88 status = client->createTCP(domain, port);
lixianyu 0:740c1eb2df13 89 ESP8266_LOG("status = %d\r\n", status);
lixianyu 0:740c1eb2df13 90 return status;
lixianyu 0:740c1eb2df13 91 }
lixianyu 0:740c1eb2df13 92
lixianyu 0:740c1eb2df13 93 void disconnect() {
lixianyu 0:740c1eb2df13 94 #if 1
lixianyu 0:740c1eb2df13 95 LOG_ENTER;
lixianyu 0:740c1eb2df13 96 status = false;
lixianyu 0:740c1eb2df13 97 buffer.clear();
lixianyu 0:740c1eb2df13 98 client->releaseTCP();
lixianyu 0:740c1eb2df13 99 #endif
lixianyu 0:740c1eb2df13 100 }
lixianyu 0:740c1eb2df13 101
lixianyu 0:740c1eb2df13 102 size_t read(void* buf, size_t len) {
lixianyu 0:740c1eb2df13 103 //uint32_t start = g_BlynkTimer.read_ms();
lixianyu 0:740c1eb2df13 104 ESP8266_LOG("Waiting: %d, Occuied: %d\r\n", len, buffer.getOccupied());
lixianyu 0:740c1eb2df13 105 //uint32_t start = millis();
lixianyu 0:740c1eb2df13 106 //buffer.dump();
lixianyu 0:740c1eb2df13 107 uint32_t start = g_BlynkTimer.read_ms();
lixianyu 0:740c1eb2df13 108 //BLYNK_LOG4("Waiting: ", len, " Occuied: ", buffer.getOccupied());
lixianyu 0:740c1eb2df13 109 while ((buffer.getOccupied() < len) && (g_BlynkTimer.read_ms() - start < 1500)) {
lixianyu 0:740c1eb2df13 110 client->run();
lixianyu 0:740c1eb2df13 111 }
lixianyu 0:740c1eb2df13 112 return buffer.read((uint8_t*)buf, len);
lixianyu 0:740c1eb2df13 113 }
lixianyu 0:740c1eb2df13 114
lixianyu 0:740c1eb2df13 115 size_t write(const void* buf, size_t len) {
lixianyu 0:740c1eb2df13 116 ESP8266_LOG("Enter write, len = %d\r\n", len);
lixianyu 0:740c1eb2df13 117 if (client->send((const uint8_t*)buf, len)) {
lixianyu 0:740c1eb2df13 118 return len;
lixianyu 0:740c1eb2df13 119 }
lixianyu 0:740c1eb2df13 120 return 0;
lixianyu 0:740c1eb2df13 121 }
lixianyu 0:740c1eb2df13 122
lixianyu 0:740c1eb2df13 123 bool connected() {
lixianyu 0:740c1eb2df13 124 //LOG_ENTER
lixianyu 0:740c1eb2df13 125 return status;
lixianyu 0:740c1eb2df13 126 }
lixianyu 0:740c1eb2df13 127
lixianyu 0:740c1eb2df13 128 int available() {
lixianyu 0:740c1eb2df13 129 client->run();
lixianyu 0:740c1eb2df13 130 //BLYNK_LOG2("Still: ", buffer.getOccupied());
lixianyu 0:740c1eb2df13 131 return buffer.getOccupied();
lixianyu 0:740c1eb2df13 132 }
lixianyu 0:740c1eb2df13 133
lixianyu 0:740c1eb2df13 134 private:
lixianyu 0:740c1eb2df13 135 ESP8266* client;
lixianyu 0:740c1eb2df13 136 bool status;
lixianyu 0:740c1eb2df13 137 BlynkFifo<uint8_t,256> buffer;
lixianyu 0:740c1eb2df13 138 const char* domain;
lixianyu 0:740c1eb2df13 139 uint16_t port;
lixianyu 0:740c1eb2df13 140 };
lixianyu 0:740c1eb2df13 141
lixianyu 0:740c1eb2df13 142 class BlynkWifi
lixianyu 0:740c1eb2df13 143 : public BlynkProtocol<BlynkTransportShieldEsp8266>
lixianyu 0:740c1eb2df13 144 {
lixianyu 0:740c1eb2df13 145 typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base;
lixianyu 0:740c1eb2df13 146 public:
lixianyu 0:740c1eb2df13 147 BlynkWifi(BlynkTransportShieldEsp8266& transp)
lixianyu 0:740c1eb2df13 148 : Base(transp)
lixianyu 0:740c1eb2df13 149 , wifi(NULL) {
lixianyu 0:740c1eb2df13 150 //pc.printf("Enter BlynkWifi(BlynkTransportShieldEsp8266& transp)\r\n");
lixianyu 0:740c1eb2df13 151 //g_BlynkTimer.start();
lixianyu 0:740c1eb2df13 152 }
lixianyu 0:740c1eb2df13 153
lixianyu 0:740c1eb2df13 154 bool connectWiFi(const char* ssid, const char* pass) {
lixianyu 0:740c1eb2df13 155 //int nowtime = millis();
lixianyu 0:740c1eb2df13 156 int nowtime = g_BlynkTimer.read_ms();
lixianyu 0:740c1eb2df13 157 ESP8266_LOG("connectWiFi: ssid=%s, pass=%s, nowtime=%u\r\n", ssid, pass, nowtime);
lixianyu 0:740c1eb2df13 158 wait_ms(500);
lixianyu 0:740c1eb2df13 159 BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
lixianyu 0:740c1eb2df13 160 /*if (!wifi->restart()) {
lixianyu 0:740c1eb2df13 161 BLYNK_LOG1(BLYNK_F("Failed to restart"));
lixianyu 0:740c1eb2df13 162 return false;
lixianyu 0:740c1eb2df13 163 }*/
lixianyu 0:740c1eb2df13 164 if (!wifi->setEcho(0)) {
lixianyu 0:740c1eb2df13 165 BLYNK_LOG1(BLYNK_F("Failed to disable Echo"));
lixianyu 0:740c1eb2df13 166 ESP8266_LOG(BLYNK_F("Failed to disable Echo"));
lixianyu 0:740c1eb2df13 167 return false;
lixianyu 0:740c1eb2df13 168 }
lixianyu 0:740c1eb2df13 169 if (!wifi->setOprToStation()) {
lixianyu 0:740c1eb2df13 170 BLYNK_LOG1(BLYNK_F("Failed to set STA mode"));
lixianyu 0:740c1eb2df13 171 ESP8266_LOG(BLYNK_F("Failed to set STA mode"));
lixianyu 0:740c1eb2df13 172 return false;
lixianyu 0:740c1eb2df13 173 }
lixianyu 0:740c1eb2df13 174 if (wifi->joinAP(ssid, pass)) {
lixianyu 0:740c1eb2df13 175 BLYNK_LOG2(BLYNK_F("IP: "), wifi->getLocalIP().c_str());
lixianyu 0:740c1eb2df13 176 ESP8266_LOG(BLYNK_F("IP: %s\r\n"), wifi->getLocalIP().c_str());
lixianyu 0:740c1eb2df13 177 } else {
lixianyu 0:740c1eb2df13 178 BLYNK_LOG1(BLYNK_F("Failed to connect WiFi"));
lixianyu 0:740c1eb2df13 179 ESP8266_LOG(BLYNK_F("Failed to connect WiFi"));
lixianyu 0:740c1eb2df13 180 return false;
lixianyu 0:740c1eb2df13 181 }
lixianyu 0:740c1eb2df13 182 if (!wifi->disableMUX()) {
lixianyu 0:740c1eb2df13 183 BLYNK_LOG1(BLYNK_F("Failed to disable MUX"));
lixianyu 0:740c1eb2df13 184 ESP8266_LOG(BLYNK_F("Failed to disable MUX"));
lixianyu 0:740c1eb2df13 185 }
lixianyu 0:740c1eb2df13 186 BLYNK_LOG1(BLYNK_F("Connected to WiFi"));
lixianyu 0:740c1eb2df13 187 int nowtime1 = g_BlynkTimer.read_ms();
lixianyu 0:740c1eb2df13 188 ESP8266_LOG("Connected to WiFi...%dms\r\n", nowtime1 - nowtime);
lixianyu 0:740c1eb2df13 189 return true;
lixianyu 0:740c1eb2df13 190 }
lixianyu 0:740c1eb2df13 191
lixianyu 0:740c1eb2df13 192 void config(ESP8266& esp8266,
lixianyu 0:740c1eb2df13 193 const char* auth,
lixianyu 0:740c1eb2df13 194 const char* domain = BLYNK_DEFAULT_DOMAIN,
lixianyu 0:740c1eb2df13 195 uint16_t port = BLYNK_DEFAULT_PORT) {
lixianyu 0:740c1eb2df13 196 LOG_ENTER;
lixianyu 0:740c1eb2df13 197 Base::begin(auth);
lixianyu 0:740c1eb2df13 198 wifi = &esp8266;
lixianyu 0:740c1eb2df13 199 this->conn.begin_domain(wifi, domain, port);
lixianyu 0:740c1eb2df13 200 }
lixianyu 0:740c1eb2df13 201
lixianyu 0:740c1eb2df13 202 void begin(const char* auth,
lixianyu 0:740c1eb2df13 203 ESP8266& esp8266,
lixianyu 0:740c1eb2df13 204 const char* ssid,
lixianyu 0:740c1eb2df13 205 const char* pass,
lixianyu 0:740c1eb2df13 206 const char* domain = BLYNK_DEFAULT_DOMAIN,
lixianyu 0:740c1eb2df13 207 uint16_t port = BLYNK_DEFAULT_PORT) {
lixianyu 0:740c1eb2df13 208 ESP8266_LOG("Enter begin, auth=%s\r\n", auth);
lixianyu 0:740c1eb2df13 209 config(esp8266, auth, domain, port);
lixianyu 0:740c1eb2df13 210 connectWiFi(ssid, pass);
lixianyu 0:740c1eb2df13 211 }
lixianyu 0:740c1eb2df13 212
lixianyu 0:740c1eb2df13 213 private:
lixianyu 0:740c1eb2df13 214 ESP8266* wifi;
lixianyu 0:740c1eb2df13 215 };
lixianyu 0:740c1eb2df13 216
lixianyu 0:740c1eb2df13 217 static BlynkTransportShieldEsp8266 _blynkTransport;
lixianyu 0:740c1eb2df13 218 BlynkWifi Blynk(_blynkTransport);
lixianyu 0:740c1eb2df13 219
lixianyu 0:740c1eb2df13 220 #include <BlynkWidgets.h>
lixianyu 0:740c1eb2df13 221
lixianyu 0:740c1eb2df13 222 #endif
lixianyu 0:740c1eb2df13 223