Home |

Trigat

Temperature Web Server with OLED

10-15-2017

This Arduino program creates a web server that displays the temperature. It uses a Nodemcu ESP8266, DHT temperature sensor, Adafruit 128x64 OLED, and 2 buttons. Information is also displayed on the OLED.

Language or Platform: Arduino

Image

Code:

// Temperature Web Server with OLED
// By Josh M
// ---------------------------------
// Uses NodeMCU ESP8266, DHT sensor, Adafruit OLED, and buttons
// Based on code from Adafruit

// Connect button 1 to GPIO16 on the NodeMcu
// Connect button 2 to GIPO10
// Use a 10k resistor for both buttons when connecting them to 3.3V
// Connect DHT sensor's DATA to GPIO2
// Connect RST on OLED to RST on the NodeMcu
// OLED CLK connects to GPIO5 of NodeMcu
// OLED DATA connect to GPIO4 of NodeMcu

#include <ESP8266WiFi.h>
#include <DHT.h> // temperature sensor
#include <Wire.h> // used for I2C communication
#include <SPI.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(OLED_RESET);

#define DHTTYPE DHT11   // use for DHT 11
//#define DHTTYPE DHT21   // use for DHT 21
//#define DHTTYPE DHT22   // use for DHT 22

int pushButton1 = 16;
int pushButton2 = 10;
int buttonState1 = 0;
int buttonState2 = 0;

const char* ssid = "SSID_NAME";
const char* password = "SSID_PASSWORD";

WiFiServer server(80);

const int DHTPin = 2;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];

void setup() {
    pinMode(pushButton1, INPUT);
    pinMode(pushButton2, INPUT);
    // initialize serial port
    Serial.begin(115200);
    delay(10);

    display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)


    // Show image buffer on the display hardware.
    // displays splashscreen
    display.display();
    delay(1000);
    display.clearDisplay();

    display.println(" ");
    // Clear the buffer.
    display.display();
    display.clearDisplay();

    dht.begin();

    // connect to SSID
    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");
    // Starting the web server
    server.begin();
    Serial.println("Web server up. Obtaining IP for the server...");
    delay(10000);
    // Printing the ESP IP address
    Serial.println(WiFi.localIP());
}

void loop() {
    WiFiClient client = server.available();
    if (client) {
        // boolean to locate when the http request ends
        boolean blank_line = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                if (c == '\n' && blank_line) {
                    float h = dht.readHumidity();
                    // Read temperature as Celsius (the default)
                    float t = dht.readTemperature();
                    // Read temperature as Fahrenheit (isFahrenheit = true)
                    float f = dht.readTemperature(true);
                    // Check if any reads failed and exit early (to try again).
                    if (isnan(h) || isnan(t) || isnan(f)) {
                      Serial.println("Failed to read from DHT sensor!");
                      strcpy(celsiusTemp,"Failed");
                      strcpy(fahrenheitTemp, "Failed");
                      strcpy(humidityTemp, "Failed");
                  }
                  else {
                      // Computes temperature values in Celsius + Fahrenheit and Humidity
                      float hic = dht.computeHeatIndex(t, h, false);
                      dtostrf(hic, 6, 2, celsiusTemp);
                      float hif = dht.computeHeatIndex(f, h);
                      dtostrf(hif, 6, 2, fahrenheitTemp);
                      dtostrf(h, 6, 2, humidityTemp);
                  }
                  // Create web page
                  client.println("HTTP/1.1 200 OK");
                  client.println("Content-Type: text/html");
                  client.println("Connection: close");
                  client.println();
                  client.println("<!DOCTYPE HTML>");
                  client.println("<html>");
                  client.println("<head></head><body bgcolor=\"#092706\"><font color=\"#46993D\"><h3>Temperature in Celsius: ");
                  client.println(celsiusTemp);
                  client.println("*C</h3><h3>Temperature in Fahrenheit: ");
                  client.println(fahrenheitTemp);
                  client.println("*F</h3><h3>Humidity: ");
                  client.println(humidityTemp);
                  client.println("%</h3>");
                  client.println("</font></body></html>");
                  break;
            }
            if (c == '\n') {
                // when new line is read
                blank_line = true;
            }
            else if (c != '\r') {
                // when new character found on line
                blank_line = false;
            }
        }
    }
    // closing the client connection
    delay(1);
    client.stop();
    }
    buttonpress();
}

void buttonpress() {

    // read the state of the pushbutton value:
    buttonState1 = digitalRead(pushButton1);
    buttonState2 = digitalRead(pushButton2);

    // check if the pushbutton is pressed.
    if (buttonState1 == LOW) {
        Serial.println("Button 1 Pushed");
        // draw rectangles
        testdrawrect();
        display.display();
        delay(1000);
        display.clearDisplay();
        // text display tests
        display.setTextSize(1);
        display.setTextColor(WHITE);
        display.setCursor(0,0);
        display.println("Connect to:");
        display.println(" ");
        display.print("http://");display.println(WiFi.localIP());
        display.println(" ");
        display.println("WIFI name:");
        display.println(ssid);
        display.display();
        display.clearDisplay();
        delay(6000);
        display.println(" ");
        display.display();
        display.clearDisplay();
        delay(20);
    }
    if (buttonState2 == LOW) {
        Serial.println("Button 2 Pushed");
        float h = dht.readHumidity();
        // Read temperature as Celsius (the default)
        float t = dht.readTemperature();
        // Read temperature as Fahrenheit (isFahrenheit = true)
        float f = dht.readTemperature(true);
        float hic = dht.computeHeatIndex(t, h, false);
        dtostrf(hic, 6, 2, celsiusTemp);
        float hif = dht.computeHeatIndex(f, h);
        dtostrf(hif, 6, 2, fahrenheitTemp);
        dtostrf(h, 6, 2, humidityTemp);
        // display temperature on OLED
        display.setTextSize(1);
        display.setTextColor(WHITE);
        display.setCursor(0,0);
        display.println("Temperature:");
        display.println(" ");
        display.print(fahrenheitTemp);
        display.println(" F");
        display.print(celsiusTemp);
        display.println(" C");
        display.println(" ");
        display.print(humidityTemp);
        display.println("% Humidity");
        display.display();
        display.clearDisplay();
        delay(5000);
        display.println(" ");
        display.display();
        display.clearDisplay();
        delay(20);
    }
}

void testdrawrect(void) {
    for (int16_t i=0; i<display.height()/2; i+=2) {
        display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
        display.display();
        delay(1);
    }
}

Back