#include <WiFi.h>
#include <PubSubClient.h>
#include <MFRC522.h>
#include <Stepper.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";
WiFiClient espclient;
bool moteurEnMarche = false; // Variable pour suivre l'état du moteur
String pir_str,mq5_str/*,rfid_str*/; //see last code block below use these to convert the float that you get back from DHT
char pir[50], mq5[50]/*,rfid[50]*/;
int mq5Value =100;
// PIR sensor
const int pirPin = 22;
// MQ5 sensor
const int mq5Pin = 2;
// RFID reader
#define SS_PIN 5
#define RST_PIN 21
const int ipaddress[4] = {103, 97, 67, 25};
byte nuidPICC[4] = {0, 0, 0, 0};
MFRC522::MIFARE_Key key;
MFRC522 rfid = MFRC522(SS_PIN, RST_PIN);
// Stepper motor
const int stepsPerRevolution = 2048;
#define IN1 19
#define IN2 18
#define IN3 5
#define IN4 17
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
void setup() {
//set the speed of the motor at 5 rpm
myStepper.setSpeed(5);
pinMode(2, OUTPUT);
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
SPI.begin();
rfid.PCD_Init();
}
void reconnect() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void callback(char* topic, byte* payload, unsigned int length1) {
Serial.print("message arrived[");
Serial.print(topic);
Serial.println("]");
if (strcmp(topic, "marche") == 0) {
if (payload[0] == '1') {
moteurEnMarche = true;
Serial.println("Moteur activé");
} else if (payload[0] == '0') {
moteurEnMarche = false;
myStepper.step(0); // Arrêter le moteur
Serial.println("Moteur arrêté");
}
}
}
PubSubClient client(mqtt_server, 1883, callback, espclient);
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
delay(2000);
// After reading PIR sensor
bool motion = digitalRead(pirPin);
if (motion==1)
Serial.println("Motion Detected");
else
Serial.println("NO-Motion Detected");
delay(1500);
// After reading MQ5 sensor
int mq5Value =100 /*analogRead(mq5Pin)*/;
Serial.println("The data from Gas sensor is");
Serial.println(mq5Value);
delay(1500);
// After reading RFID reader
//readRFID();
// After reading RFID reader
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
Serial.println("Carte RFID détectée!");
String rfidData = "111333444555";
for (byte i = 0; i < rfid.uid.size; i++) {
rfidData += String(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
rfidData += String(rfid.uid.uidByte[i], HEX);
}
// Publiez les données RFID sur MQTT
if (client.publish("rfid_data", rfidData.c_str())) {
Serial.println("Données RFID publiées sur MQTT.");
} else {
Serial.println("Échec de la publication des données RFID sur MQTT.");
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
//convertir de type float a string et string a char
pir_str = String(motion); //converting temperature (the float variable above) to a string
pir_str.toCharArray(pir, pir_str.length() + 1); //packaging up the data to publish to mqtt whoa...
mq5_str = String(mq5Value);//converting temperature (the float variable above) to a string
mq5_str.toCharArray(mq5, mq5_str.length() + 1);; //packaging up the data to publish to mqtt whoa...
//rfid_str = "Valeur RFID"; // Remplacez par les données réelles du RFID
// rfid_str.toCharArray(rfid, rfid_str.length() + 1);
// Connect to Node-RED
if (client.connect("ESP32test1")) {
Serial.println("Connected to Node-RED");
client.subscribe("marche");
client.subscribe("arret");
// Publish data for the PIR sensor
client.publish("pir", pir);
// Publish data for the MQ5 sensor
client.publish("mq5", mq5);
// client.publish("rfid", rfid);
} else {
Serial.print("failed, rc=");
Serial.println(client.state());
delay(500);
}
}