/*
Course Project
DeVry University
College of Engineering and Information Sciences
Course Number: CEIS114
Module 2: Getting Started with Wokwi and using a ESP32
Background
The Internet of Things (IoT) is growing exponentially. New technologies and applications are being developed on a regular basis and this creates an abundance of new job opportunities each with its requisite skill sets. With that the development of modern cities and urban areas has brought about an ever-increasing need for efficient traffic management systems. Ensuring the smooth flow of vehicles and pedestrian traffic at intersections is crucial not only for reducing congestion but also for enhancing safety and minimizing environmental impacts. In response to these challenges, the design and implementation of intelligent traffic light systems using IoT have become a key focus in the field of transportation engineering. This project aims to provide students with hands-on experience in creating a Multi-Intersection Traffic Light System using Wokwi Lab, offering them a valuable opportunity to explore the IoT functions, the intersection of electronics, microcontroller programming, and traffic engineering. By delving into this project, students will gain insights into the principles behind traffic signal control, as well as practical skills that can contribute to more efficient and sustainable urban transportation systems.
Scenario
Design and develop a two-way traffic light controller with a pedestrian crossing and an emergency signal. The pedestrian crossing will have an LCD display messages whether the pedestrian can or cannot cross the street. When they can cross, the LCD will count down from ten (15) to zero (0) indicating the amount of time they have to cross. The emergency signal will force red lights to turn on in all directions and the blue light will be flashing. The final step is to secure the system so that it could be controlled remotely via a web browser. The alternative final step is a non-internet connection using a motion sensor to allow traffic to continuously flow on major street and only switches to slow traffic street when motion is detected.
Deliverables
• Complete the Course Project PowerPoint Deliverable
• Screenshot of your Circuit Simulation from Wokwi
• Screenshot of Code Simulation from Wokwi
Parts List
• ESP32 Board in Wokwi
• Colored LEDs: Red, Yellow and Green in Wokwi
• Breadboard in Wokwi
Procedures
Setup with Wokwi
1. Visit the Wokwi Website: Open your web browser and go to the Wokwi website by typing in the URL: https://wokwi.com
2. Click on "Sign Up": On the Wokwi homepage, you'll find a "Sign Up" button. Click on it to start the registration process.
3. Choose Your Sign-Up Method: You can choose to sign up using your Google, or email account. Select the method that suits you best.
1. Sign Up with Google: If you choose this option, you'll be redirected to a Google login page. Enter your Google account credentials and proceed to the next step.
2. Sign Up with Email: If you opt to sign up with your email, you'll be prompted to provide the following information:
• Your full name
• Your email address
• A strong password
4. Complete the CAPTCHA: To verify that you are not a robot, you may be asked to complete a CAPTCHA challenge. Follow the instructions on the screen to complete it.
5. Agree to the Terms of Service: Before you can create your account, you'll need to read and accept Wokwi's Terms of Service and Privacy Policy. Make sure to review these documents, and if you agree, check the box to accept them.
6. Click "Sign Up": After you've chosen your sign-up method and agreed to the terms, click the "Sign Up" button.
7. Verify Your Email (if required): Depending on your chosen sign-up method, you might need to verify your email address.
1. If this is the case, check your inbox for a verification email from Wokwi and follow the instructions within to confirm your email address.
Starting with Wokwi
1. Now let’s set up the ESP32 and learn how to add components.
2. Create a New Project:
• Let’s create a new project. Click the ESP32 image.
3. Scroll down the page, under Start Templates click the ESP32 image
Add components
1. Here we will add the breadboard.
2. Add Half and a Full breadboard.
3. Click the in the Wokwi Dashboard
a. Type Breadboard and select the Half breadboard.
4. Connect ESP32 to Half Breadboard:
• Click on the ESP32 module to select it.
• Drag the ESP32 to the breadboard to place it on the Breadboard.
• Click the ESP32 and rotate it so that the ESP32 text is located on the right.
• To do this select the ESP32 and then click the “R” key on your keyboard.
Connect components
1. Connect the components using virtual wires in the Wokwi's workspace.
2. Connect the ESP32 GND pin to the GND (-) on the Half Breadboard and the ESP32 3V3 pin to the PWR (+) on the Half Breadboard
• Use the wires to connect the pins on the ESP32 to the appropriate connections on the half breadboard. Make sure to connect power (3.3V and GND)
Figure 1. ESP32 Connected to Half Breadboard
Code
1. Now that we have all the components connected time to add the code to test our ESP32 with a Wifi Scan
2. Delete the code currently in the Wokwi Code Editor.
3. Copy the code from the next page into the Wokwi Code Editor.
Note: If you did not use the pins configuration suggested in the instructions you will need to modify the code to use the GPIO pins you've connected the components to.
Run the simulation
1. Click the “Start the Simulation” button in Wokwi to start the simulation.
2. Observe the Serial Monitor based on your code's logic.
3. Click the “Stop the Simulation” button in Wokwi to stop the simulation.
4. Change the name of your project by click the pencil next to the title. Enter Module 2 and your name and click RENAME
5. Before closing click save.
Final result
Figure 1 – Code for ESP32 with a WIFI Scan
Figure 2 – Simulation of ESP32 with a WIFI Scan
CODE */
/* ESP32 WiFi Scanning example */
#include "WiFi.h"
int baudrate = 115200;
//North street
int Redpin32 = 32;
int time32_Red_On = 36000; // 26000 is about 25 seconds + -
int time32_Red_Off = 400;
int Yellowpin33 = 33;
int time33_Yellow_On = 12000;
int time33_Yellow_Off = 400;
int Greenpin25 = 25;
int time25_Green_On = 26000;// 20000 is about 20 seconds
int time25_Green_Off = 400;
//South street
int Greenpin19 = 19;
int time19_Green_On = 26000;// 20000 is about 20 seconds
int time19_Green_Off = 400;
int Yellowpin18 = 18;
int time18_Yellow_On = 12000;
int time18_Yellow_Off = 400;
int Redpin5 = 5;
int time5_Red_On = 36000; // 26000 is about 25 seconds + -
int time5_Red_Off = 400;
//Button to component street
int componentStreetButton = 14;
int CrossWalkPinComponentStreet = 13; //button pin
int crossWalkComponentStreetDigitalReadVariable; //Variable to hold button pin data
////int del = 500;//delay used to slow down print button data to monitor.
//btton to CEIS114street
int CEIS114StreetButton = 27;
int CrossWalkPinCEIS114Street = 26; //button pin
int crosswalkCEIS114StreetDigitalReadVariable; //Variable to hold button pin data
void setup() {
//Serial.println("Initializing WiFi...");
//WiFi.mode(WIFI_STA);
//Serial.println("Setup done!");
Serial.begin(baudrate); //presently set to 115200
pinMode(componentStreetButton, OUTPUT); // component street button for the led's on the left
pinMode(CrossWalkPinComponentStreet, INPUT);// presently assigned to digital pin 5
pinMode(CEIS114StreetButton, OUTPUT);
pinMode(CrossWalkPinCEIS114Street, INPUT);// presently assigned to digital pin 5
pinMode(Redpin32, OUTPUT);
pinMode(Yellowpin33, OUTPUT);
pinMode(Greenpin25, OUTPUT);
pinMode(Greenpin19, OUTPUT);
pinMode(Yellowpin18, OUTPUT);
pinMode(Redpin5, OUTPUT);
}
void loop() {
/* Serial.println("Scanning...");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Scan done!");
if (n == 0) {
Serial.println("No networks found.");
} else {
Serial.println();
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(2000);
*/
/*
led_color(0, 255, 255);//red
delay(1000);
led_color(255, 0, 255);//green
delay(1000);
led_color(0, 0, 255); //Yello
//led_color(255-255);//blue
delay(1000);
*/
//analogWrite(red_led, 255-55); // combination of these three produce "yellow"
//nalogWrite(green_led, 255 -255);
//analogWrite(blue_led, 255 - 0);
//delay(delWest);
//________traffic lights Component street_____________
///numread = analogRead(num);
if (crossWalkComponentStreetDigitalReadVariable != 0){
crossWalkComponentStreetDigitalReadVariable =digitalRead(CrossWalkPinComponentStreet);
Serial.println(crossWalkComponentStreetDigitalReadVariable);
}
if (crosswalkCEIS114StreetDigitalReadVariable != 0){
crosswalkCEIS114StreetDigitalReadVariable =digitalRead(CrossWalkPinCEIS114Street);
Serial.println(crosswalkCEIS114StreetDigitalReadVariable);
}
///if (numread >=0){
////while (crossWalkComponentStreetDigitalReadVariable){
//count = count + crossWalkComponentStreetDigitalReadVariable;
////count += crossWalkComponentStreetDigitalReadVariable; // adds each instance of the A-stable to give a +1 sequence
//Serial.println(count += crossWalkComponentStreetDigitalReadVariable);
////Serial.print(count);
////delay(del);
//_______Cross Walk buttons
digitalWrite(componentStreetButton, HIGH);
digitalWrite(CEIS114StreetButton, HIGH);
//_________Traffic Light Component street__________
digitalWrite(Greenpin25, HIGH);
digitalWrite(Redpin5, HIGH);
delay(time25_Green_On);
digitalWrite(Greenpin25, LOW);
delay(time25_Green_Off);
digitalWrite(Yellowpin33, HIGH);
delay(time33_Yellow_On);
digitalWrite(Yellowpin33, LOW);
delay(time33_Yellow_Off);
digitalWrite(Redpin32, HIGH);
delay(7000);
digitalWrite(Redpin5, LOW);
//__________traffic lights CEIS114 street________
digitalWrite(Greenpin19, HIGH);
digitalWrite(Redpin32, HIGH);
delay(time19_Green_On);
//continue green on and switch to yellow
digitalWrite(Greenpin19, LOW);
delay(time19_Green_Off);
digitalWrite(Yellowpin18, HIGH);
delay(time18_Yellow_On);
digitalWrite(Yellowpin18, LOW);
delay(time18_Yellow_Off);
digitalWrite(Redpin5, HIGH);
delay(7000);
digitalWrite(Redpin32, LOW);
}