i using this code with an ESP32 and blynk.
i use blynk version 1.3.2 installed in arduino.
newest blynk app on android.
why is my esp32 going offline after a day ?
by the way im building a automated monotube if someone whanna know.
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h> // I2C library
#include "Adafruit_SGP30.h"
#include <TimeLib.h>
#include <WidgetRTC.h>
#include "Adafruit_SHTC3.h"
#define VIN 3.3 // V power voltage
#define R 10000 //ohm resistance value
//#define TEMP_UPPER_THRESHOLD 23 // upper temperature threshold
//#define ALARM_THRESHOLD_BLYNK 26 // Alarm to app
#define LUX_SENSOR_THRESHOLD 3200 //LUX THERSHOLD
#define LED_MONOTUB_DIMMER 0 //PWM 0-1024
#define Co2_UPPER_THRESHOLD 22000 // Co2 threshold
#define Co2_MIDLE_THRESHOLD 20000 // Co2 threshold
#define Co2_LOWER_THRESHOLD 19500 // Co2 threshold
#define Co2_LOWEST_THRESHOLD 800 // Co2 threshold
#define ONE_WIRE_BUS 4
#define tvoc_topic "sensor/tvoc"
#define eco2_topic "sensor/eco2"
Adafruit_SHTC3 shtc3 = Adafruit_SHTC3();
Adafruit_SGP30 sgp;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
BlynkTimer timer;
WidgetRTC rtc;
char ssid[] = ""; // type your wifi name
char pass[] = ""; // type your wifi password
const int sensorPin = 34;
int sensorVal; // Analog value from the sensor
int lux; //Lux value
// int pinValue_A; // temperature sensor
float val1;
float temperature;
float HOUR;
float MIN;
float h1;
float t1;
//unsigned long previousMillis1 = 0; // will store last time
//unsigned long previousMillis2 = 0; // will store last time
//unsigned long previousMillis3 = 0; // will store last time
//const long interval1 = 2500; //10s interval at which to blink (milliseconds)
//const long interval2 = 750; //20s interval at which to blink (milliseconds)
//const long interval3 = 500; //20s interval at which to blink (milliseconds)
/* return absolute humidity [mg/m^3] with approximation formula
* @param temperature [°C]
* @param humidity [%RH]
*/
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
return absoluteHumidityScaled;
}
void setup()
{
Serial.begin(115200); // starting serial monitor
delay(1500);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
sensors.begin();
rtc.begin();
sgp30setup();
shtc3.begin();
timer.setInterval(1*60*1000, connectionstatus); // check every minute
//delay(500);
}
void connectionstatus() {
if (!Blynk.connected() ) {
Serial.println("Lost Blynk server connection");
//Blynk.connect();
ESP.restart();
} else {
Serial.println("Blynk OK");
Serial.println("Blynk OK");
Serial.println("Blynk OK");
Serial.println("Blynk OK");
}
}
void sgp30setup(){
if (! sgp.begin()){
Serial.println("Sensor not found :(");
while (1);
}
Serial.print("Found SGP30 serial #");
Serial.print(sgp.serialnumber[0], HEX);
Serial.print(sgp.serialnumber[1], HEX);
Serial.println(sgp.serialnumber[2], HEX);
// If you have a baseline measurement from before you can assign it to start, to 'self-calibrate'
sgp.setIAQBaseline(0x92F9, 0x962E); // Will vary for each sensor!
//eCO2: 0x92F9 & TVOC: 0x962E
}
void readtime(void){
HOUR = float(hour());
MIN = float(minute());
Blynk.virtualWrite(V3, HOUR);
Blynk.virtualWrite(V11, MIN);
delay(1000);
//Serial.println("READ_TIME_LOOP");
}
void readlux(void){
sensorVal = analogRead(sensorPin);
lux=sensorRawToPhys(sensorVal);
Blynk.virtualWrite(V12, lux);
delay(500);
}
int sensorRawToPhys(int raw){
// Conversion rule
float Vout = float(raw) * (VIN / float(4095));// Conversion analog to voltage
float RLDR = (R * (VIN - Vout))/Vout; // Conversion voltage to resistance
int phys=500/(RLDR/1000); // Conversion resitance to lumen
return phys;
}
void tempinsoil(void){
sensors.requestTemperatures(); // send the command to get temperatures
temperature = sensors.getTempCByIndex(0);
Serial.println("SOIL_TEMP");
Serial.println("temperature");
Blynk.virtualWrite(V10, temperature);
delay(750);
}
void humandtemp(void){
sensors_event_t humidity, temp;
shtc3.getEvent(&humidity, &temp);
t1 = temp.temperature;
h1 = humidity.relative_humidity;
Blynk.virtualWrite(V2, t1);
Blynk.virtualWrite(V1, h1);
delay(1000);
}
int counter = 0;
long lastMsg = 0;
void sgp30line(void){
long now = millis();
if (now - lastMsg > 1000) {
lastMsg = now;
{
// if the analog value is high enough, turn on the relay:
// put your main code here, to run repeatedly:
// If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals
sgp.setHumidity(getAbsoluteHumidity(t1, h1));
if (! sgp.IAQmeasure()) {
Serial.println("Measurement failed");
return;
}
val1 = sgp.eCO2; // Co2 level box1
Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
Blynk.virtualWrite(V5, val1);
delay(500);
if (! sgp.IAQmeasureRaw()) {
Serial.println("Raw Measurement failed");
return;
}
Serial.print("Raw H2 "); Serial.print(sgp.rawH2); Serial.print(" \t");
Serial.print("Raw Ethanol "); Serial.print(sgp.rawEthanol); Serial.println("");
counter++;
if (counter == 30) {
counter = 0;
uint16_t TVOC_base, eCO2_base;
if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
Serial.println("Failed to get baseline readings");
return;
}
Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
}
}
}
}
void loop(){
//Serial.println("begining of main loop");
Blynk.run();
timer.run();
readtime();
humandtemp();
tempinsoil();
readlux();
sgp30line();
//Serial.println("end of main loop");
}