//Write a program to display messages based on the button pressed on the
//web app and assign the OLED display, 3 servo motors, and 4 LEDs pins.
#include <WiFi.h>
#include <PubSubClient.h>
#include <ESP32Servo.h> // used to control the servo motors
#include <Wire.h> // used to enable wire connection in the circuit.
#include <Adafruit_GFX.h> // helps us to write messages on the OLED display.
#include <Adafruit_SSD1306.h> // helps us to write messages on the SSD1306 OLED displa
char clientId[50]; // can store up to 50 characters.
int servoPin1 = 12;
Servo servo1;
int servoPin2 = 4;
Servo servo2;
int servoPin3 = 15;
Servo servo3;
WiFiClient espClient; //server details
PubSubClient client(espClient);
int ledPin1 = 4; //red led
int ledPin2 = 12; //white led
int ledPin3 = 13; //yellow led
int ledPin4 = 5; //green led
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(128, 64, &Wire, 1); //display(width , height of the OLED display, connected to wire , reset=1)
void setup() {
Serial.begin(9600); //starts communication
//➔ 500: It is 500 microseconds (pulse width) when the servo angle is 0 degrees. It is a minimum value.
//➔ 2400: It is 2400 microseconds (pulse width) when the servo angle is 180 degrees. It is a maximum value
servo1.attach(servoPin1, 500, 2400);
servo2.attach(servoPin2, 500, 2400);
servo3.attach(servoPin3, 500, 2400);
//checking if the OLED display is connected or not-
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 OLED display
Serial.println(F("SSD1306 allocation failed"));
for(;;); //print only 1 time on console
}
delay(2000); //2 sec
display.clearDisplay();
display.setTextSize(1); //size of the text
display.setTextColor(WHITE);
Serial.print("Connecting to …..");
WiFi.mode(WIFI_STA); //to search a WiFi station
WiFi.begin("Wokwi-GUEST","", 6); //username, password
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
client.setServer("broker.emqx.io", 1883);
client.setCallback(callback); //written below
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
}
//prewritten code
void mqttReconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(clientId)) {
Serial.println(" connected");
client.subscribe("topicName/iot");
} else {
Serial.print("failed, rc=");
Serial.print(client.state()); //state as -2 etc
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String stMessage; //message received from MQTT means 1/2/3/4/5/6
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);//print message
stMessage += (char)message[i]; //complete message in terms of numbers
}
//class 300 starts
// starts here
if(stMessage == "1"){
Serial.println("1");
display.clearDisplay();
display.setTextSize(1); //set text size on OLED
display.setTextColor(WHITE);
display.setCursor(10, 0); //column=10 , row=0
// Display static text
display.println("Release Orbital Access Arm");
display.display(); //display the defined output text on the OLED Screen
servo1.write(180); //rotate the arm of Servo Motor by 180 Degrees
}
else if(stMessage == "2"){
Serial.println("2");
digitalWrite(ledPin1, HIGH); //green LED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 0);
// Display static text
display.println("Main engine gimbal test and lift beanie Cap");
display.display();
servo2.write(0); //No rotation
}
else if(stMessage == "3"){
Serial.println("3");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 0);
// Display static text
display.println("Activate Hydrogen Burn off system");
display.display();
digitalWrite(ledPin2, HIGH); // yellow LED
}
else if(stMessage == "4"){
Serial.println("4");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 0);
// Display static text
display.println("Main engine ignite");
display.display();
digitalWrite(ledPin3, HIGH); //white LED
}
else if(stMessage == "5"){
Serial.println("5");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 0);
// Display static text
display.println("Hydrogen vent Arm | Release tail surface mass | Detonate hold-down bolts");
display.display();
servo3.write(360); //rotate by 360 degree
}
else if(stMessage == "6"){
Serial.println("6");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 0);
// Display static text
display.println("Ignite Both SRBs");
display.display();
digitalWrite(ledPin4, HIGH); //red LED
}
}
// ends here
void loop() {
delay(1000);
if (!client.connected()) {
mqttReconnect();
}
client.loop();
}