sorry if the code isn’t up to par, this is my first time programming an arduino, and has been a huge learning experience.
Thanks for helping!
also note that I am writing special characters back to blynk, specifically the % sign and degree symbol ° .
//#define BLYNK_DEBUG // Optional, this enables lots of prints
//#define BLYNK_PRINT Serial //this causes buffer overflow as it competes with the console serial out
#include <BlynkSimpleShieldEsp8266_HardSer.h>
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN 11
#include <SimpleTimer.h>
#include <EEPROM.h>
#include <ESP8266_HardSer.h>
#include <SPI.h>
#include <Wire.h>
float hum;
float t;
float f;
float hif;
int photocellPin = 1;
int FanRelayPin = 9;
int LightRelayPin = 8;
int photocellReading;
int luxdifference;
int addressTempHum = 0;
int addressLightThresh = 1;
int addressLightOff = 2;
long LightThresholdValue;
long LightOffValue;
long HeatThresholdValue;
int HeatDifference;
int LightOffDifference;
int ManualMode;
int ManualLights;
int ManualFan;
int LiteLEDStatus;
int FanLEDStatus;
int ManualModeStatus;
int lastButtonStateLiteLed;
int lastButtonStateFanLed;
int lastButtonStateManualMode;
const char* ssid = "xxx";
const char* password = "xxxxx";
WidgetLED led1(30); //LightLED in blynk app
WidgetLED led2(31);//FanLED in blynk app
WidgetLED led3(10);//wifi connection light
WidgetLED led4(28);//manual led for light side
//WidgetLCD lcd(9);
unsigned long previousMillis;
unsigned long waitTime = 3000;
DHT dht(DHTPIN, DHTTYPE);
//float humidity, temp_f;
int BH1750_address = 0x23;
byte buff[2];
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxx";
// Set ESP8266 Serial object
#define EspSerial Serial
ESP8266 wifi(EspSerial);
SimpleTimer timer;
void setup()
{
timer.setInterval(2000, LiteLed);
timer.setInterval(2000, FanLed);
timer.setInterval (2000, SendTempHumidity);
timer.setInterval (2000, SendDataLight); //get light sensor data
timer.setInterval (2000, LightDelta); //get delta between current and threshold settings
timer.setInterval (2000, HeatDelta); //get delta between heat index and threshold settings
timer.setInterval (2000, LightOffDelta);
timer.setInterval (2000, ManualModeLed);
ManualModeStatus = 0;
lastButtonStateLiteLed = 0;
lastButtonStateFanLed = 0;
lastButtonStateManualMode = 0;
// Set ESP8266 baud rate
EspSerial.begin(115200);
//EspSerial.begin(38400);
Serial.begin(115200);
//Serial.begin(38400);
Wire.begin();
//EEPROM.read(0);
Serial.print("LightThresholdValueFromEEPROM: ");
//Readind and sending first long.
LightThresholdValue = EEPROMReadlong(0);
Serial.println(LightThresholdValue);
//EEPROM.read(1);
Serial.print("LightOffValueFromEEPROM: ");
LightOffValue = EEPROMReadlong(4);
//Readind and sending first long.
Serial.println(LightOffValue);
//EEPROM.read(2);
Serial.print("HeatThresholdValueFromEEPROM: ");
HeatThresholdValue = EEPROMReadlong(8);
//Reading and sending first long.
Serial.println(HeatThresholdValue);
dht.begin();// initialize temperature sensor
Blynk.begin(auth, wifi, ssid, password);
//wait for blynk connection
while (Blynk.connect() == false) {
// Wait until connected
}
pinMode(8, OUTPUT); // sets the digital pin as output
digitalWrite(8,HIGH); //turn off light pin on init
pinMode(9, OUTPUT); // sets the digital pin as output
digitalWrite(9, HIGH); //turn off fan pin on init
pinMode(13, OUTPUT); // sets the digital pin as output
digitalWrite(13, LOW);
led1.off();
led2.off();
led3.off();
led4.off();
}//end setup()
//int eeprom_address = 0; //We will start writing at the first memory address
long address=0;//Starting at the first byte on the eeprom.
void SendTempHumidity() {
hum = dht.readHumidity();
t = dht.readTemperature();
f = dht.readTemperature(true);
hif = dht.computeHeatIndex(f, hum);
int value1=hum*10;
String str1;
char result1[5];
result1[0]=(value1/100)+'0';
result1[1]=((value1/10)%10)+'0';
result1[2]='.';
result1[3]=(value1%10)+'0';
result1[4]='\0';
str1 +=result1;
str1 +="%";
//char buf[str.length()+1];
char buf1[8];
str1.toCharArray(buf1,sizeof(buf1));
Blynk.virtualWrite(1, buf1);//write humidity with percent sign to virtual pin 1
Blynk.virtualWrite(11, hum);//write humidity without percent sign to virtual pin 1
int value2=f*10;
String str2;
char result2[5];
result2[0]=(value2/100)+'0';
result2[1]=((value2/10)%10)+'0';
result2[2]='.';
result2[3]=(value2%10)+'0';
result2[4]='\0';
str2 +=result2;
str2 +="°F";
//char buf[str.length()+1];
char buf2[8];
str2.toCharArray(buf2,sizeof(buf2));
Blynk.virtualWrite(3, buf2); //write temp with degree sign to virtual pin 3
Blynk.virtualWrite(13, f); //write temp without degree sign to virtual pin 3
int value3=hif*10;
String str3;
char result3[5];
result3[0]=(value3/100)+'0';
result3[1]=((value3/10)%10)+'0';
result3[2]='.';
result3[3]=(value3%10)+'0';
result3[4]='\0';
str3 +=result3;
str3 +="°F";
//char buf[str.length()+1];
char buf3[8];
str3.toCharArray(buf3,sizeof(buf3));
Blynk.virtualWrite(4, buf3); //write fahrenheit heat index with degree sign to virtual pin 4
Blynk.virtualWrite(14, hif); //write fahrenheit heat index without degree sign to virtual pin 4
if (hif >=HeatThresholdValue && ManualModeStatus != 1){
digitalWrite(FanRelayPin, LOW);
}else if (ManualModeStatus != 1) {
digitalWrite (FanRelayPin, HIGH);
}
}
void SendDataLight(){
photocellReading = analogRead(photocellPin);
Blynk.virtualWrite(2, photocellReading);
Blynk.virtualWrite(12, photocellReading);
if (photocellReading <= LightThresholdValue && photocellReading >= LightOffValue && ManualModeStatus != 1){
digitalWrite(LightRelayPin, LOW);
}else if (ManualModeStatus != 1){
digitalWrite(LightRelayPin,HIGH);
}
}
BLYNK_WRITE(V5) { //working 2/23/16
LightThresholdValue = param.asInt();
address=0;
EEPROMWritelong(address, LightThresholdValue);
Blynk.virtualWrite (16, EEPROMReadlong(0));
}
BLYNK_WRITE(V6) {
LightOffValue = param.asInt();// + 10;
address=4;
EEPROMWritelong(address, LightOffValue);//Writing first long.
Blynk.virtualWrite (17, EEPROMReadlong(4)); /
}
BLYNK_WRITE(V7) {
HeatThresholdValue = param.asInt();// + 10;
address=8;
EEPROMWritelong(address, HeatThresholdValue);//Writing first long.
Blynk.virtualWrite(15, EEPROMReadlong(8));
}
void loop()
{
Blynk.run();
timer.run();
}//end loop
void ManualModeLed(){
ManualModeStatus = digitalRead(13);
if (ManualModeStatus != lastButtonStateManualMode) {
if (ManualModeStatus == 1){
led3.on();
} else
led3.off();
}
lastButtonStateManualMode = ManualModeStatus;
}
/*void ManualModeLed2(){
ManualModeStatus = digitalRead(13);
Serial.println(" ");
Serial.print("ManualMode: ");
Serial.println(ManualModeStatus);
if (ManualModeStatus != lastButtonStateManualMode) {
if (ManualModeStatus == 1){
led4.on();
} else
led4.off();
}
lastButtonStateManualMode = ManualModeStatus;
}
BLYNK_WRITE(8) //light
{
if (param.asInt()) {
digitalWrite(8, LOW);
} else {
digitalWrite(8, HIGH);
}
}
BLYNK_WRITE(9) //fan
{
if (param.asInt()) {
digitalWrite(9, LOW);
} else {
digitalWrite(9, HIGH);
}
}
void FanLed(){
FanLEDStatus = digitalRead(9);
if (FanLEDStatus != lastButtonStateFanLed) {
if (FanLEDStatus == 1){
led2.off();
} else
led2.on();
}
lastButtonStateFanLed = FanLEDStatus;
}
void LiteLed(){
LiteLEDStatus = digitalRead(8);
if (LiteLEDStatus != lastButtonStateLiteLed) {
if (LiteLEDStatus == 1){
led1.off();
} else
led1.on();
}
lastButtonStateLiteLed = LiteLEDStatus;
}
//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to address + 3....no decimals
void EEPROMWritelong(int address, long value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
//This function will return a 4 byte (32bit) long from the eeprom
//at the specified address to address + 3.
long EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
void LightDelta(){
if (LightThresholdValue <= photocellReading){
luxdifference = photocellReading - LightThresholdValue;
}else {
luxdifference = LightThresholdValue - photocellReading;
}
Blynk.virtualWrite (19, luxdifference);
}
void HeatDelta(){
if (hif>=HeatThresholdValue){
HeatDifference = hif - HeatThresholdValue;
}else{
HeatDifference = HeatThresholdValue - hif;
}
Blynk.virtualWrite (18, HeatDifference);
}
void LightOffDelta(){
if (photocellReading>=LightOffValue){
LightOffDifference = photocellReading - LightOffValue;
}else{
LightOffDifference = LightOffValue - photocellReading;
}
Blynk.virtualWrite (20, LightOffDifference);
}