// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
//#include <thermalprinter.h>
//#include <SoftwareSerial.h>
//#include <EEPROM.h>
//#include <NTPClient.h>
#include <RESTClient.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
#define LED1 2
#define LED2 4
const int idcontrolador0=1001;
void(* resetSoftware)(void) = 0;
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");
}
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
//configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
configTime(-21600, 0 , "pool.ntp.org", "time.nis.gov");
}
void loop() {
printLocalTime();
blink();
http_request_get(idcontrolador0);
delay(250);
}
void blink() {
digitalWrite(LED2, HIGH);
digitalWrite(LED1, HIGH);
delay(250);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
delay(250);
}
//*************************************************************************
void http_request_get(int a)
{
// Connect to HTTP server
EthernetClient client;
client.setTimeout(10000);
//iotparking.net/json/index.php?id=1
if (!client.connect("iotparking.net", 80)) {
Serial.println(F("Connection failed"));
resetSoftware();
return;
}
Serial.println(F("Connected!"));
// Send HTTP request
// /controllers/ws/controladores?codigo_control=1003
/*
client.print("GET /sedas3/ws/putLogs/?");
client.print("idcontrolador="+ String(a));
client.print("&idverificacion=" + String(b));
client.print("&descripcion=" + String(c));
client.print("&dato=" + String(d));
client.print(" ");
client.println(F("HTTP/1.0"));
*/
client.print("GET /controllers/ws/controladores?");
client.print("codigo_control="+ String(a));
client.print(" ");
client.println(F("HTTP/1.0"));
client.println(F("Host: iotparking.net"));
client.println(F("Connection: close"));
if (client.println() == 0) {
Serial.println(F("Failed to send request"));
client.stop();
return;
}
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
Serial.print(F("Unexpected response: "));
Serial.println(status);
client.stop();
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("Invalid response"));
client.stop();
resetSoftware();
return;
}
// Allocate the JSON document
// Use arduinojson.org/v6/assistant to compute the capacity.
//const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
//DynamicJsonDocument doc(capacity);
StaticJsonDocument<384> doc;
// Parse JSON object
DeserializationError error = deserializeJson(doc, client);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
client.stop();
return;
}
// Extract values
const int controlador = doc[0]["codigo"]; // "#serie controlador"
const int condicion = doc[0]["condicion"]; // "1", "0"
const int estado = doc[0]["estado"]; // "1", "0"
const int barrera = doc[0]["barrera"]; // "1", "0"
const int vehiculo = doc[0]["vehiculo"]; // "1", "0"
Serial.println(F("Response:"));
Serial.println("controlador=" + String(controlador));
Serial.println("condicion=" + String(condicion));
Serial.println("estado=" + String(estado));
Serial.println("barrera=" + String(barrera));
Serial.println("vehiculo=" + String(vehiculo));
// Disconnect
client.stop();
}
//*************************************************************************