India Automation Challenge 2021
OR
Esp32 Dev kit V1,4-Channel Relay,Lithium-ion Batteries,TP4056 Li-On Battery Charging Module, 12V led lights, Jumper Wires, CPU fan as AC.
#include <ESP8266WiFi.h> ........................................................................................................................................................................................... #ifdef ARDUINO_ARCH_ESP32 #define RelayPin1 5 int analogInPin = A0; boolean wifiConnected = false; Espalexa espalexa; void setup() pinMode(RelayPin1, OUTPUT); if (wifiConnected) espalexa.begin(); void loop() void secondLightChanged(uint8_t brightness) void thirdLightChanged(uint8_t brightness) void fourthLightChanged(uint8_t brightness) void fifthLightChanged(uint8_t brightness) WiFi.mode(WIFI_STA);
String apiKey = "M3JBXHIQBZYUBZXA";
const char* ssid = "Mainak's Wifi Zone";
const char* pass = "aprimtechindiaprivatelimitedmainak8";
const char* server = "api.thingspeak.com";
int analogInPin = A0;
int sensorValue;
float calibration = 0.36;
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(100);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
sensorValue = analogRead(analogInPin);
float voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration);
bat_percentage = mapfloat(voltage, 2.8, 4.2, 0, 100);
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);
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <Espalexa.h>
#define RelayPin2 4
#define RelayPin3 13
#define RelayPin4 14
#define RelayPin5 12
int sensorValue;
float calibration = 0.36;
int bat_percentage;
boolean connectWifi();
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);
void fourthLightChanged(uint8_t brightness);
void fifthLightChanged(uint8_t brightness);
const char* ssid = "Mainak's Wifi Zone";
const char* password = "aprimtechindiaprivatelimitedmainak8";
String Device_1_Name = "AC";
String Device_2_Name = "Back Light";
String Device_3_Name = "Fan";
String Device_4_Name = "Front Light";
String Device_5_Name = "Vehicle";
{
Serial.begin(115200);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(RelayPin5, OUTPUT);
wifiConnected = connectWifi();
{
espalexa.addDevice(Device_1_Name, firstLightChanged);
espalexa.addDevice(Device_2_Name, secondLightChanged);
espalexa.addDevice(Device_3_Name, thirdLightChanged);
espalexa.addDevice(Device_4_Name, fourthLightChanged);
espalexa.addDevice(Device_5_Name, fifthLightChanged);
}
else
{
while (1)
{
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
delay(2500);
}
}
}
{
espalexa.loop();
delay(1);
}
void firstLightChanged(uint8_t brightness)
{
if (brightness == 255)
{
digitalWrite(RelayPin1, LOW);
Serial.println("Device1 OFF");
}
else
{
digitalWrite(RelayPin1, HIGH);
Serial.println("Device1 ON");
}
}
{
if (brightness == 255)
{
digitalWrite(RelayPin2, LOW);
Serial.println("Device2 OFF");
}
else
{
digitalWrite(RelayPin2, HIGH);
Serial.println("Device2 ON");
}
}
{
if (brightness == 255)
{
digitalWrite(RelayPin3, LOW);
Serial.println("Device3 OFF");
}
else
{
digitalWrite(RelayPin3, HIGH);
Serial.println("Device3 ON");
}
}
{
if (brightness == 255)
{
digitalWrite(RelayPin4, LOW);
Serial.println("Device4 OFF");
}
else
{
digitalWrite(RelayPin4, HIGH);
Serial.println("Device4 ON");
}
}
{
if (brightness == 255)
{
digitalWrite(RelayPin5, LOW);
Serial.println("Device5 OFF");
}
else
{
digitalWrite(RelayPin5, HIGH);
Serial.println("Device5 ON");
}
}
boolean connectWifi()
{
boolean state = true;
int i = 0;
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 20) {
state = false; break;
}
i++;
}
Serial.println("");
if (state) {
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("Connection failed.");
}
return state;
}