#include <WiFi.h>
#include <ThingSpeak.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
const char* WIFI_NAME = "Wokwi-GUEST"; // Name of the WiFi network
const char* WIFI_PASSWORD = ""; // Password of the WiFi network
// ThingSpeak cloud
unsigned long myChannelNumber = 2662771;
const char * myWriteAPIKey = "Y611TJ3WUX5KMOV1";
const char* readAPIKey = "2KQ1HDCNBIU96N1W";
// Status code send / get from cloud
int statusCode;
WiFiClient client; // Create a WiFi client object
const int accept = 14;
const int deny = 27;
const int buzzer = 13;
// put your setup code here, to run once:
void setup()
{
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("DANG KHOI DONG");
pinMode(accept, INPUT);
pinMode(deny, INPUT);
pinMode(buzzer, OUTPUT);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
delay(1000);
lcd.clear();
}
void loop()
{
int state1 = digitalRead(accept);
int state2 = digitalRead(deny);
// Ket noi wifi
if (WiFi.status() != WL_CONNECTED)
{
lcd.setCursor(0, 0);
lcd.print("DANG KET NOI DEN");
lcd.setCursor(1, 6);
lcd.print("WIFI");
Serial.print("Attempting to connect");
WiFi.begin(WIFI_NAME, WIFI_PASSWORD); // Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Wifi not connected"); // Print a message if WiFi is not connected
}
Serial.println("Wifi connected !"); // Print a message if WiFi is connected
Serial.println("Local IP: " + String(WiFi.localIP())); // Print the local IP address
WiFi.mode(WIFI_STA); // Set the WiFi mode to station mode
ThingSpeak.begin(client); // Initialize the ThingSpeak library
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("KET NOI");
lcd.setCursor(1, 0);
lcd.print("THANH CONG");
Serial.println("\nConnected.");
delay(1000);
lcd.clear();
}
if (state1 == HIGH)
{
int myInt = 466087775;
int numberOfResults = 5;
// Prepare the URL to get the recent results from Field 2
String url = "https://api.thingspeak.com/channels/" + String(myChannelNumber) +
"/fields/2.json?api_key=" + readAPIKey + "&results=" + String(numberOfResults);
// Loop through the latest entries from Field 2
for (int i = 0; i < numberOfResults; i++) {
int field2Value = ThingSpeak.readIntField(myChannelNumber, 2, readAPIKey);
// Check if the read was successful
if (field2Value == -1) {
Serial.println("Failed to read data from ThingSpeak");
break;
} else {
Serial.print("Field 2 value (entry ");
Serial.print(i + 1);
Serial.print("): ");
Serial.println(field2Value);
// Compare the value from ThingSpeak with your stored integer
if (field2Value == myInt) {
Serial.println("Values match!");
ThingSpeak.setField(1, myInt);
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) {
Serial.println("Data pushed successfully");
lcd.setCursor(0, 0);
lcd.print("DUOC PHEP");
tone(buzzer, 330, 3);
delay(5000);
lcd.clear();
break;
} else {
Serial.println("Push error" + String(statusCode));
lcd.setCursor(0, 0);
lcd.print("KHONG DUOC PHEP");
tone(buzzer, 659, 3);
delay(5000);
lcd.clear();
}
} else {
Serial.println("Values do not match.");
lcd.setCursor(0, 0);
lcd.print("KHONG DUOC PHEP");
tone(buzzer, 659, 3);
delay(5000);
lcd.clear();
}
}
}
}
else if (state2 == HIGH)
{
int myInt = 56623776;
int numberOfResults = 5;
// Prepare the URL to get the recent results from Field 2
String url = "https://api.thingspeak.com/channels/" + String(myChannelNumber) +
"/fields/2.json?api_key=" + readAPIKey + "&results=" + String(numberOfResults);
// Loop through the latest entries from Field 2
for (int i = 0; i < numberOfResults; i++) {
int field2Value = ThingSpeak.readIntField(myChannelNumber, 2, readAPIKey);
// Check if the read was successful
if (field2Value == -1) {
Serial.println("Failed to read data from ThingSpeak");
break;
} else {
Serial.print("Field 2 value (entry ");
Serial.print(i + 1);
Serial.print("): ");
Serial.println(field2Value);
// Compare the value from ThingSpeak with your stored integer
if (field2Value == myInt) {
Serial.println("Values match!");
ThingSpeak.setField(1, myInt);
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (statusCode == 200) {
Serial.println("Data pushed successfully");
lcd.setCursor(0, 0);
lcd.print("DUOC PHEP");
tone(buzzer, 330, 3);
delay(5000);
lcd.clear();
break;
} else {
Serial.println("Push error" + String(statusCode));
lcd.setCursor(0, 0);
lcd.print("KHONG DUOC PHEP");
tone(buzzer, 659, 3);
delay(5000);
lcd.clear();
}
} else {
Serial.println("Values do not match.");
lcd.setCursor(0, 0);
lcd.print("KHONG DUOC PHEP");
tone(buzzer, 659, 3);
delay(5000);
lcd.clear();
break;
}
}
}
}
}