0% found this document useful (0 votes)
5 views3 pages

2 Led Iot

This document is an Arduino sketch for controlling two LEDs via MQTT and displaying their states on an OLED screen. It connects to a Wi-Fi network and subscribes to topics to receive commands for turning the LEDs on or off. The OLED display updates to reflect the current state of the LEDs based on the received messages.

Uploaded by

ahmedissaoui980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

2 Led Iot

This document is an Arduino sketch for controlling two LEDs via MQTT and displaying their states on an OLED screen. It connects to a Wi-Fi network and subscribes to topics to receive commands for turning the LEDs on or off. The OLED display updates to reflect the current state of the LEDs based on the received messages.

Uploaded by

ahmedissaoui980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Arduino.

h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <U8g2lib.h>

// Wi-Fi and MQTT settings


const char* ssid = "ahmed";
const char* password = "12345678";
const char* mqtt_server = "test.mosquitto.org";

// Initialize OLED Display


U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
WiFiClient espClient;
PubSubClient client(espClient);

// LED Pin Definitions


const int ledPin1 =2;
const int ledPin2 =4;

// LED States
bool stateled1 = false;
bool stateled2 = false;

// Function to connect to Wi-Fi


void setup_wifi() {
Serial.println("\nConnecting to WiFi...");
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}

Serial.println("\nWiFi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}

// Function to initialize OLED


void setup_oled() {
u8g2.begin();
}

// Function to display LED states on OLED


void affichage() {
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);

u8g2.drawStr(10, 20, "A Aissaoui ");


u8g2.drawStr(10, 30, "groupe1A ");

// Display LED 1 state


if (stateled1) {
u8g2.drawStr(10, 50, "LED1 on");
} else {
u8g2.drawStr(10, 50, "LED1 off");
}
// Display LED 2 state
if (stateled2) {
u8g2.drawStr(10, 60, "LED2 on");
} else {
u8g2.drawStr(10, 60, "LED2 off");
}

u8g2.sendBuffer();
}

// MQTT Callback Function


void callback(char* topic, byte* payload, unsigned int length) {
char message[length + 1];
memcpy(message, payload, length);
message[length] = '\0'; // Null-terminate string

Serial.print("Message received [");


Serial.print(topic);
Serial.print("]: ");
Serial.println(message);

bool updated = false; // Flag to check if OLED needs update

// Handling LED1 topic


if (strcmp(topic, "iot1") == 0) {
Serial.println("Handling LED1 command...");
if (strcmp(message, "1") == 0) {
digitalWrite(ledPin1, HIGH);
stateled1 = true;
updated = true;
} else if (strcmp(message, "0") == 0) {
digitalWrite(ledPin1, LOW);
stateled1 = false;
updated = true;
}
}

// Handling LED2 topic


else if (strcmp(topic, "iot2") == 0) {
Serial.println("Handling LED2 command...");
if (strcmp(message, "1") == 0) {
digitalWrite(ledPin2, HIGH);
stateled2 = true;
updated = true;
} else if (strcmp(message, "0") == 0) {
digitalWrite(ledPin2, LOW);
stateled2 = false;
updated = true;
}
}

// Update OLED if necessary


if (updated) {
affichage();
}
}

// Function to Reconnect to MQTT Broker


void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESPClient")) {
Serial.println("Connected!");
client.subscribe("iot1");
client.subscribe("iot2");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" Trying again in 5 seconds...");
delay(5000);
}
}
}

void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(115200);

setup_wifi();
setup_oled();

client.setServer(mqtt_server, 1883);
client.setCallback(callback);

affichage(); // Initial OLED update


}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();

delay(100);
}

You might also like