Skip to content

Instantly share code, notes, and snippets.

@funkfinger
Created October 5, 2015 05:00
Show Gist options
  • Select an option

  • Save funkfinger/4b260699a1c22a31279a to your computer and use it in GitHub Desktop.

Select an option

Save funkfinger/4b260699a1c22a31279a to your computer and use it in GitHub Desktop.

Revisions

  1. funkfinger created this gist Oct 5, 2015.
    163 changes: 163 additions & 0 deletions my_remote_temp_server.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,163 @@
    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>


    //////////////////////////
    // DHT sensor stuff....
    //////////////////////////
    #include "DHT.h"
    #define DHTPIN 2
    #define DHTTYPE DHT22
    DHT dht(DHTPIN, DHTTYPE);


    //////////////////////////
    // debug mode
    //////////////////////////
    #define DEBUG false

    //////////////////////////
    // ESP8266 stuff....
    //////////////////////////
    const char* ssid = "....................";
    const char* password = "....................";
    MDNSResponder mdns;
    ESP8266WebServer server(80);
    const int led = 0;

    void handleRoot() {
    //////////////////////////
    // read DHT sensor
    //////////////////////////
    String response = "";
    char buf1[80];
    char buf2[16];
    float h = dht.readHumidity();
    float c = dht.readTemperature();
    float f = dht.readTemperature(true);
    float hif = dht.computeHeatIndex(f, h);
    float hic = dht.computeHeatIndex(c, h, false);



    if (isnan(h) || isnan(f)) {
    response = "Failed to read from DHT sensor!";
    }
    else {

    response += "{";

    // temp in fahrenheit
    dtostrf(f, 5, 2, buf2);
    sprintf(buf1, "\"tempF\": \"%s\",", buf2);
    response += buf1;

    // temp in celsius
    dtostrf(c, 5, 2, buf2);
    sprintf(buf1, "\"tempC\": \"%s\",", buf2);
    response += buf1;

    // humidity
    dtostrf(h, 5, 2, buf2);
    sprintf(buf1, "\"humidity\": \"%s\",", buf2);
    response += buf1;

    // heat index in fahrenheit
    dtostrf(hif, 5, 2, buf2);
    sprintf(buf1, "\"heatIndexF\": \"%s\",", buf2);
    response += buf1;

    // heat index in Ce;sius
    dtostrf(hic, 5, 2, buf2);
    sprintf(buf1, "\"heatIndexC\": \"%s\"", buf2);
    response += buf1;

    response += "}";

    if (DEBUG) doDebug(h, c, f, hic, hif);
    }

    digitalWrite(led, 1);
    server.send(200, "text/plain", response);
    digitalWrite(led, 0);



    }

    void handleNotFound() {
    digitalWrite(led, 1);
    String message = "File Not Found\n\n";
    message += "URI: ";
    message += server.uri();
    message += "\nMethod: ";
    message += (server.method() == HTTP_GET) ? "GET" : "POST";
    message += "\nArguments: ";
    message += server.args();
    message += "\n";
    for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
    }
    server.send(404, "text/plain", message);
    digitalWrite(led, 0);
    }

    void doDebug(float h, float t, float f, float hic, float hif) {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    }

    void setup(void) {
    //////////////////////////
    // start DHT sensor
    //////////////////////////
    dht.begin();

    pinMode(led, OUTPUT);
    digitalWrite(led, 0);
    if (DEBUG) Serial.begin(115200);
    WiFi.begin(ssid, password);
    if (DEBUG) Serial.println("");

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (DEBUG) Serial.print(".");
    }

    if (DEBUG) {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    }

    if (mdns.begin("esp8266", WiFi.localIP())) {
    if (DEBUG) Serial.println("MDNS responder started");
    }

    server.on("/", handleRoot);

    server.onNotFound(handleNotFound);

    server.begin();
    if (DEBUG) Serial.println("HTTP server started");
    }

    void loop(void) {
    server.handleClient();
    }