//Lauren Gabrielle Fogaça 202210184
//Henrique Scudeller de Oliveira 202210324
//Gabriel Martinelli Galvão Andreello Simoni 202210183
//Ricardo Altever Alves Lessa 202210295
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
// Configurações WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Configurações ThingSpeak
const char* server = "api.thingspeak.com";
String readApiKey = "A2N0T6LI9YJIWZNR"; // Substitua pelo seu Read API Key
String channelID = "2995170"; // Substitua pelo seu Channel ID
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variáveis para controle de tempo
unsigned long lastTime = 0;
unsigned long timerDelay = 30000; // 30 segundos
// Struct para armazenar dados dos sensores
struct SensorData {
float tempC;
float tempF;
float distCm;
float distInch;
bool dataValid;
};
SensorData sensorData;
void setup() {
lcd.init();
lcd.backlight();
// Conexão WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
// Inicializar estrutura de dados
sensorData.dataValid = false;
delay(1000);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Escola de Eng. ");
lcd.setCursor(0, 1);
lcd.print(" de Piracicaba ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Sistemas ");
lcd.setCursor(0, 1);
lcd.print(" Embarcados ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Projeto 03 ");
lcd.setCursor(0, 1);
lcd.print("----------------");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("# Consumidor #");
lcd.setCursor(0, 1);
lcd.print("# Subscriber #");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lauren Gabrielle");
lcd.setCursor(0, 1);
lcd.print(" Fogaca ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Henrique S. ");
lcd.setCursor(0, 1);
lcd.print(" de Oliveira ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ricardo Altever");
lcd.setCursor(0, 1);
lcd.print(" Alves Lessa ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Gabriel M. G. ");
lcd.setCursor(0, 1);
lcd.print(" Andreello ");
delay(1000);
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Buscar dados do ThingSpeak
if (WiFi.status() == WL_CONNECTED) {
fetchThingSpeakData();
}
lastTime = millis();
}
// Exibir dados no LCD
if (sensorData.dataValid) {
displayData();
} else {
delay(1000);
}
}
void fetchThingSpeakData() {
HTTPClient http;
String url = "http://api.thingspeak.com/channels/" + channelID + "/feeds/last.json?api_key=" + readApiKey;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
// Extrair dados
if (doc.containsKey("field1")) {
sensorData.tempC = doc["field1"].as<float>();
sensorData.tempF = doc["field2"].as<float>();
sensorData.distCm = doc["field3"].as<float>();
sensorData.distInch = doc["field4"].as<float>();
sensorData.dataValid = true;
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Erro conexao!");
lcd.setCursor(0, 1);
lcd.print("Codigo: ");
lcd.print(httpResponseCode);
delay(1000);
}
http.end();
}
void displayData() {
static int displayMode = 0;
static unsigned long displayTimer = 0;
if (millis() - displayTimer > 3000) {
displayMode++;
if (displayMode > 3) displayMode = 0;
displayTimer = millis();
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Recuperar dados");
lcd.setCursor(0, 1);
lcd.print(" do Bronken ");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperatura: ");
lcd.setCursor(0, 1);
lcd.print(sensorData.tempC, 1);
lcd.print(" C");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperatura: ");
lcd.setCursor(0, 1);
lcd.print(sensorData.tempF, 1);
lcd.print("F");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distancia: ");
lcd.setCursor(0, 1);
lcd.print(sensorData.distCm, 1);
lcd.print("cm");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distancia: ");
lcd.setCursor(0, 1);
lcd.print(sensorData.distInch, 1);
lcd.print("pol");
delay(30000);
}