AdSense

Sunday 30 August 2020

Home Automation with a Raspberry PI and ESP8266, part 2: A simple sensor (ESP8266)

(Deutsche Version) As the first project, I want to show my temperature and humidity sensors. I use an ESP8266 who reads a Si7021 sensor. The Si7021 is connected with a 4 pin cable to the ESP on pins D1-D4, as you can see in the image:

Additionally, you have to connect the pin D0 to RST, so deep sleep works, which we will use later. Using quite cheap phone chargers, I used a capacitor parallel to G and 3V.

This is placed in a 3d printed housing that you can find on thingiverse: https://www.thingiverse.com/thing:4583759. The sensor is fixed with the small hook in the housing. afterwards the ESP is placed on top and the lid fixes everything. The open side is suited to connect a phone charger.



The code is comparable to the one on my last post about this topic, https://physudo-e.blogspot.com/2019/07/wifi-thermometer-with-dht22-and-eps8266.html. In the setup part, the wifi is connected. If this does not work, the ESP is resetted after 100 seconds. Additionally, the Si7021 sensor is initialized.

In loop, if the WiFI is connected (otherwise the ESP is resetted), the temperature and humidity is measured. If anything goes wrong here, the ESP is resetted, too. Afterwards, everythin is sent via http request to the Raspberry PI as described in the previous post (https://physudo-e.blogspot.com/2020/08/home-automation-with-raspberry-pi-and.html).

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid     = "WIFI SSID";
const char* password = "WIFI PASSWORD";

#include "Adafruit_Si7021.h"

Adafruit_Si7021 sensor = Adafruit_Si7021();

void setup()
{
  Serial.begin(9600);

  Serial.print("Your are connecting to;");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  int resetCtr = 0;
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    resetCtr ++;
    Serial.print(".");
    if (resetCtr > 200)
    {
      ESP.restart();
    }
  }

  pinMode(D0, WAKEUP_PULLUP);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  digitalWrite(D3, HIGH);
  digitalWrite(D4, LOW);
  delay(500);

  while (!sensor.begin())
  {
    Serial.println("Did not find Si7021 sensor!");
  }

  digitalWrite(D3, LOW);
}



void loop() {
  if (WiFi.status() != WL_CONNECTED)
  {
    ESP.restart();
  }

  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println("");
    Serial.println("Your ESP is connected!");
    Serial.println("Your IP address is: ");
    Serial.println(WiFi.localIP());

    digitalWrite(D3, HIGH);
    delay(500);
    float t = sensor.readTemperature();
    Serial.print("Temperature: ");
    Serial.println(t);
    float h = sensor.readHumidity();
    Serial.print("Humidity: ");
    Serial.println(h);
    if (isnan(t))
    {
      t = sensor.readTemperature();
    }
    if (isnan(h))
    {
      h = sensor.readHumidity();
    }
    digitalWrite(D3, LOW);

    if (isnan(t))
    {
      ESP.restart();
    }
    if (isnan(h))
    {
      ESP.restart();
    }
    

        
    HTTPClient http;
    char requestString[255] = "";
    sprintf(requestString, "http://raspberrypi/SendValues.php?name=Temperatursensor_1&split=1&data=%f,%f", t, h);    
    http.begin(requestString);
    int httpCode = http.GET();
    Serial.println(httpCode);

    if (httpCode > 0)
    {
      String payload = http.getString();
      Serial.println(payload);
    }

    http.end();

    ESP.deepSleep(300e6); 
    delay(300000);
    ESP.reset();
  }
  else
  {
    Serial.println("");
    Serial.println("WiFi not connected");
    delay(1000);
  }
}

No comments:

Post a Comment