Skip to content

Instantly share code, notes, and snippets.

@yavor87
Last active May 21, 2016 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yavor87/289d128bbeadcc1335a8 to your computer and use it in GitHub Desktop.
Save yavor87/289d128bbeadcc1335a8 to your computer and use it in GitHub Desktop.
ESP8266 sensor server
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define DHTPIN 2
#define DHTTYPE DHT11
#define LEDPIN 14
ESP8266WebServer server(80);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// Setup LED
pinMode(LEDPIN, OUTPUT);
// Setup DTH11 sensor
dht.begin();
// Setup serial
Serial.begin(115200);
delay(100);
// Connect to WiFi
WiFi.mode(WIFI_STA);
delay(500);
// Called to check if SSID and password has already been stored by previous WPS call.
// The SSID and password are stored in flash memory and will survive a full power cycle.
// Calling ("",""), i.e. with blank string parameters, appears to use these stored values.
WiFi.begin("","");
// Long delay required especially soon after power on.
delay(4000);
if (WiFi.status() != WL_CONNECTED) {
WiFiSmartConfig();
}
Serial.println("\nWiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Setup server
server.on("/", handle_root);
server.on("/actuators/led", HTTP_GET, getLedStatus);
server.on("/actuators/led", HTTP_POST, setLedStatus);
server.on("/sensors/dht11", HTTP_GET, getDHT11Status);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void handle_root() {
server.send(200, "text/plain", "Hello from the weather esp8266, read from /temp or /humidity");
}
void getLedStatus() {
int ledStatus = digitalRead(LEDPIN);
String ledStatusStr = ledStatus == HIGH ? "true" : "false";
String json = "{ \"isOn\": " + ledStatusStr + " }";
server.send(200, "application/json", json);
}
void setLedStatus() {
if (!server.hasArg("status")) {
server.send(400, "text/plain", "Missing \"status\" argument");
return;
}
String arg = server.arg("status");
arg.toLowerCase();
int ledStatus = LOW;
if (arg.equals("high") || arg.equals("true"))
ledStatus = HIGH;
digitalWrite(LEDPIN, ledStatus);
getLedStatus();
}
void getDHT11Status() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
float heatIndex = dht.computeHeatIndex(temp, hum, false);
String json = "{ \"temp\": " + String(temp) + ", \"humidity\": " + String(hum / 100) + ", \"heatIndex\": " + String(heatIndex) + " }";
server.send(200, "application/json", json);
}
void WiFiSmartConfig() {
Serial.println("\nAttempting SmartConfig...");
WiFi.beginSmartConfig();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (WiFi.smartConfigDone()) {
Serial.println("SmartConfig Success");
break;
}
}
}
void WiFiWPSConfig() {
while (WiFi.status() != WL_CONNECTED) {
Serial.println("\nAttempting WPS connection ...");
WiFi.beginWPSConfig();
// Another long delay required.
delay(3000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment