Advertisement
NBK_PlanB

Untitled

Dec 31st, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. // NBK_PlanB3 - 12-27-2017 Basic climate sensor sending
  2. // temp, humidity, pressure, altitude, dew point, and heat index
  3. // via MQTT
  4.  
  5. //
  6. //Requires PubSubClient found here: https://github.com/knolleary/pubsubclient
  7. //
  8. //ESP8266 Simple MQTT climate sensor
  9.  
  10. #include <PubSubClient.h>
  11. #include <WiFiManager.h>
  12. #include <ESP8266WebServer.h>
  13. #include <ESP8266mDNS.h>
  14. #include <ESP8266HTTPUpdateServer.h>
  15. #include <Wire.h>
  16. #include <Adafruit_BME280.h>
  17.  
  18. void callback(char* topic, byte* payload, unsigned int length);
  19.  
  20. //EDIT THESE LINES TO MATCH YOUR SETUP
  21. #define MQTT_SERVER "x.x.x.x" //your MQTT IP Address
  22. #define SEALEVELPRESSURE_HPA (1013.25)
  23.  
  24. const char* host = "esp8266-secondfloor-tempsensor";
  25. unsigned long tempTimer=10000;
  26. int tempDelay = 60000; //One minutesA
  27. char const* publishClimateData = "/house/sensors/climate/";
  28.  
  29.  
  30.  
  31. Adafruit_BME280 bme; // I2C
  32. ESP8266WebServer httpServer(80);
  33. ESP8266HTTPUpdateServer httpUpdater;
  34. WiFiClient wifiClient;
  35. PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
  36.  
  37. void setup()
  38. {
  39.  
  40. Wire.begin(D1, D2);
  41. Serial.begin(115200);
  42. delay(10);
  43.  
  44. setupNetwork();
  45. if (!bme.begin()) {
  46. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  47. while (1);
  48. }
  49.  
  50. reconnect();
  51. //wait a bit before starting the main loop
  52. delay(2000);
  53. }
  54.  
  55. void loop()
  56. {
  57. httpServer.handleClient();
  58. //maintain MQTT connection
  59. if (!client.connected()) {
  60. reconnect();
  61. }
  62. client.loop();
  63. if (millis()- tempTimer > tempDelay)
  64. {
  65. checkTemp();
  66. }
  67. delay(20);
  68. }
  69.  
  70. void setupNetwork()
  71. {
  72. //Wifi Manager
  73. WiFiManager wifiManager;
  74. WiFi.hostname(host);
  75. //first parameter is name of access point, second is the password
  76. wifiManager.autoConnect(host);//, "password");
  77.  
  78. httpUpdater.setup(&httpServer);
  79. httpServer.begin();
  80.  
  81. MDNS.addService("http", "tcp", 80);
  82. Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
  83. }
  84.  
  85.  
  86.  
  87. void callback(char* topic, byte* payload, unsigned int length)
  88. {
  89. // Nothing here, publish only
  90. }
  91.  
  92. void reconnect()
  93. {
  94. while (!client.connected()) {
  95. Serial.print("Attempting MQTT connection...");
  96.  
  97. if (client.connect(host)) {
  98. Serial.println("\tMQTT Connected");
  99. } else {
  100. Serial.print("Failed to connect to MQTT - ");
  101. Serial.print(client.state());
  102. Serial.println(" trying again in 5 seconds.");
  103. delay (5000);
  104. }
  105. }
  106.  
  107. }
  108. void checkTemp()
  109. {
  110. tempTimer = millis();
  111.  
  112. float tempC=bme.readTemperature();
  113. float tempF=((tempC*9/5)+32);
  114. float pressure=bme.readPressure();
  115. float kPa=pressure/100.0F;
  116. float barInches=pressure*0.000296133971008484; //Inch Mercury at 60F
  117. float altitude=bme.readAltitude(SEALEVELPRESSURE_HPA);
  118. float humidity=bme.readHumidity();
  119. double dewPoint = calcDewPointFast(tempC, humidity);
  120. double heatIndex = calcHeatIndex(tempF, humidity);
  121.  
  122. String payloadStr = "{";
  123. payloadStr += "\"ClientID\":\""; payloadStr += host; payloadStr += "\",";
  124. payloadStr += "\"TempF\":"; payloadStr += tempF; payloadStr += ",";
  125. payloadStr += "\"TempC\":"; payloadStr += tempC; payloadStr += ",";
  126. payloadStr += "\"Pressure\":"; payloadStr += pressure; payloadStr += ",";
  127. payloadStr += "\"BarometricPressure\":"; payloadStr += barInches; payloadStr += ",";
  128. payloadStr += "\"Humidity\":"; payloadStr += humidity; payloadStr += ",";
  129. payloadStr += "\"DewPoint\":"; payloadStr += dewPoint; payloadStr += ",";
  130. payloadStr += "\"HeatIndex\":"; payloadStr += heatIndex;
  131. payloadStr += "}";
  132.  
  133. char payload[200];
  134. payloadStr.toCharArray( payload, 200 );
  135. client.publish(publishClimateData,payload);
  136. Serial.println(payload);
  137. }
  138.  
  139. // delta max = 0.6544 wrt dewPoint()
  140. // 6.9 x faster than dewPoint()
  141. // reference: http://en.wikipedia.org/wiki/Dew_point
  142. double calcDewPointFast(double celsius, double humidity)
  143. {
  144. double a = 17.271;
  145. double b = 237.7;
  146. double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
  147. double Td = (b * temp) / (a - temp);
  148. return Td;
  149. }
  150.  
  151. double calcHeatIndex(double tempF, double humidity)
  152. {
  153. double c1 = -42.38, c2 = 2.049, c3 = 10.14, c4 = -0.2248, c5= -6.838e-3, c6=-5.482e-2, c7=1.228e-3, c8=8.528e-4, c9=-1.99e-6 ;
  154. double T = tempF;
  155. double R = humidity;
  156.  
  157. double A = (( c5 * T) + c2) * T + c1;
  158. double B = ((c7 * T) + c4) * T + c3;
  159. double C = ((c9 * T) + c8) * T + c6;
  160.  
  161. double rv = (C * R + B) * R + A;
  162. return rv;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement