Advertisement
kaihein

Weather Station

Apr 13th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. // Reports sensor readings every ~30 seconds.
  2.  
  3. #include <ESP8266WiFi.h> // Include the Wi-Fi library
  4. #include <PubSubClient.h> // Include the MQTT library
  5. #include <Wire.h>
  6. #include <SPI.h>
  7. #include <Adafruit_Sensor.h>
  8. #include <Adafruit_BME280.h>
  9.  
  10. // Defines pins numbers
  11. const int Base = 4; // D2 - Blue
  12. const int BME_Clock = 14; // D5 Direct
  13. const int BME_MOSI = 12; //D6 Direct
  14. const int BME_CS = 13; //D7 Direct
  15. const int BME_MISO = 15; // D8 Direct
  16.  
  17. // Settings for libraries
  18. WiFiClient espClient;
  19. PubSubClient client(espClient);
  20. Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_Clock); // software SPI
  21.  
  22. // All constants in the network settings area blanked out for privacy purposes.
  23. const char* WiFiSSID = ""; // SSID of network to connect to
  24. const char* WiFiPassword = ""; // Password for network
  25. const char* MQTTServer = ""; // Server running MQTT
  26. const char* MQTTUser = ""; // User for MQTT
  27. const char* MQTTPassword = ""; // Password for MQTT
  28.  
  29. // Defines global variables. Most only need to be global to assign a value only once.
  30. unsigned long lastUpdate = 0;
  31.  
  32. void setup_wifi();
  33.  
  34. void reconnect();
  35.  
  36. void setup() {
  37. // Initialize serial communication with computer:
  38. Serial.begin(115200); // Starts the serial communication
  39.  
  40. // Set pin modes
  41. pinMode(Base, OUTPUT);
  42.  
  43. // Connect to the WiFi
  44. setup_wifi();
  45.  
  46. // Settings for MQTT server
  47. client.setServer(MQTTServer, 1883);
  48.  
  49. // Settings for BME280
  50. bool status;
  51. status = bme.begin();
  52. if (!status) {
  53. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  54. while (1);
  55. }
  56.  
  57. lastUpdate = millis();
  58. }
  59.  
  60. void loop() {
  61. // Defines to current time to check against whether status reports should be done this loop
  62. unsigned long now = millis();
  63.  
  64. // Check if connected
  65. if (!client.connected()) {
  66. reconnect();
  67. }
  68. client.loop();
  69.  
  70. // Update statuses if it has been at least 30 seconds since last update
  71. if (now - lastUpdate > 30000) {
  72. // BME280 read data
  73. float OutsideTemp = (bme.readTemperature() * 9 / 5 + 32);
  74. float OutsideHumidity = bme.readHumidity();
  75. float OutsidePressure = (bme.readPressure() / 100.0F);
  76. float AdjOutsidePressure = (OutsidePressure * pow(1 - 0.0065 * 345 / (bme.readTemperature() + 0.0065 * 345 + 273.15), -5.257));
  77. char OutsideTempMQTT[6]; // Buffer for the Temperature output of the BME280 under the deck
  78. char OutsideHumidityMQTT[6]; // Buffer for the Humidity output of the BME280 under the deck
  79. char OutsidePressureMQTT[7]; // Buffer for the Pressure output of the BME280 under the deck
  80. char AdjOutsidePressureMQTT[7]; // Buffer for the Altitude Adjusted Pressure output of the BME280 under the deck
  81.  
  82. // BME280 display data
  83. Serial.println("Temperature (F)\tHumidity (%)\tPressure (mbar)\tAdj Pressure (mbar)");
  84. Serial.print(OutsideTemp);
  85. Serial.print(",\t\t");
  86. Serial.print(OutsideHumidity);
  87. Serial.print(",\t\t");
  88. Serial.print(OutsidePressure);
  89. Serial.print(",\t\t");
  90. Serial.println(AdjOutsidePressure);
  91.  
  92. // Only report Temperature and Humidity over MQTT if they are within the realm of possibility
  93. if ((OutsideTemp >= -50) && (OutsideTemp <= 150)) {
  94. dtostrf(OutsideTemp, 5, 1, OutsideTempMQTT);
  95. client.publish("home/underdeck/Temperature", OutsideTempMQTT);
  96. }
  97. if ((OutsideHumidity >= 0) && (OutsideHumidity <= 100)) {
  98. dtostrf(OutsideHumidity, 5, 1, OutsideHumidityMQTT);
  99. client.publish("home/underdeck/Humidity", OutsideHumidityMQTT);
  100. }
  101. if ((OutsidePressure >= 900) && (OutsidePressure <= 1100)) {
  102. dtostrf(OutsidePressure, 6, 1, OutsidePressureMQTT);
  103. dtostrf(AdjOutsidePressure, 6, 1, AdjOutsidePressureMQTT);
  104. client.publish("home/underdeck/Pressure", OutsidePressureMQTT);
  105. client.publish("home/underdeck/AdjPressure", AdjOutsidePressureMQTT);
  106. }
  107.  
  108. // Set update reference time to "now"
  109. lastUpdate = now ;
  110. digitalWrite(Base, HIGH);
  111. }
  112.  
  113. // Turn off fan 5 seconds before taking reading
  114. switch (now - lastUpdate){
  115. case 25000 ... 35000:
  116. if (Base != LOW){
  117. digitalWrite(Base, LOW);
  118. }
  119. break;
  120. default:
  121. if (Base != HIGH){
  122. digitalWrite(Base, HIGH);
  123. }
  124. break;
  125. }
  126. }
  127.  
  128. void setup_wifi() {
  129. delay(10);
  130. // We start by connecting to a WiFi network
  131. Serial.println();
  132. Serial.print("Connecting to ");
  133. Serial.println(WiFiSSID);
  134.  
  135. if (WiFi.SSID() != WiFiSSID) {
  136. WiFi.begin(WiFiSSID, WiFiPassword);
  137. WiFi.persistent(true);
  138. WiFi.setAutoConnect(true);
  139. WiFi.setAutoReconnect(true);
  140. }
  141.  
  142. while (WiFi.status() != WL_CONNECTED) {
  143. delay(500);
  144. Serial.print(".");
  145. }
  146.  
  147. Serial.println("");
  148. Serial.println("WiFi connected");
  149. Serial.println("IP address: ");
  150. Serial.println(WiFi.localIP());
  151. }
  152.  
  153. void reconnect() {
  154. // Loop until we're reconnected
  155. while (!client.connected()) {
  156. Serial.print("Attempting MQTT connection...");
  157. // Attempt to connect
  158. if (client.connect(MQTTUser, MQTTUser, MQTTPassword)) {
  159. Serial.println("connected");
  160. // Once connected, publish an announcement...
  161. client.publish("hello", "Horae Connected");
  162. } else {
  163. Serial.print("failed, rc=");
  164. Serial.print(client.state());
  165. Serial.println(" try again in 5 seconds");
  166. // Wait 5 seconds before retrying
  167. delay(5000);
  168. }
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement