IoT Controlling Fridge using ESP8266 Wifi Module

Posted on Fri May 15 2020 00:00:00 GMT+0530 (India Standard Time)

Tagged : IoT


After setting up the mind to Control Fridge using my Smart Phone. I downloaded Arduino and saw the examples they gave given for ESP8266 Wifi Module.

There are many examples given in Arduino.

I made the Simple Home Controller API in MongoDB where we can set the status of the Equipment (Fridge).

Here's the look

Pic : Device Off & On Status

Then, I grabbed the examples that is given in Arduino and customized to my need. Here is it.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>

#include <WiFiClientSecureBearSSL.h>
const uint8_t fingerprint[20] = {0x3E, 0x42, 0x9E, 0x36, 0x5D, 0x25, 0x07, 0xB3, 0x77, 0xB4, 0xAC, 0x7C, 0x26, 0xD7, 0x6F, 0x9A, 0xCD, 0xEB, 0x7B, 0x32};
ESP8266WiFiMulti WiFiMulti;

//const char* ssid = "SULTHAN";
//const char* password = "9042445010";
const char* ssid = "SA";
const char* password = "1234567890";
//int wait_time = 10000; // Prod timing
int wait_time = 2000;
int inputPin = D6;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(inputPin, OUTPUT);      // set pin as input
  // Setting power off at initial stage
  digitalWrite(inputPin,HIGH);
  digitalWrite(LED_BUILTIN, HIGH);  // sets the LED to the button's value
  Serial.begin(9600);
  Serial.println();
  Serial.println();
  Serial.println();
  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] Wait %d...\n", t);
    Serial.flush();
    delay(1000);
  }
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(ssid, password);
}
void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {
    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
    client->setFingerprint(fingerprint);
    HTTPClient https;
    if (https.begin(*client, "http://sulthanallaudeen.com:3000/getDeviceStatus")) {  // HTTPS
      int httpCode = https.GET();
      if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
          if(payload=="on"){
            digitalWrite(inputPin,LOW);
            digitalWrite(LED_BUILTIN, LOW);  // sets the LED to the button's value
            Serial.println("Turn on");
          } else if (payload=="off"){
            Serial.println("Turn off");
            digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
            digitalWrite(inputPin,HIGH);           
          } else {
            Serial.println("Invalid status");
            }

        }
      } else {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }
  String console = "Wait for " + (String)wait_time + "s before next round...";
  Serial.println(console);
  delay(wait_time); // Wait for 10 seconds
}

What it does ? 

Step 1: Generated fingerprint as my website is https

Step 2: Connect to wifi (loop until it gets connected)

Step 3: Hit the API to get Device Status ( Do it in a particular interval, Say 1 minute) 

Step 4: Based on the Response, turn on or turn off the switch (Power Relay which is connected to the Fridge)

So, now I can set the Device's status from my Mobile that will be watched periodically by the ESP and the preference will be done to the connected Equipment.

 

Note : Instead of trying to Fridge you can simulate the same to low power equipment for a safety measure

Search
Side Widget
You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!