Skip to content

Instantly share code, notes, and snippets.

@shihyu
Created September 13, 2015 14:42
Show Gist options
  • Save shihyu/c050731e1d5706492f76 to your computer and use it in GitHub Desktop.
Save shihyu/c050731e1d5706492f76 to your computer and use it in GitHub Desktop.
#include <SoftwareSerial.h>
#include "./DHT.h"
#define DEBUG
#define _ledpin 13
SoftwareSerial esp8266(10, 11); // RX, TX
//*-- DHT11
#define _dhtpin 8
#define _dhttype DHT11
DHT dht11( _dhtpin, _dhttype );
uint8_t dhtbuf[2];
String SSID = "";
String PASS = "";
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
// 使用 GET 傳送資料的格式
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=Q7WJSQJAS2KZMR2O";
void setup()
{
// Open serial communications and wait for port to open:
esp8266.begin(115200);
esp8266.setTimeout(5000);
Serial.begin(115200); //can't be faster than 19200 for softserial
Serial.println("ESP8266 Demo");
//test if the module is ready
esp8266.println("AT+RST");
delay(1000);
if (esp8266.find("ready")) {
Serial.println("Module is ready");
} else {
Serial.println("Module have no response.");
while (1);
}
delay(1000);
//connect to the wifi
boolean connected = false;
for (int i = 0; i < 5; i++) {
if (connectWiFi()) {
connected = true;
break;
}
}
if (!connected) {
while (1);
}
delay(5000);
//print the ip addr
/*esp8266.println("AT+CIFSR");
Serial.println("ip address:");
while (esp8266.available())
Serial.write(esp8266.read());*/
//set the single connection mode
esp8266.println("AT+CIPMUX=0");
// DHT11
dht11.begin();
pinMode( _ledpin, OUTPUT );
digitalWrite( _ledpin, LOW );
}
void sendDebug(String cmd)
{
Serial.print("SEND: ");
Serial.println(cmd);
esp8266.println(cmd);
}
void loop()
{
dhtbuf[0] = dht11.readHumidity();
dhtbuf[1] = dht11.readTemperature();
// 確認取回的溫溼度數據可用
if (isnan(dhtbuf[0]) || isnan(dhtbuf[1])) {
Serial.println("Failed to read form DHT11");
} else {
digitalWrite(_ledpin, HIGH);
char buf[3];
String HH, TT;
buf[0] = 0x30 + dhtbuf[1] / 10;
buf[1] = 0x30 + dhtbuf[1] % 10;
TT = (String(buf)).substring(0, 2);
buf[0] = 0x30 + dhtbuf[0] / 10;
buf[1] = 0x30 + dhtbuf[0] % 10;
HH = (String(buf)).substring(0, 2);
updateDHT11(TT, HH);
#ifdef DEBUG
Serial.print("Humidity: ");
Serial.print(HH);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(TT);
Serial.println(" *C\t");
#endif
digitalWrite(_ledpin, LOW);
}
delay(6000); // 60 second
}
void updateDHT11(String T, String H)
{
// 設定 ESP8266 作為 Client 端
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if (esp8266.find("Error")) {
Serial.print("RECEIVED: Error\nExit1");
return;
}
cmd = GET + "&field1=" + T + "&field2=" + H + "\r\n";
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if (esp8266.find(">")) {
Serial.print(">");
Serial.print(cmd);
esp8266.print(cmd);
} else {
sendDebug("AT+CIPCLOSE");
}
if (esp8266.find("OK")) {
Serial.println("RECEIVED: OK");
} else {
Serial.println("RECEIVED: Error\nExit2");
}
}
boolean connectWiFi()
{
esp8266.println("AT+CWMODE=1");
String cmd = "AT+CWJAP=\"";
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
Serial.println(cmd);
esp8266.println(cmd);
delay(2000);
if (esp8266.find("OK")) {
Serial.println("OK, Connected to WiFi.");
return true;
} else {
Serial.println("Can not connect to the WiFi.");
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment