Skip to content

Instantly share code, notes, and snippets.

@unwiredtech
Created July 8, 2022 07:13
Show Gist options
  • Save unwiredtech/2bfe69bd4026114e8b971d64b6e0ba25 to your computer and use it in GitHub Desktop.
Save unwiredtech/2bfe69bd4026114e8b971d64b6e0ba25 to your computer and use it in GitHub Desktop.
/*
-----------------------
ESP8266 Monitor its Own Battery Level using IoT
-----------------------
Voltage Monitoring in Volts and Percentage
Temperate and Humidity Sensor
*/
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPLkoUaNN29"
#define BLYNK_DEVICE_NAME "Smart IOT"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI
#include "BlynkEdgent.h"
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define DHTTYPE DHT22 // DHT 22
#define DHTPIN D4 //DHT22 Pin D4(GPIO 2)
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
float voltage;
int bat_percentage;
int analogInPin = A0; // Analog input pin
int sensorValue;
float calibration = 7.9; // Check Battery voltage using multimeter & add/subtract the value
void setup()
{
Serial.begin(115200);
delay(1000);
BlynkEdgent.begin();
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
display.setTextColor(WHITE);
delay(100);
}
void loop() {
BlynkEdgent.run();
float t = dht.readTemperature();
float h = dht.readHumidity();
sensorValue = analogRead(analogInPin);
voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration); //multiply by two as voltage divider network is 100K & 100K Resistor
bat_percentage = mapfloat(voltage, 10, 13.6, 0, 100); //12Volt LiFePo4 Setup
if (bat_percentage >= 100)
{
bat_percentage = 100;
}
if (bat_percentage <= 0)
{
bat_percentage = 1;
}
//send data to blynk
Blynk.virtualWrite(V0, bat_percentage); // for battery voltage
Blynk.virtualWrite(V1, voltage); // for battery percentage
Blynk.virtualWrite(V3, t); //for Temperature
Blynk.virtualWrite(V4, h); //for Humidity
//Print data on serial monitor
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
Serial.print("Analog Value = ");
Serial.println(sensorValue);
Serial.print("Output Voltage = ");
Serial.println(voltage);
Serial.print("Battery Percentage = ");
Serial.println(bat_percentage);
Serial.println();
Serial.println("****************************");
Serial.println();
delay(20000);
if (bat_percentage <=50)
{
Serial.println("Battery level below 50%, Charge battery on time");
//send notification
Blynk.logEvent("battery_low", "Battery is getting low.... Plugin to charge") ;
delay(20000);
}
if (bat_percentage <= 20)
{
Serial.println("Battery level below 20%, Charge battery on time");
//send notification
Blynk.logEvent("battery_low", "Battery is in critical level....") ;
delay(20000);
}
// display temperature on OLED
// display.clearDisplay();
// display.setTextColor(WHITE);
// display.setTextSize(1);
// display.setCursor(0, 0);
// display.print("Temperature: ");
// display.setTextSize(2);
// display.setCursor(0, 10);
// display.print(t);
// display.print(" ");
// display.setTextSize(1);
// display.cp437(true);
// display.write(167);
// display.setTextSize(2);
// display.print("C");
// display humidity on OLED
// display.setTextSize(1);
// display.setCursor(0, 35);
// display.print("Humidity: ");
// display.setTextSize(2);
// display.setCursor(0, 45);
// display.print(h);
// display.print(" %");
// display.display();
//delay(1500);
}
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment