// Constants
#define DELAY 1000
// public include
#include <WiFi.h>
#include <PubSubClient.h>
#include <WebServer.h>
#include <HTTPClient.h>
WiFiClient espClient;
PubSubClient mqtt(espClient);
// private include
#include "common.h"
#include "commonWiFi.h"
#include "commonUbidots.h"
#include "commonThingSpeak.h"
// variables
int cont = 0;
float temperatureAVG = 0.0;
float humidityAVG = 0.0;
void setup() {
beginSerial();
connectWiFi();
connectMQTT();
}
void loop() {
delay(DELAY);
cont++;
if (!mqtt.connected())
connectMQTT();
mqtt.loop();
if(parada) {
Serial.println("Stoped system");
} else {
//Serial.print("temperature1: #"); Serial.println(temperature1);
//Serial.print("temperature2: #"); Serial.println(temperature2);
//Serial.print("humidity1: #"); Serial.println(humidity1);
//Serial.print("humidity2: #"); Serial.println(humidity2);
temperatureAVG = (temperature1 + temperature2) / 2.0;
humidityAVG = (humidity1 + humidity2) / 2.0;
temperatureAVG += random(-10, 10);
humidityAVG += random(-20, 20);
//Serial.print("temperatureAVG: #"); Serial.println(temperatureAVG);
//Serial.print("humidityAVG: #"); Serial.println(humidityAVG);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(REST_THINGSPEAK) + "?api_key=" + apiKey +
"&field1=" + String(temperatureAVG) +
"&field2=" + String(humidityAVG);
Serial.println(url);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP code response: ");
Serial.println(httpResponseCode);
Serial.print("HTTP response: ");
Serial.println(response);
Serial.println();
} else {
Serial.print("HTTP code error: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi not conected");
}
}
}