Skip to content

Instantly share code, notes, and snippets.

@cretudorin
Last active August 18, 2019 12:02
Show Gist options
  • Save cretudorin/bd2bc6d2305b1e4c32a3cb975de8e7aa to your computer and use it in GitHub Desktop.
Save cretudorin/bd2bc6d2305b1e4c32a3cb975de8e7aa to your computer and use it in GitHub Desktop.
using a nodemcu (ESP8266) to send weather data via http using dht22 and bmp180
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define DHTPIN D5
#define DHTTYPE DHT22
#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "pass"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const String host = "http://<ip>:8123/api/states/sensor.";
const String bearer = "token";
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
void httpRequest(float temp, String sensorName, String _unit, String name){
HTTPClient http;
http.begin(host + sensorName);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Bearer " + bearer);
int httpCode = http.POST(
"{ \"state\":" + String(temp) + ", \"attributes\": { \"unit_of_measurement\": \"" + String(_unit) + "\", \"friendly_name\": \"" + String(name) +"\"}}"
);
// Get the response
String payload = http.getString();
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
// Close connection
http.end();
}
void setup() {
Serial.begin(9600);
// Wait for serial to initialize.
while(!Serial) { }
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
}
dht.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
float temp0 = bmp.readTemperature();
// convert to mbar
float pressure = bmp.readPressure() * 0.01;
float altitude = bmp.readAltitude();
float seaLeveLPresure = bmp.readSealevelPressure();
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
float altitudeCalculated = bmp.readAltitude(101500);
Serial.println("altitude: "+ String(altitude));
Serial.println("seaLeveLPresure: "+ String(seaLeveLPresure));
Serial.println("altitudeCalculated: "+ String(altitudeCalculated));
float humidity = dht.readHumidity();
float temp1 = dht.readTemperature();
// Compute heat index
float heatIndex = dht.computeHeatIndex(temp0, humidity, false);
if(WiFi.status()== WL_CONNECTED){
httpRequest(heatIndex, "ws_heat_index", "°C", "Weather station heat index");
delay(500);
httpRequest(temp0, "ws_temp0", "°C", "Weather station temp sensor 1");
delay(500);
httpRequest(heatIndex, "ws_heat_index", "°C", "Weather station heat index");
delay(500);
httpRequest(temp1, "ws_temp1", "°C", "Weather station temp sensor 2");
delay(500);
httpRequest(pressure, "ws_pressure", "mbar", "Weather station pressure sensor");
delay(500);
httpRequest(humidity, "ws_humidity", "%", "Weather station humidity sensor");
} else {
Serial.println("Error in WiFi connection");
}
float minute = 1000000 * 60;
Serial.println("Deep sleep mode for 15 minutes");
ESP.deepSleep(minute * 15);
delay(100);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment