Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Contents of "credentials.h"
- // You should get Auth Token in the Blynk App.
- // Go to the Project Settings (nut icon).
- #define BLYNK_TEMPLATE_ID "Template code here"
- #define BLYNK_DEVICE_NAME "Device Name here"
- char auth[] = "Device key here";
- // Your WiFi credentials.
- char ssid[] = "your WiFi ssid";
- char pass[] = "your WiFi password";
- */
- #include "credentials.h"
- #define BLYNK_FIRMWARE_VERSION "0.1.0"
- #define BLYNK_PRINT Serial
- #define MODE 13 //MODE is either "PGM" (Always ON) or "Deep Sleep"
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #include <BlynkSimpleEsp8266_SSL.h>
- //#include <BlynkSimpleEsp32_SSL.h>
- #include <Wire.h>
- #include <TimeLib.h>
- #include <WidgetRTC.h>
- #include <utility/BlynkDateTime.h>
- #include <HCSR04.h> //This library has the TRIGGER delay increased to 20
- HCSR04 hc(12,14); //Init an Ultrasonic object ESP32 (TRIG, ECHO)
- const int LED = LED_BUILTIN;
- ADC_MODE(ADC_VCC);
- BlynkTimer timer;
- WidgetRTC rtc;
- bool pgm = false;
- String pgm_mode;
- //#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
- #define SLEEP_TIME_SEC 300 /* Time ESP will go to sleep (in seconds) */
- //BME object on the default I2C pins
- Adafruit_BME280 bme;
- #define pressure_offset 3.9
- const int numDistReads = 10;
- float temp, hum, pres, volts, distReads[numDistReads], totalDist = 0, averageDist = 0, distance;
- int readDistIndex = 0;
- void myTimerEvent(){
- digitalWrite(LED, LOW); //Visually see how long the loop takes
- getSensorVals(); //Read Sensors
- if(pgm){
- pgm_mode = ("true");
- }else{
- pgm_mode = ("false");
- }
- char time_buf[24]; // size big enough for string
- sprintf(time_buf,"%02u/%02u/%02u \xe2\x81\x91 %02u:%02u:%02u", day(),month(),year(),hour(),minute(),second());
- //BLYNK Virtual Pin Assignments
- Blynk.virtualWrite(V1, pres);
- Blynk.virtualWrite(V2, hum);
- Blynk.virtualWrite(V3, temp);
- Blynk.virtualWrite(V4, time_buf);
- Blynk.virtualWrite(V5, volts);
- Blynk.virtualWrite(V6, distance);
- Blynk.virtualWrite(V7, pgm_mode);
- Blynk.virtualWrite(V8, averageDist);
- if(pgm){
- Serial.println("---------------------------------------------------------------------------------------------------------------");
- Serial.print("PGM MODE: "); Serial.print(pgm_mode);Serial.print("\t\t");
- Serial.print("Vcc: ");Serial.print(volts);Serial.print("v \t\t");
- Serial.print("Time Stamp: ");
- Serial.println(time_buf);
- Serial.print("Temperature: ");Serial.print(temp, 1);Serial.print(" \xe2\x84\x83\t");
- Serial.print("Humidity: ");Serial.print(hum, 1);Serial.print("% \t");
- Serial.print("Pressure: ");Serial.print(pres, 0);Serial.print("hPa \t");
- Serial.print("Distance: ");Serial.print(distance, 1);Serial.print("mm \t");
- Serial.print("Dist Avg: ");Serial.print(averageDist, 1);Serial.println("mm");
- Serial.println("---------------------------------------------------------------------------------------------------------------");
- Serial.println("");
- digitalWrite(LED, HIGH);
- return;
- }else{
- Serial.println("Going to Sleep Now............");Serial.println("");
- Serial.flush();
- delay(500); //Small delay to let update finish
- digitalWrite(LED, HIGH);
- ESP.deepSleep(SLEEP_TIME_SEC * 1000000L); //Create sleep period ESP8266
- //esp_deep_sleep_start(); //30 second Deep Sleep Start ESP32
- }
- }
- void getSensorVals(){
- //Configure MODE state IE Always ON for OTA programming or normal run in deep sleep mode
- if(digitalRead(MODE) == LOW){
- pgm = true;
- averageDist = 0; totalDist = 0; //Zero float calculation values in "PGM" mode
- for(int i = 0; i < numDistReads; i++){ //Zero Array Values in "PGM" mode
- distReads[i] = ((float)0.0);
- }
- }else{
- pgm = false;
- }
- //Smoothing Array for Distance reading
- for (int thisReading = 0; thisReading < numDistReads; thisReading++) {
- distReads[thisReading] = ((float)hc.dist()*10);
- totalDist = totalDist + distReads[thisReading];
- delay(40); //This delay helps Sensor processing and avoids false readings
- }
- averageDist = totalDist / numDistReads; //Creates Average from 10 samples
- //Vcc, BME280 Weather and HC-SR04 Sensor Reads
- temp = bme.readTemperature();
- hum = bme.readHumidity();
- pres = round((bme.readPressure()/100.0F) + pressure_offset);
- distance = ((float)hc.dist()*10);
- delay(40); //This delay helps Sensor processing and avoids false readings
- volts = ((float)ESP.getVcc()/1000);
- return;
- }
- BLYNK_CONNECTED() {
- // Synchronize time on connection
- rtc.begin();
- }
- void setup()
- {
- // Debug console
- Serial.begin(115200);
- pinMode(LED, OUTPUT);
- digitalWrite(LED, LOW); //Visually see how long the loop takes
- pinMode(MODE, INPUT_PULLUP); //Sleep mode = LOW, pgm mode = HIGH
- if (! bme.begin(0x76, &Wire)) {
- Serial.println("Could not find a valid BME280 sensor, check wiring!");
- while (1);
- }
- //Serial.println("ESP Awake Now............");
- Blynk.begin(auth, ssid, pass);
- // You can also specify server:
- //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
- //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
- while (Blynk.connect() == false) {
- // Wait until connected
- }
- setSyncInterval(30 * 60); //Sync RTC interval in seconds (30 minutes)
- // Setup a function to be called every 60 seconds
- timer.setInterval(60000L, myTimerEvent); //run update every minute when in "Allways ON mode"
- //Setup Sleep Timer
- //esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //ESP32
- Serial.println(""); Serial.println("Blynk Connected............"); Serial.println("");
- digitalWrite(LED, HIGH); //Visually see how long the loop takes
- myTimerEvent(); //run the event after setup
- }
- void loop()
- {
- Blynk.run();
- timer.run(); // Initiates BlynkTimer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement