#include <WiFi.h> //
#include <WiFiClient.h> //Network - Wifi - Blynk
// #include <BlynkSimpleEsp32.h> //
#include <driver/adc.h> //MISC
#include <HardwareSerial.h> //PM
#include <Adafruit_SHT31.h> //SHT
#include "PMS.h" //PM
#define DEBUG
#ifdef DEBUG
#define debugln(info) Serial.println(info)
#define debug(info) Serial.print(info)
#else
#define debugln(info)
#define debug(info)
#endif
// // Network - Blnyk
// #define BLYNK_TEMPLATE_ID "TMPL6qrUULuBh"
// #define BLYNK_TEMPLATE_NAME "Quickstart Template"
// #define BLYNK_AUTH_TOKEN "vPuCD0uubZ_NLts7IPru6ZepSBHhy-Dp"
// Sensor - ADC - MISC
#define ADC_MAX_VALUE 4095
#define ADC_MIN_VALUE 0
#define ADC_SAMPLES 3
#define ADC_SAMPLE_DELAY 100
#define MSIC_CO_MIN 0
#define MSIC_CO_MAX 1000
#define MSIC_NO2_MIN 0
#define MSIC_NO2_MAX 1000
#define MAP(x, out_min, out_max) \
((x - (float)ADC_MIN_VALUE) * (out_max - out_min) / (float)(ADC_MAX_VALUE - ADC_MIN_VALUE) + out_min)
//==================================================================================================================
// // Network - Wifi _ Blynk
// const char *ssid = "MBW";
// const char *pass = "billy2023";
// BlynkTimer timer;
// Sensor - MSIC
adc1_channel_t adc_channel[3] = {ADC1_CHANNEL_4, // co
ADC1_CHANNEL_6, // no2
ADC1_CHANNEL_7}; // nh3
float co = 0, no2 = 0, nh3 = 0;
// Sensor - SHT
Adafruit_SHT31 sht31 = Adafruit_SHT31();
float temperature = 0.0, hum = 0.0;
// Sensor - PMS
PMS pms(Serial2);
PMS::DATA data;
String pm1, pm2_5, pm10;
//==================================================================================================================
// Init
void setup()
{
// Debug
#ifdef DEBUG
Serial.begin(9600);
#endif
// Init - Sensor - I2C - SHT
debugln("[debug] SHT31 test....");
if (!sht31.begin(0x44))
{ // Set to 0x45 for alternate i2c addr
debugln("[debug] Couldn't find SHT31!");
while (1)
delay(1);
}
// // Init - Network - Wifi - Blynk
// Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// timer.setInterval(10000, sendSensor);
// Init - Sensor - ADC - MISC
adc1_config_width(ADC_WIDTH_BIT_12);
for (unsigned int i = 0; i < sizeof(adc_channel) / sizeof(int); i++)
adc1_config_channel_atten(adc_channel[i], ADC_ATTEN_DB_11);
// Init - Sensor - USART - PMS
Serial2.begin(9600);
// Wait for all Init to finish its job
delay(500);
}
// Run
void loop()
{
// Network - Blynk
// Blynk.run();
// timer.run();
}
void sendSensor()
{
//===================================================================================
// Sensor - MSIC
unsigned int temp[3] = {0, 0, 0};
unsigned int count = 0;
// Repeat for n samples
while ((count < ADC_SAMPLES))
{
// Delay samples
if (count > 0)
delay(ADC_SAMPLE_DELAY);
// Read sensor - 3 channel
for (unsigned int i = 0; i < sizeof(adc_channel) / sizeof(int); i++)
{
// Read i channel
unsigned int t = adc1_get_raw(adc_channel[i]);
debugln("[msic] Channel " + String(i) + " = " + String(t));
// Filter if it's not in range
if (!((t >= ADC_MIN_VALUE) && (t <= ADC_MAX_VALUE)))
{
goto SKIP_MSIC;
}
// Add
temp[i] += t;
}
// Count number of reading sensor
count++;
}
// Average
co = (float)(temp[0] / ADC_SAMPLES);
co = MAP(co, MSIC_CO_MIN, MSIC_CO_MAX);
no2 = (float)(temp[1] / ADC_SAMPLES);
no2 = MAP(no2, MSIC_NO2_MIN, MSIC_NO2_MAX);
// nh3 = (float)(temp[2] / ADC_SAMPLES);
SKIP_MSIC:
//===================================================================================
// Sensor - PM
if (pms.readUntil(data))
{
pm1 = data.PM_AE_UG_1_0;
pm2_5 = data.PM_AE_UG_2_5;
pm10 = data.PM_AE_UG_10_0;
debug("[pm] PM 1.0 (ug/m3): ");
debugln(data.PM_AE_UG_1_0);
debug("[pm] PM 2.5 (ug/m3): ");
debugln(data.PM_AE_UG_2_5);
debug("[pm] PM 10.0 (ug/m3): ");
debugln(data.PM_AE_UG_10_0);
}
else
{
debugln("[pm] No PMS data!");
}
//===================================================================================
// Sensor - SHT
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (!isnan(t))
{ // check if 'is not a number'
debug("[sht] Temp *C = ");
debug(t);
debugln("\t\t");
temperature = t;
}
else
{
debugln("[sht] Failed to read temperature");
}
if (!isnan(h))
{ // check if 'is not a number'
debug("[sht] Hum. % = ");
debugln(h);
hum = h;
}
else
{
debugln("[sht] Failed to read humidity");
}
// Blynk.virtualWrite(V0, pm1);
// Blynk.virtualWrite(V1, pm2_5);
// // Blynk.virtualWrite(V2, pm10);
// Blynk.virtualWrite(V3, hum);
// Blynk.virtualWrite(V4, temperature);
// Blynk.virtualWrite(V5, no2);
// // Blynk.virtualWrite(V6, nh3);
// Blynk.virtualWrite(V7, co);
}