Created
September 5, 2018 10:48
-
-
Save ricardoccpaiva/c499ff210d9e9e8bda0f80d4a2be457e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#include <ArduinoJson.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#include "floatToString.h" | |
Adafruit_BME280 bme; | |
ESP8266WiFiMulti WiFiMulti; | |
HTTPClient http; | |
#define USE_SERIAL Serial | |
#define OPENWEATHER_API_KEY String("ZZZZ") | |
float pressure; | |
float temperature; | |
float humidity; | |
int altimeter; | |
int sea_level_pressure_hp; | |
const size_t bufferSize = JSON_ARRAY_SIZE(3) + 2*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(12) + 480; | |
DynamicJsonBuffer jsonBuffer(bufferSize); | |
int parseJson(String json){ | |
JsonObject& root = jsonBuffer.parseObject(json); | |
JsonObject& main = root["main"]; | |
sea_level_pressure_hp = main["pressure"]; // 1032 | |
} | |
void setup_wifi(){ | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
for(uint8_t t = 4; t > 0; t--) { | |
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); | |
USE_SERIAL.flush(); | |
delay(1000); | |
} | |
WiFi.mode(WIFI_STA); | |
WiFiMulti.addAP("zzz", "zzz"); | |
// allow reuse (if server supports it) | |
http.setReuse(true); | |
} | |
void setup() { | |
bme.begin(); | |
Serial.begin(9600); | |
Serial.println("Adafruit bme280 test:"); | |
setup_wifi(); | |
} | |
void get_atmospheric_presure(){ | |
// wait for WiFi connection | |
if((WiFiMulti.run() == WL_CONNECTED)) { | |
http.begin("http://samples.openweathermap.org/data/2.5/weather?q=Lisbon&appid=" + OPENWEATHER_API_KEY); | |
//http.begin("192.168.1.12", 80, "/test.html"); | |
int httpCode = http.GET(); | |
if(httpCode > 0) { | |
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); | |
// file found at server | |
if(httpCode == HTTP_CODE_OK) { | |
parseJson(http.getString()); | |
} | |
} else { | |
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} | |
delay(1000); | |
} | |
void saveToInflux(float temp, float humidity, float pressure){ | |
if((WiFiMulti.run() == WL_CONNECTED)) { | |
char buffer[25]; | |
HTTPClient http; | |
http.begin("http://host:8086/write?db=home_sensors"); | |
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); | |
String message = "home_sensors,SOURCE=node3 temp="+String(temp)+",humidity="+String(humidity)+",pressure="+String(pressure); | |
int httpCode = http.POST(message); | |
if(httpCode > 0) { | |
Serial.printf("[HTTP] POST... code: %d\n", httpCode); | |
if(httpCode == HTTP_CODE_OK) { | |
String payload = http.getString(); | |
Serial.println(payload); | |
} | |
} else { | |
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} | |
} | |
void loop() { | |
//Read values from the sensor: | |
pressure = bme.readPressure(); | |
temperature = bme.readTemperature(); | |
humidity = bme.readHumidity(); | |
altimeter = bme.readAltitude (1050.35); //Change the "1050.35" to your city current barrometric pressure (https://www.wunderground.com) | |
Serial.print("Temperature = "); | |
Serial.print(bme.readTemperature()); | |
Serial.print("°"); | |
Serial.println("C"); | |
Serial.print("Pressure = "); | |
Serial.print(bme.readPressure() / 100.0F); | |
Serial.println(" hPa"); | |
Serial.print("Approx. Altitude = "); | |
Serial.print(bme.readAltitude(sea_level_pressure_hp)); | |
Serial.println(" m"); | |
Serial.print("Humidity = "); | |
Serial.print(bme.readHumidity()); | |
Serial.println(" %"); | |
Serial.println(); | |
saveToInflux(temperature, humidity, pressure); | |
delay(60000); //Update every 5 sec | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment