#define B1 18
#define B2 19
#define B3 21
#define B4P 22
#define LED1 12
#define LED2 13
#define LED3 14
#define LED4P 15
#include <WiFi.h>
#include "ThingSpeak.h"
#include <HTTPClient.h>
#include <WebServer.h>
int b1, b2, b3, b4p;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* host = "api.thingspeak.com";
const char* APIkeyWrite = "APU9NWDB1DT4NEQ2";
const String APIkeyRead = "JUQ3FU9FS1UQSY0Q";
const String UltimosRESULTADOS = "1";
const String IDchannel = "IDcanal";
String urlreceive = "http://api.thingspeak.com/channels/" + IDchannel + "/feeds.json?api_key=" + APIkeyRead + "&results=" + UltimosRESULTADOS;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Conectando-se ao WiFi");
//ThingSpeak.begin(client); // Initialize ThingSpeak
pinMode(B1, INPUT_PULLUP);
pinMode(B2, INPUT_PULLUP);
pinMode(B3, INPUT_PULLUP);
pinMode(B4P, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4P, OUTPUT);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("OK! IP=");
Serial.println(WiFi.localIP());
delay(1000);
}
void loop() {
int banco1 = 0;
int banco2 = 0;
int banco3 = 0;
int banco4p = 0;
if (WiFi.status() == WL_CONNECTED)
Serial.println("CONECTADO\n");
else
Serial.println("NAO conectou\n");
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort))
{
return;
}
HTTPClient http;
http.begin(urlreceive);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
{
String resposta = http.getString();
Serial.println(resposta);
}
b1 = digitalRead(B1);
if (b1 == 0) {
analogWrite(LED1, HIGH);
banco1++;
}
else
analogWrite(LED1, LOW);
b2 = digitalRead(B2);
if (b2 == 0) {
analogWrite(LED2, HIGH);
banco2++;
}
else
analogWrite(LED2, LOW);
b3 = digitalRead(B3);
if (b3 == 0) {
analogWrite(LED3, HIGH);
banco3++;
}
else
analogWrite(LED3, LOW);
b4p = digitalRead(B4P);
if (b4p == 0) {
analogWrite(LED4P, HIGH);
banco4p++;
}
else
analogWrite(LED4P, LOW);
Serial.println(b1);
Serial.println(b2);
Serial.println(b3);
Serial.println(b4p);
int bancos_padrao_livres = 3 - (banco1 + banco2 + banco3);
int bancos_pref_livres = 1 - banco4p;
String url = "/update?api_key=";
url += APIkeyWrite;
url += "&field1=";
url += String(banco1);
url += "&field2=";
url += String(banco2);
url += "&field3=";
url += String(banco3);
url += "&field4=";
url += String(banco4p);
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);
Serial.println("Bancos padrao livres: " + String(bancos_padrao_livres));
Serial.println("Bancos preferenciais livres: " + String(bancos_pref_livres));
String urlreceive = "/update?api_key=";
urlreceive += APIkeyWrite;
url += "&field1=";
url += String(banco1);
url += "&field2=";
url += String(banco2);
url += "&field3=";
url += String(banco3);
url += "&field4=";
url += String(banco4p);
urlreceive += "\r\n";
Serial.println(urlreceive);
String resposta = String("GET ") + urlreceive + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n";
http.begin(urlreceive); //endereça a urlreceive
if (httpCode == HTTP_CODE_OK) {
String resposta = http.getString(); //Obtem a resposta da nuvem
Serial.println(resposta); //Escreve a resposta no monitor
}
delay(10); // this speeds up the simulation
}