Electorials

ESP8266 Weather Station v1

Jul 26th, 2021 (edited)
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. /*
  2.  * ESP8266 Weather Station v1
  3.  * Karl Ng - 26/07/2021
  4.  *
  5.  * Youtube Video: https://youtu.be/vjC9QMoJuIU
  6.  * Website: https://www.electorials.com
  7.  * Contact Email: info.electorials@gmail.com
  8.  */
  9.  
  10. #include <ESP8266WiFi.h>
  11. #include <time.h>
  12. #include <U8x8lib.h>
  13. #include "DFRobot_BME280.h"
  14. #include "Wire.h"
  15.  
  16. const char* ssid = "Wifi";              
  17. const char* password = "Password";      
  18.  
  19. const char* NTP_SERVER = "ch.pool.ntp.org";
  20. 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)
  21.  
  22. tm timeinfo;
  23. time_t now;
  24. long unsigned lastNTPtime;
  25. unsigned long lastEntryTime;
  26.  
  27. U8X8_SH1106_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // OLEDs without Reset of the Display
  28.  
  29. typedef DFRobot_BME280_IIC    BME;    //
  30. BME   bme(&Wire, 0x77);   // select TwoWire peripheral and set sensor address
  31. #define SEA_LEVEL_PRESSURE    1015.0f
  32.  
  33. void printLastOperateStatus(BME::eStatus_t eStatus) // show last sensor operate status
  34. {
  35.   switch(eStatus) {
  36.   case BME::eStatusOK:    Serial.println("everything ok"); break;
  37.   case BME::eStatusErr:   Serial.println("unknow error"); break;
  38.   case BME::eStatusErrDeviceNotDetected:    Serial.println("device not detected"); break;
  39.   case BME::eStatusErrParameter:    Serial.println("parameter error"); break;
  40.   default: Serial.println("unknow status"); break;
  41.   }
  42. }
  43.  
  44. void setup()
  45. {
  46.   u8x8.begin();
  47.   Serial.begin(115200);
  48.   WiFi.begin(ssid, password);
  49.  
  50.   int counter = 0;
  51.   while (WiFi.status() != WL_CONNECTED)
  52.   {
  53.     delay(200);    
  54.     if (++counter > 100)
  55.       ESP.restart();
  56.   }
  57.  
  58.   configTime(0, 0, NTP_SERVER);
  59.   // See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
  60.   setenv("GMT0BST,M3.5.0/1,M10.5.0", TZ_INFO, 1);
  61.  
  62.   if (getNTPtime(10))
  63.   {  
  64.     // wait up to 10sec to sync
  65.   }
  66.   else
  67.   {
  68.     ESP.restart();
  69.   }
  70.   showTime(&timeinfo);
  71.   lastNTPtime = time(&now);
  72.   lastEntryTime = millis();
  73.    
  74.   bme.reset();
  75.   Serial.println("bme read data test");
  76.   while(bme.begin() != BME::eStatusOK) {
  77.     Serial.println("bme begin faild");
  78.     printLastOperateStatus(bme.lastOperateStatus);
  79.     delay(2000);
  80.   }
  81.   delay(100);
  82. }
  83.  
  84. void loop()
  85. {
  86.   getNTPtime(10);
  87.   showTime(&timeinfo);
  88.   delay(1000);
  89.  
  90.   float   temp = bme.getTemperature();
  91.   uint32_t    press = bme.getPressure();
  92.   float   alti = bme.calAltitude(SEA_LEVEL_PRESSURE, press);
  93.   float   humi = bme.getHumidity();
  94.  
  95.   char temp_buff[5]; char hum_buff[5]; char pres_buff[9];
  96.   char cel_buff[11] = "C";
  97.   char per_buff[11] = "%";
  98.   char pa_buff[11] = " Pa";
  99.  
  100.   dtostrf(temp, 3, 1, temp_buff);
  101.   strcat(temp_buff, cel_buff);
  102.   dtostrf(humi, 3, 0, hum_buff);
  103.   strcat(hum_buff, per_buff);
  104.   dtostrf(press, 5, 0, pres_buff);
  105.   strcat(pres_buff, pa_buff);
  106.  
  107.   u8x8.setFont(u8x8_font_artossans8_r);
  108.   u8x8.setCursor(0,2);
  109.   u8x8.print("Temp: ");
  110.   u8x8.setCursor(6,2);
  111.   u8x8.print(temp_buff);
  112.   u8x8.setCursor(0,4);
  113.   u8x8.print("Hum: ");
  114.   u8x8.setCursor(6,4);
  115.   u8x8.print(hum_buff);
  116.   u8x8.setCursor(0,6);
  117.   u8x8.print("Pres: ");
  118.   u8x8.setCursor(6,6);
  119.   u8x8.print(pres_buff);
  120. }
  121.  
  122. bool getNTPtime(int sec)
  123. {
  124.   {
  125.     uint32_t start = millis();
  126.     do
  127.     {
  128.       time(&now);
  129.       localtime_r(&now, &timeinfo);
  130.       delay(10);
  131.     } while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
  132.    
  133.     if (timeinfo.tm_year <= (2016 - 1900))
  134.         return false;  // the NTP call was not successful
  135.   }
  136.   return true;
  137. }
  138.  
  139. void showTime(tm *localTime)
  140. {
  141.   char time_output[30];
  142.  
  143.   u8x8.setFont(u8x8_font_artossans8_n);
  144.   u8x8.setCursor(0,0);
  145.   sprintf(time_output, "%02d:%02d", localTime->tm_hour, localTime->tm_min);
  146.   u8x8.print(time_output);
  147.  
  148.   u8x8.setFont(u8x8_font_artossans8_r);
  149.   u8x8.setCursor(7,0);
  150.   sprintf(time_output, "%02d/%02d/%02d", localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year - 100);
  151.   u8x8.print(time_output);
  152. }
Add Comment
Please, Sign In to add comment