Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NBK_PlanB3 - 12-27-2017 Basic climate sensor sending
- // temp, humidity, pressure, altitude, dew point, and heat index
- // via MQTT
- //
- //Requires PubSubClient found here: https://github.com/knolleary/pubsubclient
- //
- //ESP8266 Simple MQTT climate sensor
- #include <PubSubClient.h>
- #include <WiFiManager.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include <Wire.h>
- #include <Adafruit_BME280.h>
- void callback(char* topic, byte* payload, unsigned int length);
- //EDIT THESE LINES TO MATCH YOUR SETUP
- #define MQTT_SERVER "x.x.x.x" //your MQTT IP Address
- #define SEALEVELPRESSURE_HPA (1013.25)
- const char* host = "esp8266-secondfloor-tempsensor";
- unsigned long tempTimer=10000;
- int tempDelay = 60000; //One minutesA
- char const* publishClimateData = "/house/sensors/climate/";
- Adafruit_BME280 bme; // I2C
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- WiFiClient wifiClient;
- PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
- void setup()
- {
- Wire.begin(D1, D2);
- Serial.begin(115200);
- delay(10);
- setupNetwork();
- if (!bme.begin()) {
- Serial.println("Could not find a valid BME280 sensor, check wiring!");
- while (1);
- }
- reconnect();
- //wait a bit before starting the main loop
- delay(2000);
- }
- void loop()
- {
- httpServer.handleClient();
- //maintain MQTT connection
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- if (millis()- tempTimer > tempDelay)
- {
- checkTemp();
- }
- delay(20);
- }
- void setupNetwork()
- {
- //Wifi Manager
- WiFiManager wifiManager;
- WiFi.hostname(host);
- //first parameter is name of access point, second is the password
- wifiManager.autoConnect(host);//, "password");
- httpUpdater.setup(&httpServer);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
- }
- void callback(char* topic, byte* payload, unsigned int length)
- {
- // Nothing here, publish only
- }
- void reconnect()
- {
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- if (client.connect(host)) {
- Serial.println("\tMQTT Connected");
- } else {
- Serial.print("Failed to connect to MQTT - ");
- Serial.print(client.state());
- Serial.println(" trying again in 5 seconds.");
- delay (5000);
- }
- }
- }
- void checkTemp()
- {
- tempTimer = millis();
- float tempC=bme.readTemperature();
- float tempF=((tempC*9/5)+32);
- float pressure=bme.readPressure();
- float kPa=pressure/100.0F;
- float barInches=pressure*0.000296133971008484; //Inch Mercury at 60F
- float altitude=bme.readAltitude(SEALEVELPRESSURE_HPA);
- float humidity=bme.readHumidity();
- double dewPoint = calcDewPointFast(tempC, humidity);
- double heatIndex = calcHeatIndex(tempF, humidity);
- String payloadStr = "{";
- payloadStr += "\"ClientID\":\""; payloadStr += host; payloadStr += "\",";
- payloadStr += "\"TempF\":"; payloadStr += tempF; payloadStr += ",";
- payloadStr += "\"TempC\":"; payloadStr += tempC; payloadStr += ",";
- payloadStr += "\"Pressure\":"; payloadStr += pressure; payloadStr += ",";
- payloadStr += "\"BarometricPressure\":"; payloadStr += barInches; payloadStr += ",";
- payloadStr += "\"Humidity\":"; payloadStr += humidity; payloadStr += ",";
- payloadStr += "\"DewPoint\":"; payloadStr += dewPoint; payloadStr += ",";
- payloadStr += "\"HeatIndex\":"; payloadStr += heatIndex;
- payloadStr += "}";
- char payload[200];
- payloadStr.toCharArray( payload, 200 );
- client.publish(publishClimateData,payload);
- Serial.println(payload);
- }
- // delta max = 0.6544 wrt dewPoint()
- // 6.9 x faster than dewPoint()
- // reference: http://en.wikipedia.org/wiki/Dew_point
- double calcDewPointFast(double celsius, double humidity)
- {
- double a = 17.271;
- double b = 237.7;
- double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
- double Td = (b * temp) / (a - temp);
- return Td;
- }
- double calcHeatIndex(double tempF, double humidity)
- {
- 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 ;
- double T = tempF;
- double R = humidity;
- double A = (( c5 * T) + c2) * T + c1;
- double B = ((c7 * T) + c4) * T + c3;
- double C = ((c9 * T) + c8) * T + c6;
- double rv = (C * R + B) * R + A;
- return rv;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement