Created
March 24, 2017 12:00
-
-
Save Joelii/5d0ada98333dc13710a281e2aa423afb to your computer and use it in GitHub Desktop.
ESP-12E DHT11 sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include "DHT.h" | |
#define DHTPIN 2 | |
#define DHTTYPE DHT11 // DHT 11 | |
const char libversion[] = "0.0.9"; // API is defined by HELP command | |
// http://semver.org/ 2.0.0: majorBreakApi.minorBackwardsCompat.patchBugFix | |
int i = 0; | |
bool debug = false; | |
bool verb = true; // because verbose seems to be a reserved word | |
String cmd = ""; | |
const char* ssid = "xxxxxx"; | |
const char* password = "xxxxxx"; | |
const char* host = "xxxxxxxx"; | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
v("INFO BotbookESP ready."); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void d(String msg) { | |
if (!debug) return; | |
Serial.print("DEBUG: "); | |
Serial.println(msg); | |
} | |
void v(String msg) { | |
if ((!verb) && (!debug)) return; | |
Serial.print("INFO: "); | |
Serial.println(msg); | |
} | |
String getStringItem(int n, String haystack, char sep = ' ') { | |
int pos = 0; | |
int prevpos = 0; | |
String needle = ""; | |
for (int i = 0; i <= n; i++) { | |
pos = haystack.indexOf(sep, prevpos); | |
if (pos == -1) { | |
needle = haystack.substring(prevpos); | |
} else { | |
needle = haystack.substring(prevpos, pos); | |
} | |
if (i == n) return needle; | |
if (pos == -1) return ""; | |
prevpos = pos + 1; | |
} | |
return "getStringItem() error"; | |
} | |
void printWLANStatus() { | |
int status = WiFi.status(); | |
if (debug) Serial.print(status); | |
if (status == WL_NO_SHIELD) v(" WL_NO_SHIELD"); | |
if (status == WL_IDLE_STATUS) v(" WL_IDLE_STATUS"); | |
if (status == WL_NO_SSID_AVAIL) v(" WL_NO_SSID_AVAIL - does not work with hidded SSID"); | |
if (status == WL_SCAN_COMPLETED) v(" WL_SCAN_COMPLETED"); | |
if (status == WL_CONNECTED) v(" WL_CONNECTED"); | |
if (status == WL_CONNECT_FAILED) v(" WL_CONNECT_FAILED"); | |
if (status == WL_CONNECTION_LOST) v(" WL_CONNECTION_LOST"); | |
if (status == WL_DISCONNECTED) v(" WL_DISCONNECTED"); | |
} | |
bool connectWLAN(String ssid, String password, int timeout = 20) { | |
if ((ssid.length() <= 0) || (password.length() <= 0)) { | |
Serial.println("ERROR: CONNECT: No ssid or password supplied. "); | |
return false; | |
} | |
v("ConnectWLAN(): connecting to SSID "); | |
d(ssid); | |
char ssidStatic[100]; | |
ssid.toCharArray(ssidStatic, 100); | |
char passwordStatic[100]; | |
password.toCharArray(passwordStatic, 100); | |
WiFi.begin(ssidStatic, passwordStatic); | |
int i = 0; | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
printWLANStatus(); | |
if (i >= timeout) { | |
Serial.println("ERROR: CONNECT: Timeout"); | |
return false; | |
} | |
i++; | |
} | |
Serial.print("OK WLAN connected. ESP IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
String httpGet(String url) { | |
if (url.length() <= 0) { | |
Serial.println("ERROR: GET: no url supplied. "); | |
return ""; | |
} | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println("ERROR: GET: Not connected to a WLAN network."); | |
printWLANStatus(); | |
return ""; | |
} | |
v("httpGet(): Opening URL: "); | |
v(url); | |
String urls = url; | |
int prevpos = 0; | |
int pos = urls.indexOf("://"); | |
String proto = urls.substring(prevpos, pos); | |
d("INFO proto: "); | |
d(proto); | |
prevpos = pos + 3; | |
pos = urls.indexOf("/", prevpos); | |
String host = urls.substring(prevpos, pos); | |
d("host: "); | |
d(host); | |
String path = urls.substring(pos); | |
d("INFO path: "); | |
d(path); | |
WiFiClient client; | |
char hostStatic[100]; | |
host.toCharArray(hostStatic, 100); | |
bool isconnected = client.connect(hostStatic, 80); | |
if (!isconnected) { | |
Serial.println("ERROR TCP socket connection failed. "); | |
} | |
//host = "one.api.botbook.com"; | |
client.print(String("GET ") + path + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
unsigned long started = millis(); | |
while (client.available() == 0) { | |
if (millis() - started > 5 * 1000) { | |
Serial.println("ERROR TCP socket client timeout on HTTP GET request."); | |
client.stop(); | |
return ""; | |
} | |
} | |
String line = "Initial value"; | |
char feed = '\n'; | |
d("Skipping header and empty line..."); | |
while (line.length() > 1) { | |
if (client.available() <= 0) { | |
Serial.println("ERROR: GET: http response ended prematurely"); | |
return ""; | |
} | |
line = client.readStringUntil(feed); | |
d(line); | |
} | |
d("Taking first line of body..."); | |
if (client.available() <= 0) { | |
Serial.println("ERROR: GET: http response ended prematurely"); | |
return ""; | |
} | |
line = client.readStringUntil(feed); | |
// line=line.trim() | |
d(line); | |
line = String("OK: Server said: ")+line; | |
return line; | |
} | |
void getSerialCommand() { | |
while (Serial.available() == 0) { | |
delay(50); | |
} | |
cmd = Serial.readStringUntil('\n'); | |
d("Command:"); | |
d(cmd); | |
if (cmd.startsWith("HELLO")) { | |
Serial.print("OK Hello from ESP! BotBookComESP version "); | |
Serial.print(libversion); | |
Serial.println(". Try 'HELP' for help."); | |
return; | |
} | |
if (cmd.startsWith("CONNECT")) { | |
String ssid = getStringItem(1, cmd); | |
d("ssid "); | |
d(ssid); | |
String password = getStringItem(2, cmd); | |
d("password: "); | |
d(password); | |
connectWLAN(ssid, password); | |
return; | |
} | |
if (cmd.startsWith("GET")) { | |
String url = getStringItem(1, cmd); | |
d("url "); | |
d(url); | |
String firstLine = httpGet(url); | |
Serial.println(firstLine); | |
return; | |
} | |
if (cmd.startsWith("DEBUG")) { | |
debug = true; | |
verb = true; | |
Serial.println("OK Debug mode enabled: more verbose serial printing"); | |
return; | |
} | |
if (cmd.startsWith("TRY")) { | |
Serial.println("OK: tryHTTPget() test initiated."); | |
tryHTTPget(); | |
return; | |
} | |
if (cmd.startsWith("HELP")) { | |
help(); | |
return; | |
} | |
Serial.print("ERROR Unknown command: \""); | |
Serial.print(cmd); | |
Serial.println("\". Try HELP."); | |
} | |
void help() { | |
Serial.print("Help for BotBookESP API version "); | |
Serial.print(libversion); | |
Serial.println(":"); | |
Serial.println("HELLO"); | |
Serial.println("HELP"); | |
Serial.println("CONNECT ssid password"); | |
Serial.println("GET http://139.162.200.127/add//?x=88.73"); | |
} | |
void tryHTTPget() { | |
v("BotbookComESP "); | |
v(libversion); | |
connectWLAN("ar", "BotBookCom"); | |
String ans = httpGet("http://139.162.200.127/add//?x=88.73"); | |
v("INFO: httpGet() returned: "); | |
Serial.println(ans); | |
} | |
void loop() { | |
delay(1000); | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
float f = dht.readTemperature(true); | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
String ans = httpGet(String("http://one.api.botbook.com/add//?x=") + String(h)); | |
Serial.println(ans); | |
// float hif = dht.computeHeatIndex(f, x); | |
// float hic = dht.computeHeatIndex(t, x, false); | |
Serial.println(h); | |
Serial.println(t) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment