-
Notifications
You must be signed in to change notification settings - Fork 2
/
LeerPrecioLuz_ESIOS_API.ino
227 lines (196 loc) · 6.95 KB
/
LeerPrecioLuz_ESIOS_API.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Placa WeMos D1
// Tiny RTC DS1307 I2C
// LCD Crystal PCF8574A
// Consulta GET ejemplo: indicators/1013?start_date=09-06-2019T00%3A00&end_date=09-06-2019T23%3A00
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "config.h" // donde estan los datos sensibles
// WiFi Parameters
const char* ssid = SSID_NAME;
const char* password = WIFI_PASSWORD;
HTTPClient httpClient; //Object of class HTTPClient
//API
const String SERVER_ESIOS = SERVER_API;
const String TOKEN = TOKEN_API;
const char* FINGERPRINT=FINGERPRINT_API;
const String TYPE = TYPE_DEFAULT;
const String PEAJE = PEAJE_DEFAULT;
//LCD
const int NUM_CHAR = 20;
const int NUM_LINES = 4;
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
//TINY RTC
RTC_DS3231 rtc; //los puertos son los mismos que la version anterior, solo cambia la precisión del hardware.
String strToday,strTime;
int analogBAT = A0; //leer estado de la bateria ADC
//Comprobacion ERRORES
boolean hasError;
String msgError;
//OTROS FLAGS
boolean _DEBUGGER;
unsigned int statusProgram;
unsigned long previousMillis; // millis() returns an unsigned long.
void setup() {
initValues();
Serial.begin(115200);
Wire.begin(); // Inicia el puerto I2C
rtc.begin();
DateTime today = rtc.now();
if (!rtc.begin()) {
if(_DEBUGGER == true) {Serial.println(F("No encuentro RTC"));}
hasError = true;
msgError="No encuentro RTC";
yield();
while (1);
}
if (rtc.lostPower()) {
initDate();
msgError="Perdida potencia...";
if(_DEBUGGER == true) {
Serial.println("Perdida potencia...");
Serial.println("Se ha adjustado la fecha");
}
delay(1000);
}
if( String(today.year(),DEC) != "2019"){
initDate();
msgError="Valor de fabrica..."+String(today.year(),DEC) ;
if(_DEBUGGER == true) {
Serial.println("Tiene un valor de fabrica y se ha adjustado");
}
delay(1000);
}
pinMode(analogBAT, OUTPUT);
lcd.begin(NUM_CHAR,NUM_LINES); // initialize the lcd for 20 chars 4 lines and turn on backlight
lcd.backlight(); // backlight on
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
yield();
Serial.println("Connecting...");
delay(5000);
}
}
void loop() {
Serial.println("Error: "+ msgError);
delay(5000);
/***
* Comprobamos el valor de BAT del RTC
*/
unsigned long currentMillis = millis(); // grab current time
if ((unsigned long)(currentMillis - previousMillis) >= CHECK_BAT) {
previousMillis = millis();
int valBAT = analogRead(analogBAT); // read the input pin
Serial.println("BAT: " + valBAT);
}
/***
* En cada vuelta asignamos el DateTime
*/
DateTime today = rtc.now(); // Obtiene la fecha y hora del RTC
strToday = String(today.day(),DEC) + "-"+String(today.month(),DEC) + "-"+String(today.year(),DEC);
strTime = String(today.hour(),DEC) + ":"+String(today.minute(),DEC) + ":"+String(today.second(),DEC);
delay(1000);
Serial.println("ESTADO OUTSIDE: "+statusProgram);
delay(1000);
if ( WiFi.status() == WL_CONNECTED && statusProgram == 0) {
statusProgram == 1;
delay(1000);
Serial.println("ESTADO INSIDE: "+statusProgram);
delay(1000);
//TODO: Activar un led para saber el estado del wifi.
///////////
String url = getURLIndicatorWithDateTime(strToday);
if(_DEBUGGER == true) {Serial.println(url);}
httpClient.begin(url,FINGERPRINT);
httpClient.addHeader("Host", "api.esios.ree.es");
httpClient.addHeader("Authorization", "Token token="+TOKEN);
httpClient.addHeader("Accept", "application/json; application/vnd.esios-api-v1+json");
httpClient.addHeader("Content-Type", "application/json");
//httpClient.addHeader("Cookie", "");
int httpCodeResponse = httpClient.GET();
if(_DEBUGGER == true) {Serial.println("HTTPCODE: " + httpCodeResponse); }
if(!httpCodeResponse>0){
hasError = true;
msgError = httpClient.errorToString(httpCodeResponse);
if(_DEBUGGER == true){Serial.println("Error code response: "+ msgError);}
}
//Check the returning code
if (httpCodeResponse >0) {
// Get the request response payload
String payload = httpClient.getString();
//if(_DEBUGGER == true){Serial.println("PAYLOAD: "+ payload);}
printPayLoadValues(payload);
}
httpClient.end(); //Close connection
if(_DEBUGGER == true){Serial.println("fin conexión");}
}
else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
void initValues(){
strToday,strTime= "";
analogBAT = A0; //leer estado de la bateria ADC
//Comprobacion ERRORES
hasError = false;
msgError="No Error";
//OTROS FLAGS
_DEBUGGER = true;
statusProgram= 0;
previousMillis=0; // millis() returns an unsigned long.
}
void printPayLoadValues(String payload){
Serial.println("printPayLoadValues");
// Inside the brackets is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/v6/assistant to compute the capacity.
DynamicJsonDocument doc(13000) ;
// Deserialize the JSON document
yield();
DeserializationError error = deserializeJson(doc, payload);
// Test if parsing succeeds.
if (error) {
hasError=true;
msgError=error.c_str();
if(_DEBUGGER == true) {
Serial.print("deserializeJson() failed: ");
}
return;
}
const char* des = doc["indicator"]["short_name"];
Serial.println(des);
JsonArray values = doc["indicator"]["values"];
for (JsonObject val : values) {
Serial.print(" Fecha ");
Serial.print(val["datetime"].as<char *>());
Serial.print(", valor: ");
Serial.print(val["value"].as<long>());
Serial.println(" ");
}
}
String getURLIndicatorWithDateTime(String hoy){
//indicators/1013?start_date=09-06-2019T00%3A00&end_date=09-06-2019T23%3A00
String url = SERVER_ESIOS + TYPE + '/' + PEAJE + "?start_date=" + hoy + "T00%3A00";
return url;
}
void initDate(){
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));// Establece la fecha y hora
}
void printDate(DateTime date)
{
Serial.print(date.day(), DEC);
Serial.print('/');
Serial.print(date.month(), DEC);
Serial.print('/');
Serial.print(date.year(), DEC);
Serial.print("-----");
Serial.print(date.hour(), DEC);
Serial.print(':');
Serial.print(date.minute(), DEC);
Serial.print(':');
Serial.print(date.second(), DEC);
Serial.println();
}