User Tools

Site Tools


esp8266_ws_150

Documentation ESP8266 base program code

ESP8266 Weather Station V 1.5.0

Code

ESP8266_AP_Web_WS_1_5_0.ino
/*
Author: Dubravko Penezic
 
Version: 1.5.0, 2016
 
This code is example how to use ESP8266 microcontroler like AP, with Web server, and
data presentation via Web pages and I2C OLED SSD1306 display.
Data are read from 1-Wire DS18B20 temperature sensor and DHT11 humidity sensor.
Air preasure will be readed from I2C DMP085 sensor.
 
Source code is provided as is, without any warranty.
 
Distributetd under CC BY v 3.0
 
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN  2
 
#include <Dp1WBasic.h>
#include <Dp1WDS18xxxTermo.h>
#define ONE_WIRE_BUS 14
 
#include <ESP_SSD1306.h>    
#include <Adafruit_GFX.h>
 
#define SDA 5
#define SCL 4
 
// #include <Adafruit_BMP085.h>
 
// WiFi Definitions
const char AP_SSID[] = "ESP8266-WS";
const char AP_PSK[] = "TestTest";
 
// Pin Definitions
// 1-Wire sensor
const int TS_PIN = 16;
 
DHT dht(DHTPIN, DHTTYPE, 11);
 
Dp1WBasic oneWire(ONE_WIRE_BUS);
Dp1WDS18xxxTermo tSen(&oneWire);
 
ESP8266WebServer server(80);
 
ESP_SSD1306 display(false, SDA, SCL);
 
// Adafruit_BMP085 bmp(SDA, SCL, BMP085_ULTRAHIGHRES);
 
float dht_hum, dht_temp, dht_hi, ds_temp, bmp_temp, bmp_alt;  // Values read from sensor
unsigned long previousMillis = 0;
unsigned long bmp_press, bmp_press0;
String webString="";
const long interval = 3000;
const long loop_interval = 60000;
 
unsigned long currentMillis = 0;
unsigned long preMillis = 0;
 
SensorInfo tempSensors[16];
 
void setup() 
{
  Serial.begin(115200);
  Serial.println("ESP8266 - Weather Station V 1.0.0.");
  // SSD1306 Init
  display.begin(SSD1306_SWITCHCAPVCC);  // Switch OLED
  // Clear the buffer.
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("  ESP8266 \n\n WS 1.5.0 ");
  display.display();
  WiFi.mode(WIFI_AP);
  WiFi.softAP(AP_SSID, AP_PSK);
  Serial.println("WiFi AP start");
  dht.begin();
  Serial.println("DHT start");
//  bmp.begin();
//  Serial.println("BMP085 start"); 
  set_WebServer();
  Serial.println("HTTP server started");
}
 
void loop() 
{
  server.handleClient();
  unsigned long currentMillis = millis();
  unsigned long preMillis = previousMillis;
 
  if(currentMillis - previousMillis >= loop_interval) {
    get_DS18B20_data();
    previousMillis=preMillis;
    get_dht_data();
//    previousMillis=preMillis;
//    get_BMP085_data();
    Serial.println("Ocitali podatke");
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.print("Temperature: ");
    display.print(ds_temp);
    display.println(" C");
    display.print("Humidity   : ");
    display.print(dht_hum);
    display.println(" %");
    display.print("Heat index : ");
    display.print(dht_hi);
    display.println(" C");
//    display.print("Air Pressure : ");
//    display.print(bmp_press);
//    display.println(" Pa");
//    display.print("Altitude     : ");
//    display.print(bmp_alt);
//    display.println(" m");    
    display.display();
  }
 
 
 
}
 
void set_WebServer()
{
  server.on("/", handle_root);
  server.on("", handle_root);
  server.on("/temp", handle_temp);
  server.on("/humidity", handle_humidity);
 
  server.begin();
}
 
void handle_root() {
  server.send(200, "text/html", "<h1>Hello from the ESP8266 WS V 1.5.0</h1><br><h2> Data are available from link <a href=\"/temp\"> Temperature (/temp)</a> and <a href=\"/humidity\"> Humidity (/humidity)</a></h2>");
  delay(100);
}
 
void handle_temp() {
  get_DS18B20_data();
  webString="<h1>Temperature data from DS18B20</h1><br><h2>Temperature: "+String((int)ds_temp)+" C<br><br><a href=\"/\">Home</a></h2>";
  server.send(200, "text/html", webString);
  delay(100);
}
 
void handle_humidity() {
  get_dht_data();
  webString="<h1>Humidity data from DHT11</h1><br><h2>Humidity: "+String((int)dht_hum)+"% <br>Temperature: "+String((int)dht_temp)+" C <br>Heat Index: "+String((int)dht_hi)+" C<br><br><a href=\"/\">Home</a></h2>";
  server.send(200, "text/html", webString);
  delay(100);
}
 
void get_dht_data() {
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;   
 
    dht_hum = dht.readHumidity();      
    dht_temp = dht.readTemperature(true);
    dht_hi = dht.computeHeatIndex(dht_temp, dht_hum);
    dht_hi = dht.convertFtoC(dht_hi);
    dht_temp = dht.convertFtoC(dht_temp);
    if (isnan(dht_hum) || isnan(dht_temp)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    } else {
      Serial.print("Read DHT sensor temp = ");
      Serial.print(dht_temp);
      Serial.print(" C humidity = ");
      Serial.print(dht_hum);
      Serial.print("% Heat Index = ");
      Serial.print(dht_hi);  
      Serial.println(" C"); 
    }
  }
}
 
void get_DS18B20_data()
{
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
 
    byte broj = tSen.readTemperature(tempSensors, 16, true);
 
    if(broj == 0 ) { 
      Serial.print("No 1-Wire Temperature Sensor Found on Digital Pin ");
      Serial.println(ONE_WIRE_BUS);
    } else {
      Serial.print("\n\nTemperature sensor ROM Address: ");
 
      for(byte j=0; j<8;j++) {
        if (tempSensors[0].dAddr[j] < 16) Serial.print("0");
        Serial.print(tempSensors[0].dAddr[j], HEX);
      }
 
      Serial.print(" Sensor type: ");
      switch (tempSensors[0].dAddr[0]) {
        case DS18B20MODEL:
          Serial.println("DS18B20");
          break;
        case DS1822MODEL:
          Serial.println("DS1822");
          break;
        case DS18S20MODEL:
          Serial.println("DS18S20/DS1820");
          break;
      }
 
      Serial.print("Actual temperature \t");
      Serial.print(tempSensors[0].dTemperature);
      ds_temp = tempSensors[0].dTemperature;
      Serial.print(" C (");
      Serial.print(getResolution(tempSensors[0].dAddr,tempSensors[0].dTempReso),DEC);
      Serial.println(" bit)");  
    }
  }
}
 
byte getResolution(uint8_t* deviceAddress, uint8_t conf) {
 
  if (deviceAddress[0] == DS18S20MODEL) return 9; // this model has a fixed resolution
 
  switch (conf) {
    case TEMP_12_BIT:
      return 12;
 
    case TEMP_11_BIT:
      return 11;
 
    case TEMP_10_BIT:
      return 10;
 
    case TEMP_9_BIT:
      return 9;
  }
}
 
//void get_BMP085_data() {
//  unsigned long currentMillis = millis();
// 
//  if(currentMillis - previousMillis >= interval) {
//    previousMillis = currentMillis;
//
//    Serial.print("Temperature = ");
//    bmp_temp = bmp.readTemperature();
//    Serial.print(bmp_temp);
//    Serial.println(" C"); 
//    Serial.print("Pressure = ");
//    bmp_press = bmp.readPressure();
//    Serial.print(bmp_press);
//    Serial.println(" Pa");
//    Serial.print("Altitude = ");
//    bmp_alt = bmp.readAltitude();
//    Serial.print(bmp_alt);
//    Serial.println(" meters");
//    Serial.print("Pressure at sealevel (calculated) = ");
//    bmp_press0 = bmp.readSealevelPressure();
//    Serial.print(bmp_press0);
//    Serial.println(" Pa");
//  }
//}

HW configuration



All files together

esp8266_ws_150.txt · Last modified: 2016/09/01 14:17 (external edit)