Last active
May 29, 2020 02:38
-
-
Save daniel7byte/00f3c0ac6275dc42e0ecd479bcc350af 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
// Librerias EST, Firebase y JSON | |
#include <ESP8266WiFi.h> | |
#include <FirebaseArduino.h> | |
#include <ArduinoJson.h> | |
// Librerias Fabricante | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
// Constantes Token Firebase | |
#define FIREBASE_HOST "" | |
#define FIREBASE_AUTH "" | |
// Constantes WiFi | |
#define WIFI_SSID "" | |
#define WIFI_PASSWORD "" | |
// Nombre del dispositivo | |
const String device = "nodemcu-1"; | |
// Nombre del Sensor | |
const String sensorName = "ds18b20-x"; | |
// Puerto Sensor | |
#define SENSOR_PORT D2 | |
// Configura la libreria OneWire | |
OneWire oneWireObjeto(SENSOR_PORT); | |
DallasTemperature sensorDS18B20(&oneWireObjeto); | |
void setup() | |
{ | |
// Abre el puerto Serie | |
Serial.begin(9600); | |
// Conecta con el sensor | |
sensorDS18B20.begin(); | |
// Conecta a la red WiFi | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
Serial.print("Conectando"); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
Serial.print("."); | |
delay(250); | |
} | |
Serial.println(); | |
Serial.print("Conectado, mi IP es: "); | |
Serial.println(WiFi.localIP()); | |
// Se conecta y autentica con Firebase | |
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); | |
} | |
bool primeraMuestra = true; | |
float valorTempAnterior = 0; | |
float valorTempActual = 0; | |
void loop() | |
{ | |
// Tomamos la temperatura | |
sensorDS18B20.requestTemperatures(); | |
Serial.println(sensorDS18B20.getTempCByIndex(0)); | |
if(primeraMuestra) | |
{ | |
valorTempAnterior = sensorDS18B20.getTempCByIndex(0); | |
primeraMuestra = false; | |
} | |
// Promedia la medicion | |
valorTempActual = (valorTempAnterior + sensorDS18B20.getTempCByIndex(0))/2; | |
valorTempAnterior = valorTempActual; | |
// Inicializa la clase JSON | |
DynamicJsonBuffer jsonBuffer; | |
// Estructura la informacion | |
String input = "{\"timestamp\": {\".sv\": \"timestamp\"} ,\"value\":"+String(valorTempActual)+"}"; | |
JsonObject &data = jsonBuffer.parseObject(input); | |
// Agrega un nuevo valor para /device/sensorName | |
String name = Firebase.push(device + "/" + sensorName, data); | |
// Si se produce errores | |
if (Firebase.failed()) | |
{ | |
Serial.print("pushing " + device + "/" + sensorName + " failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
Serial.println("pushed: /" + device + "/" + sensorName); | |
// Actualiza valor unico en el sensor | |
Firebase.setFloat(device + "/" + sensorName + "/value", valorTempActual); | |
// Si se produce errores | |
if (Firebase.failed()) | |
{ | |
Serial.print("setting " + device + "/" + sensorName + "/value" + " failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment