#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#define ch1 18
#define pot1 33
#define led 19
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* host = "api.thingspeak.com";
const char* APIkeyWrite = "QJEY59O6MSTSZMOQ"; //Colocar a KEY que você criou aqui!
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Exp. 8 - NUVEM");
pinMode(ch1, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(115200);
WiFi.begin(ssid, password); // rede e senha do WiFi
Serial.print("Conectando-se ao WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Conectou no WIFI");
delay(1000);
lcd.clear();
}
void loop()
{
bool botao1=digitalRead(ch1);
if (botao1==false)
{
lcd.setCursor(0,0);
lcd.print("Chave: ON");
}
else
{
lcd.setCursor(0,0);
lcd.print("Chave: OFF");
}
int pot = analogRead(pot1);
pot = map(pot, 0, 4095, 0, 100);
lcd.setCursor(0,1);
lcd.print("pot:");
lcd.setCursor(5,1);
lcd.print(pot);
if (WiFi.status() == WL_CONNECTED)
digitalWrite(led, HIGH);
else
digitalWrite(led, LOW);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort))
{
return;
}String url = "/update?api_key=";
url += APIkeyWrite;
url += "&field1=";
url += String(botao1);
url += "&field2=";
url += String(pot);
url += "\r\n";
Serial.println(url);
String endereco = String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" + "Connection: close\r\n\r\n";
Serial.println(endereco);
client.print(endereco);
delay(10000);
lcd.clear();
}