/*
https://wokwi.com/projects/350761084378939988
On se propose de réaliser un système IoT à base de microcontrôleur ESP32
qui permet de:
1. SI le Mot de passe saisie est correct
récupérer les paramètres suivants (Nom,Prenom, password) à partir de Broker MQTT (Topic: Mardi/Info)
2.SiNON
Envoyer les informations au serveur Beebotte
parametre Beebotte
channel:Mardi
Channel Token:token_rGseePUCxhKd8HOU
resource:"Nom"
resource:"Prenom"
resource:"Password"
parametre de Broker MQTT:
server: ds-lce3-2023.japaneast.cloudapp.azure.com
port:1883
username:student
password:lce3
clientID: <Your Name>
topic:Mardi/Info
FORMAT Json File: {"data":["Nom":"votre-nom","Prenom":"votre-prenom"],"Password":"1234" }
*/
/**************************************************/
/**************************************************/
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char message[6];
char key;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {23, 22, 21, 19};
byte colPins[COLS] = {18, 5, 4, 2};
int i=0;
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const String password = "1234";
String input_password;
void lireString() {
char key = keypad.getKey();
if (key) {
Serial.print(key);
if (key == '*') {
input_password = ""; // reset the input password
} else if (key == '#') {
if (input_password == password ) {
Serial.println();
Serial.println("The password is correct");
} else {Serial.println();
Serial.println("The password is incorrect");
}
input_password = ""; // reset the input password
} else {
input_password += key; // append new character to input password string
}
}
}
void setup(){
Serial.begin(9600);
}
void loop(){
lireString();
}
#include <WiFi.h>
#include <PubSubClient.h>
const char *ssid = "Votre-SSID"; // Remplacez par le nom de votre réseau WiFi
const char *password = "Votre-Mot-de-Passe"; // Remplacez par le mot de passe de votre réseau WiFi
const char *mqttServer = "ds-lce3-2023.japaneast.cloudapp.azure.com";
const int mqttPort = 1883;
const char *mqttUser = "student";
const char *mqttPassword = "lce3";
const char *clientID = "Votre-Nom";
const char *mqttTopic = "Mardi/Info";
const char *beebotteChannel = "Mardi";
const char *beebotteToken = "token_rGseePUCxhKd8HOU";
const char *beebotteResourceNom = "Nom";
const char *beebotteResourcePrenom = "Prenom";
const char *beebotteResourcePassword = "Password";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.println("Message arrived in topic: " + String(topic));
// Convertir le tableau de bytes en une chaîne de caractères
String message;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
// Analyser le message JSON
// Vous devrez peut-être ajuster cela en fonction de la structure réelle de vos données JSON
// Dans l'exemple, je suppose que le message est dans le format donné
if (message.indexOf("data") != -1 && message.indexOf("Nom") != -1 && message.indexOf("Prenom") != -1 && message.indexOf("Password") != -1) {
// Mot de passe correct
Serial.println("Mot de passe correct");
// Extraire les informations
String nom = message.substring(message.indexOf("\"Nom\":\"") + 8, message.indexOf("\",\"Prenom\""));
String prenom = message.substring(message.indexOf("\"Prenom\":\"") + 11, message.indexOf("\",\"Password\""));
String password = message.substring(message.indexOf("\"Password\":\"") + 13, message.indexOf("\"}"));
// Envoyer les informations à Beebotte
sendToBeebotte(nom, prenom, password);
} else {
// Mot de passe incorrect
Serial.println("Mot de passe incorrect");
// Envoyer les informations à Beebotte
sendToBeebotte("", "", "");
}
}
void sendToBeebotte(String nom, String prenom, String password) {
// Connexion à Beebotte
if (client.connect(clientID, beebotteToken, "")) {
Serial.println("Connected to Beebotte");
// Envoi des données à Beebotte
client.publish(beebotteChannel + "/" + beebotteResourceNom, nom.c_str());
client.publish(beebotteChannel + "/" + beebotteResourcePrenom, prenom.c_str());
client.publish(beebotteChannel + "/" + beebotteResourcePassword, password.c_str());
Serial.println("Data sent to Beebotte");
} else {
Serial.println("Failed to connect to Beebotte");
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
// Connexion au Broker MQTT
if (client.connect(clientID, mqttUser, mqttPassword)) {
Serial.println("Connected to MQTT broker");
client.subscribe(mqttTopic);
} else {
Serial.println("Failed to connect to MQTT broker");
}
}
void loop() {
if (!client.connected()) {
// Reconnecter au Broker MQTT si la connexion est perdue
setup_wifi();
setup();
}
// Maintenir la connexion MQTT active
client.loop();
}
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
const char *ssid = "VotreSSID";
const char *password = "VotreMotDePasse";
const char *mqtt_server = "ds-lce3-2023.japaneast.cloudapp.azure.com";
const int mqtt_port = 1883;
const char *mqtt_username = "student";
const char *mqtt_password = "lce3";
const char *mqtt_clientID = "VotreNom";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
// Le code de configuration WiFi reste inchangé
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.println("Message received:");
Serial.println(topic);
DynamicJsonDocument doc(256);
deserializeJson(doc, payload);
const char *receivedPassword = doc["Password"];
if (strcmp(receivedPassword, "1234") == 0) {
// Mot de passe correct, récupérer les paramètres
const char *nom = doc["data"]["Nom"];
const char *prenom = doc["data"]["Prenom"];
const char *password = doc["Password"];
// Faire quelque chose avec les données récupérées
Serial.println("Données récupérées du MQTT:");
Serial.println("Nom: " + String(nom));
Serial.println("Prenom: " + String(prenom));
Serial.println("Password: " + String(password));
} else {
// Mot de passe incorrect, envoyer les informations à Beebotte
sendDataToBeebotte(doc["data"]["Nom"], doc["data"]["Prenom"], doc["Password"]);
}
}
void sendDataToBeebotte(const char *nom, const char *prenom, const char *password) {
HTTPClient http;
String url = "https://api.beebotte.com/v1/data/write/Mardi?token=token_rGseePUCxhKd8HOU";
url += "&resource=Nom&data=" + String(nom);
url += "&resource=Prenom&data=" + String(prenom);
url += "&resource=Password&data=" + String(password);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Données envoyées à Beebotte:");
Serial.println(payload);
} else {
Serial.println("Échec de la requête HTTP");
}
http.end();
}
void reconnect() {
// Le code de reconnexion MQTT reste inchangé
}
void setup() {
// Initialisation du port série et du WiFi
Serial.begin(115200);
setup_wifi();
// Configuration du client MQTT
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
// Le code principal reste inchangé
if (!client.connected()) {
reconnect();
}
client.loop();
}