Created
July 4, 2022 12:48
-
-
Save unwiredtech/729d8c527540d9eb9dc0325f9315dd91 to your computer and use it in GitHub Desktop.
arduino-voltage-monitor-12V
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
String apiKey = "DT5E8MZSXYCUBLMD"; | |
const char* ssid = "yourssid"; // Enter your WiFi Network's SSID | |
const char* pass = "yourwifipassword"; // Enter your WiFi Network's Password | |
const char* server = "api.thingspeak.com"; | |
int analogInPin = A0; // Analog input pin | |
int sensorValue; // Analog Output of Sensor | |
float calibration = 8.21; // Check Battery voltage using multimeter & add/subtract the value | |
int bat_percentage; | |
WiFiClient client; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(1000); | |
Serial.print("*"); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
} | |
void loop() | |
{ | |
sensorValue = analogRead(analogInPin); | |
float voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration); //multiply by two as voltage divider network is 100K & 100K Resistor | |
bat_percentage = mapfloat(voltage, 2.9, 3.65, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage | |
if (bat_percentage >= 100) | |
{ | |
bat_percentage = 100; | |
} | |
if (bat_percentage <= 0) | |
{ | |
bat_percentage = 1; | |
} | |
Serial.print("Analog Value = "); | |
Serial.print(sensorValue); | |
Serial.print("\t Output Voltage = "); | |
Serial.print(voltage); | |
Serial.print("\t Battery Percentage = "); | |
Serial.println(bat_percentage); | |
delay(1000); | |
if (client.connect(server, 80)) | |
{ | |
String postStr = apiKey; | |
postStr += "&field1="; | |
postStr += String(voltage); | |
postStr += "&field2="; | |
postStr += String(bat_percentage); | |
postStr += "\r\n\r\n"; | |
client.print("POST /update HTTP/1.1\n"); | |
delay(5000); | |
client.print("Host: api.thingspeak.com\n"); | |
delay(5000); | |
client.print("Connection: close\n"); | |
delay(5000); | |
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); | |
delay(5000); | |
client.print("Content-Type: application/x-www-form-urlencoded\n"); | |
delay(5000); | |
client.print("Content-Length: "); | |
delay(5000); | |
client.print(postStr.length()); | |
delay(5000); | |
client.print("\n\n"); | |
delay(5000); | |
client.print(postStr); | |
delay(5000); | |
} | |
client.stop(); | |
Serial.println("Sending...."); | |
delay(15000); | |
} | |
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