/* Lab Test
* Name: Lyana
* Class: DEM
* Date: 19 Jan 2024
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
int YLED=19;
int GLED = 18;
int RLED = 23;
int GledState = LOW; // ledState used to set the Green LED
int RledState = LOW; // ledState used to set the Red LED
int YledState = LOW; // ledState used to set the YellowLED
long GpreviousMillis = 0; // will store last time Green LED turn on
long RpreviousMillis = 0; // will store last time Red LED turn on
long YpreviousMillis = 0; // will store last time Yellow LED turn on
int Ginterval = 1000; // interval for green ld; this change according to qn
int Rinterval = 2000; // interval for red led; this change according to qn
int Yinterval = 2000; // interval for yellow led; this change according to qn
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32_YOURNAME"; // *** Remember to Change the name to yoru name
int PORTNUM = 1883;
void setup()
{
Serial.begin(9600);
delay(5000);
pinMode(RLED, OUTPUT);
digitalWrite(RLED , HIGH);// off the Red Led
pinMode(GLED, OUTPUT);
digitalWrite(GLED , HIGH); // off the Green LED
pinMode(YLED, OUTPUT);
digitalWrite(YLED , HIGH); // off the Green LED
}
void loop()
{
long currentMillis = millis();
if ( currentMillis - GpreviousMillis >= Ginterval) // this is for the RED LED *8 Change according to Qn
{
GpreviousMillis = currentMillis;
if (GledState == LOW)
{
GledState = HIGH;
digitalWrite(GLED,GledState);
}
else
{
GledState = LOW;
digitalWrite(GLED,GledState);
}
}
if ( currentMillis - RpreviousMillis >= Rinterval) // this is for the RED LED *8 Change according to Qn
{
RpreviousMillis = currentMillis;
if (RledState == LOW)
{
RledState = HIGH;
digitalWrite(RLED,RledState);
}
else
{
RledState = LOW;
digitalWrite(RLED,RledState);
}
}
}