Skip to content

Instantly share code, notes, and snippets.

@Kaziuz
Created December 1, 2019 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kaziuz/ad3751c3b075d593cda2c111af3b242b to your computer and use it in GitHub Desktop.
Save Kaziuz/ad3751c3b075d593cda2c111af3b242b to your computer and use it in GitHub Desktop.
/* Librerias usadas
* https://github.com/PaulStoffregen/Time
* https://github.com/PaulStoffregen/TimeAlarms
* https://learn.adafruit.com/dht/using-a-dhtxx-sensor
*
* Recursos
* https://www.switchdoc.com/2018/11/tutorial-capacitive-moisture-sensor-grove/
*/
// import libraries
#include <DHT.h>
#include <DHT_U.h>
#include <Time.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
// defines and variables globals sensors
// dth11
#define DHTPIN 4 // D2
#define DHTTYPE DHT11
#define HTTP_TIMEOUT 4000
// screen oled
#define OLED_MOSI 14 // D5
#define OLED_CLK 16 // D0
#define OLED_DC 13 // D7
#define OLED_RESET 12 // D6
#define OLED_CS 15 // D8
// lights- relay IN4
int lights = 2;
// ventilador - relay IN3
int air = 0;
// capacitive sensor
const int waterValue = 464;
int soilMoistureValue = 0;
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
display.setTextSize(0.5);
display.setTextColor(WHITE);
display.setCursor(0,0);
Serial.println();
Serial.println();
//
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] BOOT WAIT %d...\r\n", t);
Serial.flush();
delay(1000);
}
//
display.setTextColor(WHITE);
display.setCursor(0,10);
// casa del negro
// WiFi.begin("user", "pass");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
display.println("UN/LOQUER - cultiva");
display.print(".");
}
display.display();
delay(3000);
// H, M, S, D, M, A
setTime(14,6,0,9,11,19);
pinMode(lights, OUTPUT);
pinMode(air, OUTPUT);
dht.begin();
}
void loop() {
display.clearDisplay();
display.setTextSize(0.5);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("time: ");
display.print(hour());
display.print(":");
display.print(minute());
display.print(":");
display.print(second());
display.print(" ");
display.setCursor(0,8);
// display alarms lights state
lucesFloracion();
// display capacitive himudity
display.setCursor(0,16);
soilMoistureValue = analogRead(A0);
display.print("Soil moisture: ");
display.println(soilMoistureValue);
delay(500);
// display dht sensor
display.setCursor(0,24);
int h = dht.readHumidity();
int t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
display.println(F("Failed read dht!"));
return;
}
display.print(F("Hum: "));
display.print(h);
display.print(F(" % Tem: "));
display.print(t);
display.print(F(" C "));
display.println("");
// run air at inddor
ventilacion();
display.display();
sendDataInflux(String(h), String(t), String(soilMoistureValue));
}
void lucesVegetativo() {
// 18 horas luz, 6 horas oscuridad
// se prenden a las 6 de la mañana y se apagan a las 12 de la noche
if ( hour() < 6 ) {
digitalWrite(lights, LOW);
display.println("Veget Light OFF!");
} else {
digitalWrite(lights, HIGH);
display.println("Veget Light ONN!");
}
}
void lucesFloracion() {
// 12 horas luz, 12 horas oscuridad
// a las 6 de la mañana se prenden y a las 6 de la tarde se apagan
if ( (hour() >= 6) && (hour() < 18) ) {
digitalWrite(lights, HIGH);
display.println("Flor Light ONN!");
} else {
digitalWrite(lights, LOW);
display.println("Flor Light OFF");
}
}
void ventilacion() {
// se prende cada 15 min 1 min
if ( (minute() == 1) || (minute() == 16) || (minute() == 46) || (minute() == 31) ) {
digitalWrite(air, HIGH);
} else {
digitalWrite(air, LOW);
}
}
String influxFrame( String dht11_humidity, String dht11_temperature, String soilCapacitiveSensor) {
const String SENSOR_ID = "DHT11_llanadas"; // Nombre del sensor en la plataforma, la 1 VEZ CAMBIAR !!!!
const String STR_COMMA = ",";
const String STR_SLASH = "/";
const String STR_DOT = ".";
const String STR_COLON = ":";
const String STR_NULL = "NULL";
const String STR_ZERO = "0";
const String STR_SPACE = " ";
// El primer dato en el squema de la DB es el id del sensor
String frame = SENSOR_ID + STR_COMMA + "id=" + SENSOR_ID + STR_SPACE;
// Add GPS data
frame += "lat=";
frame += "6.2563143" + STR_COMMA; // coordenada GSP lat
frame += "lng=";
frame += "-75.5386472" + STR_COMMA; // coordenada lng lat
frame += "altitude=";
frame += STR_ZERO + STR_COMMA;
frame += "course=";
frame += STR_ZERO + STR_COMMA;
frame += "speed=";
frame += STR_ZERO + STR_COMMA;
//Add DHT11 data
//if
frame += "humidity=";
frame += dht11_humidity + STR_COMMA;
frame += "temperature=";
frame += dht11_temperature + STR_COMMA;
// } else {
// frame += "humidity=" + STR_NULL + STR_COMMA + "temperature=" + STR_NULL + STR_COMMA;
// }
// ADD capacitive sensor data
frame += "soil_capacitive=";
frame += soilCapacitiveSensor + STR_COMMA;
// Add Plantower data
// if
frame += "pm1=";
frame += STR_ZERO + STR_COMMA;
frame += "pm25=";
frame += STR_ZERO + STR_COMMA;
frame += "pm10=";
frame += STR_ZERO;
// } else {
// frame += "pm1=" + STR_NULL + STR_COMMA + "pm25=" + STR_NULL + STR_COMMA + "pm10=" + STR_NULL;
// }
return frame;
}
// función que envía la trama de datos
void sendDataInflux ( String humidity, String temperature, String _soilCapacitiveSensor ) {
// El post a la base de datos tiene una trama siguiente:
// volker0001,id=volker0001 lat=6.268115,lng=-75.543407,altitude=1801.1,course=105.55,speed=0.00,humidity=37.00,temperature=25.00,pm1=22,pm25=31,pm10=32
// Para nuestro caso que SOLO es el envío de datos del dht_11 que es humedad y temperatura la trama es la siguiente
// DHT11_llanadas, id=DHT11_llanadas, lat=6.2563143, lng=-75.5386472, altitude=0, course=0, speed=0, humidity=37.00, temperature=25.00, pm1=0, pm25=0, pm10=0 1434055562000000000
if(WiFi.status() == 3) {
HTTPClient http;
// _testsensorhumedad es el nombre de la DB donde se almacenan estos datos
http.begin("http://aqa.unloquer.org:8086/write?db=_testsensorhumedad"); // endPoint final, '_testsensorhumedad' es el nombre de la base de datos
http.setTimeout(HTTP_TIMEOUT);
http.addHeader("Content-Type", "--data-binary");
// esto se debe de integrar con el soil capacitive sensor
// String frame = influxFrame(humidity, temperature, _soilCapacitiveSensor); // Construimos el request POST
String frame = influxFrame(humidity, temperature, _soilCapacitiveSensor);
int httpCode = http.POST(frame); // Envíamos los datos haciendo un POST
if(httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
Serial.println("Envío de datos con exito!");
} else {
Serial.print("[HTTP] failed, error;");
Serial.println(http.errorToString(httpCode).c_str());
}
http.end();
}
delay(60000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment