Advertisement
macca-nz

Blink 2.0 example

Jul 13th, 2021 (edited)
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Contents of "credentials.h"
  3.  
  4.  // You should get Auth Token in the Blynk App.
  5.  // Go to the Project Settings (nut icon).
  6.  #define BLYNK_TEMPLATE_ID "Template code here"
  7.  #define BLYNK_DEVICE_NAME "Device Name here"
  8.  
  9.  char auth[] = "Device key here";
  10.  
  11.  // Your WiFi credentials.
  12.  char ssid[] = "your WiFi ssid";
  13.  char pass[] = "your WiFi password";
  14.  */
  15.  
  16. #include "credentials.h"
  17. #define BLYNK_FIRMWARE_VERSION        "0.1.0"
  18. #define BLYNK_PRINT Serial
  19. #define MODE 13                       //MODE is either "PGM" (Always ON) or "Deep Sleep"
  20.  
  21. #include <Adafruit_Sensor.h>
  22. #include <Adafruit_BME280.h>
  23. #include <BlynkSimpleEsp8266_SSL.h>
  24. //#include <BlynkSimpleEsp32_SSL.h>
  25. #include <Wire.h>
  26. #include <TimeLib.h>
  27. #include <WidgetRTC.h>
  28. #include <utility/BlynkDateTime.h>
  29. #include <HCSR04.h>                   //This library has the TRIGGER delay increased to 20
  30. HCSR04 hc(12,14);                     //Init an Ultrasonic object ESP32 (TRIG, ECHO)
  31. const int LED = LED_BUILTIN;
  32.  
  33. ADC_MODE(ADC_VCC);
  34. BlynkTimer timer;
  35. WidgetRTC rtc;
  36.  
  37. bool pgm = false;
  38. String pgm_mode;
  39.  
  40. //#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
  41. #define SLEEP_TIME_SEC  300        /* Time ESP will go to sleep (in seconds) */
  42.  
  43. //BME object on the default I2C pins
  44. Adafruit_BME280 bme;
  45. #define pressure_offset 3.9
  46.  
  47. const int numDistReads = 10;
  48. float temp, hum, pres, volts, distReads[numDistReads], totalDist = 0, averageDist = 0, distance;
  49. int readDistIndex = 0;
  50.  
  51. void myTimerEvent(){
  52.   digitalWrite(LED, LOW);    //Visually see how long the loop takes
  53.  
  54.   getSensorVals();          //Read Sensors
  55.  
  56.   if(pgm){
  57.     pgm_mode = ("true");
  58.   }else{
  59.     pgm_mode = ("false");
  60.   }
  61.  
  62.   char time_buf[24]; // size big enough for string
  63.   sprintf(time_buf,"%02u/%02u/%02u  \xe2\x81\x91  %02u:%02u:%02u", day(),month(),year(),hour(),minute(),second());
  64.  
  65.   //BLYNK Virtual Pin Assignments
  66.   Blynk.virtualWrite(V1, pres);
  67.   Blynk.virtualWrite(V2, hum);
  68.   Blynk.virtualWrite(V3, temp);
  69.   Blynk.virtualWrite(V4, time_buf);
  70.   Blynk.virtualWrite(V5, volts);
  71.   Blynk.virtualWrite(V6, distance);
  72.   Blynk.virtualWrite(V7, pgm_mode);
  73.   Blynk.virtualWrite(V8, averageDist);
  74.    
  75.   if(pgm){
  76.     Serial.println("---------------------------------------------------------------------------------------------------------------");
  77.     Serial.print("PGM MODE: "); Serial.print(pgm_mode);Serial.print("\t\t");
  78.     Serial.print("Vcc: ");Serial.print(volts);Serial.print("v \t\t");
  79.     Serial.print("Time Stamp: ");
  80.     Serial.println(time_buf);
  81.     Serial.print("Temperature: ");Serial.print(temp, 1);Serial.print(" \xe2\x84\x83\t");
  82.     Serial.print("Humidity: ");Serial.print(hum, 1);Serial.print("% \t");
  83.     Serial.print("Pressure: ");Serial.print(pres, 0);Serial.print("hPa \t");
  84.     Serial.print("Distance: ");Serial.print(distance, 1);Serial.print("mm \t");
  85.     Serial.print("Dist Avg: ");Serial.print(averageDist, 1);Serial.println("mm");
  86.     Serial.println("---------------------------------------------------------------------------------------------------------------");
  87.     Serial.println("");
  88.     digitalWrite(LED, HIGH);  
  89.     return;
  90.   }else{
  91.     Serial.println("Going to Sleep Now............");Serial.println("");
  92.     Serial.flush();
  93.     delay(500);                    //Small delay to let update finish
  94.     digitalWrite(LED, HIGH);
  95.     ESP.deepSleep(SLEEP_TIME_SEC * 1000000L);  //Create sleep period ESP8266
  96.     //esp_deep_sleep_start();         //30 second Deep Sleep Start ESP32
  97.     }
  98. }
  99.  
  100. void getSensorVals(){
  101.  
  102.   //Configure MODE state IE Always ON for OTA programming or normal run in deep sleep mode
  103.   if(digitalRead(MODE) == LOW){
  104.     pgm = true;
  105.     averageDist = 0; totalDist = 0;            //Zero float calculation values in "PGM" mode
  106.     for(int i = 0; i < numDistReads; i++){     //Zero Array Values in "PGM" mode
  107.     distReads[i] = ((float)0.0);
  108.     }
  109.   }else{
  110.     pgm = false;
  111.   }
  112.    
  113.   //Smoothing Array for Distance reading
  114.   for (int thisReading = 0; thisReading < numDistReads; thisReading++) {
  115.     distReads[thisReading] = ((float)hc.dist()*10);
  116.     totalDist = totalDist + distReads[thisReading];
  117.     delay(40);                                          //This delay helps Sensor processing and avoids false readings
  118.   }
  119.   averageDist = totalDist / numDistReads;               //Creates Average from 10 samples
  120.  
  121.   //Vcc, BME280 Weather and HC-SR04 Sensor Reads
  122.   temp = bme.readTemperature();
  123.   hum = bme.readHumidity();
  124.   pres = round((bme.readPressure()/100.0F) + pressure_offset);
  125.   distance = ((float)hc.dist()*10);
  126.   delay(40);                                            //This delay helps Sensor processing and avoids false readings
  127.   volts = ((float)ESP.getVcc()/1000);
  128.  
  129.   return;
  130. }
  131.  
  132. BLYNK_CONNECTED() {
  133.   // Synchronize time on connection
  134.   rtc.begin();
  135. }
  136.  
  137. void setup()
  138. {
  139.   // Debug console
  140.   Serial.begin(115200);
  141.   pinMode(LED, OUTPUT);
  142.   digitalWrite(LED, LOW);           //Visually see how long the loop takes
  143.   pinMode(MODE, INPUT_PULLUP);      //Sleep mode = LOW, pgm mode = HIGH
  144.  
  145.     if (! bme.begin(0x76, &Wire)) {
  146.         Serial.println("Could not find a valid BME280 sensor, check wiring!");
  147.         while (1);
  148.     }
  149.    
  150.   //Serial.println("ESP Awake Now............");
  151.   Blynk.begin(auth, ssid, pass);
  152.  
  153.  
  154.   // You can also specify server:
  155.   //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  156.   //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  157.   while (Blynk.connect() == false) {
  158.     // Wait until connected
  159.   }
  160.  
  161.   setSyncInterval(30 * 60);                             //Sync RTC interval in seconds (30 minutes)
  162.  
  163.   // Setup a function to be called every 60 seconds
  164.   timer.setInterval(60000L, myTimerEvent);              //run update every minute when in "Allways ON mode"
  165.  
  166.   //Setup Sleep Timer
  167.   //esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //ESP32
  168.   Serial.println(""); Serial.println("Blynk Connected............"); Serial.println("");
  169.   digitalWrite(LED, HIGH);    //Visually see how long the loop takes
  170.   myTimerEvent();             //run the event after setup
  171. }
  172.  
  173. void loop()
  174. {
  175.   Blynk.run();
  176.   timer.run(); // Initiates BlynkTimer
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement