Last active
December 9, 2018 20:56
-
-
Save chipviled/85ce010b0a883821b769891e26b8b460 to your computer and use it in GitHub Desktop.
esp_(nanomcu_v3)_dht_v3
This file contains hidden or 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
/** | |
* Experiment v3 | |
*/ | |
#include "DHT.h" | |
#include "ESP8266WiFi.h" | |
#include "WiFiClient.h" | |
#include "ESP8266WebServer.h" | |
#define DHTPIN_1 4 // D2 | |
#define DHTPIN_2 14 // D5 | |
#define DHTTYPE DHT22 | |
const char* ssid = "SSID"; | |
const char* password = "PASSWORD"; | |
const char* token = "TOKEN"; | |
IPAddress ip(192, 168, 1, 61); //статический IP | |
IPAddress gateway(192, 168, 1, 99); | |
IPAddress subnet(255, 255, 255, 0); | |
ESP8266WebServer server(80); | |
DHT dht1(DHTPIN_1, DHTTYPE); | |
DHT dht2(DHTPIN_2, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
Serial.setTimeout(2000); | |
// Wait for serial to initialize. | |
while(!Serial) { delay(50); } | |
WiFi.begin(ssid, password); | |
WiFi.config(ip, gateway, subnet); | |
Serial.println(""); | |
// ожидание соединения | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
server.on("/", handleRoot); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
String getTempData(int id) { | |
float h; | |
float t; | |
float hic; | |
if (id == 1) { | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
h = dht1.readHumidity(); | |
t = dht1.readTemperature(); | |
// Compute heat index in Celsius (isFahreheit = false) | |
hic = dht1.computeHeatIndex(t, h, false); | |
} else if (id == 2) { | |
h = dht2.readHumidity(); | |
t = dht2.readTemperature(); | |
hic = dht2.computeHeatIndex(t, h, false); | |
} else { | |
Serial.println("Incorrect id:" + String(id)); | |
return "{\"error\":true,\"message\":\"Failed to select DHT sensor!\"}"; | |
} | |
if (isnan(h) || isnan(t)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return "{\"error\":true,\"message\":\"Failed to read from DHT sensor!\"}"; | |
} | |
Serial.print("Humidity: "); | |
Serial.print(h); | |
Serial.print(" %\t"); | |
Serial.print("Temperature: "); | |
Serial.print(t); | |
Serial.print(" *C "); | |
Serial.print("Heat index: "); | |
Serial.print(hic); | |
Serial.println(" *C "); | |
return "{\"id\":" + String(id) + ",\"h\":" + String(h, 2) + ",\"t\":" | |
+ String(t, 2) + ",\"hic\":" + String(hic, 2) + "}"; | |
} | |
void handleRoot() { | |
Serial.println("Get request"); | |
if (server.hasArg("token") == false or server.arg("token") != token){ | |
server.send(200, "application/json", "{\"error\":true,\"message\":\"You forget some params\"}"); | |
return; | |
} | |
if (server.hasArg("id") == false){ | |
server.send(200, "application/json", "{\"error\":true,\"message\":\"You forget set id\"}"); | |
return; | |
} | |
if (server.arg("id") != "1" and server.arg("id") != "2"){ | |
server.send(200, "application/json", "{\"error\":true,\"message\":\"Incorrect id\"}"); | |
return; | |
} | |
Serial.println("Get data for response"); | |
String data = getTempData(server.arg("id").toInt()); | |
server.send(200, "application/json", data); | |
} | |
void loop() { | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment