Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DHT.h"
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #define DHTPIN 5
- #define DHTTYPE DHT22
- #define RATE 115200
- #define DEBUG true
- #define SSID "szvsyGm6E74c6HnevvfBHuuY6NG3otjOyxBpEyb9"
- #define PASS "szvsyGm6E74c6HnevvfBHuuY6NG3otjOyxBpEyb9"
- DHT myDht(DHTPIN, DHTTYPE);
- ESP8266WebServer server(80);
- void setup() {
- myDht.begin();
- if (DEBUG == true) {
- Serial.begin(RATE);
- Serial.println("");
- Serial.println("Connecting to WiFI");
- }
- WiFi.begin(SSID, PASS);
- while (WiFi.status() != WL_CONNECTED) {
- delay(10);
- if (DEBUG == true) {
- Serial.print(".");
- }
- }
- if (DEBUG == true) {
- Serial.println("");
- Serial.println("Connected");
- Serial.println(WiFi.localIP());
- }
- server.on("/measurement", HTTP_GET, returnMeasurement);
- server.begin();
- if (DEBUG == true) {
- Serial.println("HTTP server started");
- }
- }
- void returnMeasurement() {
- if (DEBUG == true) {
- Serial.println("GET on /measurement");
- }
- float temperature = myDht.readTemperature();
- float humidity = myDht.readHumidity();
- float heatIndex = myDht.computeHeatIndex(temperature, humidity, false);
- String json = "{\"temperature\":" + String(temperature) + ",\"humidity\":" + String(humidity) + ",\"heatIndex\":" + String(heatIndex) + "}";
- server.send(200, "application/json", json);
- }
- void loop() {
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement