Electorials

ESP8266 Weather Station v2

Aug 5th, 2021 (edited)
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. /*
  2.  * ESP8266 Weather Station v2
  3.  * Karl Ng - 06/08/2021
  4.  *
  5.  * Youtube Video: https://youtu.be/39G71TzMNTA
  6.  * Website: https://www.electorials.com
  7.  * Contact Email: [email protected]
  8.  */
  9.  
  10. #include <ESP8266WiFi.h>
  11. #include <time.h>
  12. #include <U8x8lib.h>
  13. #include "DFRobot_BME280.h"
  14.  
  15. const char* ssid = "WIFI";              
  16. const char* password = "PASSWORD";      
  17.  
  18. const char* NTP_SERVER = "ch.pool.ntp.org";
  19. const char* TZ_INFO    = "GMT+0BST-1,M3.5.0/01:00:00,M10.5.0/02:00:00";  // enter your time zone (https://remotemonitoringsystems.ca/time-zone-abbreviations.php)
  20.  
  21. tm timeinfo;
  22. time_t now;
  23. long unsigned lastNTPtime;
  24. unsigned long lastEntryTime;
  25.  
  26. U8X8_SH1106_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // OLEDs without Reset of the Display
  27.  
  28. typedef DFRobot_BME280_IIC    BME;    //
  29. BME   bme(&Wire, 0x77);   // select TwoWire peripheral and set sensor address
  30. #define SEA_LEVEL_PRESSURE    1015.0f
  31.  
  32. const int touchPin = D0;
  33. int touchState = 0;
  34. int pageNumber = 0;
  35.  
  36. int dustPin = D3;
  37. unsigned long duration;
  38. unsigned long starttime;
  39. unsigned long sampletime_ms = 2000;
  40. unsigned long lowpulseoccupancy = 0;
  41. float ratio = 0;
  42. float concentration = 0;
  43.  
  44. void printLastOperateStatus(BME::eStatus_t eStatus) // show last sensor operate status
  45. {
  46.   switch(eStatus) {
  47.   case BME::eStatusOK:    Serial.println("everything ok"); break;
  48.   case BME::eStatusErr:   Serial.println("unknow error"); break;
  49.   case BME::eStatusErrDeviceNotDetected:    Serial.println("device not detected"); break;
  50.   case BME::eStatusErrParameter:    Serial.println("parameter error"); break;
  51.   default: Serial.println("unknow status"); break;
  52.   }
  53. }
  54.  
  55. void setup()
  56. {
  57.   pinMode(touchPin, INPUT);
  58.   pinMode(dustPin,INPUT);
  59.   starttime = millis();
  60.   u8x8.begin();
  61.   Serial.begin(9600);
  62.   WiFi.begin(ssid, password);
  63.  
  64.   int counter = 0;
  65.   while (WiFi.status() != WL_CONNECTED)
  66.   {
  67.     delay(200);    
  68.     if (++counter > 100)
  69.       ESP.restart();
  70.   }
  71.  
  72.   configTime(0, 0, NTP_SERVER);
  73.   // See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
  74.   setenv("GMT0BST,M3.5.0/1,M10.5.0", TZ_INFO, 1);
  75.  
  76.   if (getNTPtime(10))
  77.   {  
  78.     // Wait up to 10 seconds to sync
  79.   }
  80.   else
  81.   {
  82.     ESP.restart();
  83.   }
  84.   showTime(&timeinfo);
  85.   lastNTPtime = time(&now);
  86.   lastEntryTime = millis();
  87.    
  88.   bme.reset();
  89.   while(bme.begin() != BME::eStatusOK) {
  90.     printLastOperateStatus(bme.lastOperateStatus);
  91.     delay(2000);
  92.   }
  93.   delay(100);
  94. }
  95.  
  96. void loop()
  97. {
  98.   duration = pulseIn(dustPin, LOW);
  99.   lowpulseoccupancy = lowpulseoccupancy+duration;
  100.   if ((millis()-starttime) >= sampletime_ms)
  101.   {
  102.     ratio = lowpulseoccupancy/(sampletime_ms*10.0);  
  103.     concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62;
  104.     lowpulseoccupancy = 0;
  105.     starttime = millis();
  106.   }
  107.   touchState = digitalRead(touchPin);
  108.   getNTPtime(10);
  109.   showTime(&timeinfo);
  110.  
  111.   float   temp = bme.getTemperature();
  112.   uint32_t press = bme.getPressure();
  113.   float   alti = bme.calAltitude(SEA_LEVEL_PRESSURE, press);
  114.   float   humi = bme.getHumidity();
  115.  
  116.   char temp_buff[9]; char hum_buff[9]; char pres_buff[9]; char alt_buff[9]; char dust_buff[15];
  117.   char cel_buff[11] = "C";
  118.   char per_buff[11] = "%";
  119.   char pa_buff[11] = " Pa";
  120.   char met_buff[11] = "m";
  121.   char pcs_buff[11] = " pcs";
  122.  
  123.   dtostrf(temp, 3, 1, temp_buff);
  124.   strcat(temp_buff, cel_buff);
  125.   dtostrf(humi, 3, 0, hum_buff);
  126.   strcat(hum_buff, per_buff);
  127.   dtostrf(press, 5, 0, pres_buff);
  128.   strcat(pres_buff, pa_buff);
  129.   dtostrf(alti, 5, 2, alt_buff);
  130.   strcat(alt_buff, met_buff);
  131.   dtostrf(concentration, 3, 1, dust_buff);
  132.   strcat(dust_buff, pcs_buff);
  133.  
  134.   if(touchState == HIGH){
  135.     if(pageNumber < 2){
  136.       u8x8.clearDisplay();
  137.       pageNumber=pageNumber + 1;
  138.     }
  139.     else {
  140.       pageNumber = 0;
  141.     }
  142.   }
  143.  
  144.   if(pageNumber == 0){
  145.   u8x8.setFont(u8x8_font_artossans8_r);
  146.   u8x8.setCursor(0,2);
  147.   u8x8.print("Temp: ");
  148.   u8x8.setCursor(6,2);
  149.   u8x8.print(temp_buff);
  150.   u8x8.setCursor(0,4);
  151.   u8x8.print("Humi: ");
  152.   u8x8.setCursor(6,4);
  153.   u8x8.print(hum_buff);
  154.   u8x8.setCursor(0,6);
  155.   u8x8.print("Pres: ");
  156.   u8x8.setCursor(6,6);
  157.   u8x8.print(pres_buff);
  158.   }
  159.   else if(pageNumber == 1){
  160.   u8x8.setFont(u8x8_font_artossans8_r);
  161.   u8x8.setCursor(0,2);
  162.   u8x8.print("Alti: ");
  163.   u8x8.setCursor(6,2);
  164.   u8x8.print(alt_buff);
  165.   u8x8.setCursor(0,4);
  166.   u8x8.print("Dust: ");
  167.   u8x8.setCursor(6,4);
  168.   u8x8.print(dust_buff);
  169.   }
  170.   else if(pageNumber == 2){
  171.   u8x8.setFont(u8x8_font_artossans8_r);
  172.   u8x8.setCursor(0,2);
  173.   u8x8.print("Page 3");
  174.   }
  175. }
  176.  
  177. bool getNTPtime(int sec)
  178. {
  179.   {
  180.     uint32_t start = millis();
  181.     do
  182.     {
  183.       time(&now);
  184.       localtime_r(&now, &timeinfo);
  185.       delay(10);
  186.     } while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
  187.    
  188.     if (timeinfo.tm_year <= (2016 - 1900))
  189.         return false;  // the NTP call was not successful
  190.   }
  191.   return true;
  192. }
  193.  
  194. void showTime(tm *localTime)
  195. {
  196.   char time_output[30];
  197.  
  198.   u8x8.setFont(u8x8_font_artossans8_n);
  199.   u8x8.setCursor(0,0);
  200.   sprintf(time_output, "%02d:%02d", localTime->tm_hour, localTime->tm_min);
  201.   u8x8.print(time_output);
  202.  
  203.   u8x8.setFont(u8x8_font_artossans8_r);
  204.   u8x8.setCursor(7,0);
  205.   sprintf(time_output, "%02d/%02d/%02d", localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year - 100);
  206.   u8x8.print(time_output);
  207. }
Add Comment
Please, Sign In to add comment