Tracking du dossier VHard (les différents code des ESP, matériels embarqués.. etc)
This commit is contained in:
73
VHard/buzzer/standard_vert/standard_vert.ino
Normal file
73
VHard/buzzer/standard_vert/standard_vert.ino
Normal file
@ -0,0 +1,73 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
// Configurations WiFi et MQTT
|
||||
const char* ssid = "fablab";
|
||||
const char* password = "geek make code do";
|
||||
//const char* ssid = "SaucissonLand";
|
||||
//const char* password = "47C619A574D41311C811EB563C";
|
||||
const char* mqtt_server = "192.168.73.20";
|
||||
const char* mqtt_topic = "vulture/buzzer/pressed/3";
|
||||
const char* mqtt_message = "{\"buzzer_id\": 3, \"color\": \"#28D42E\"}";
|
||||
//hostname wifi et client id mqtt
|
||||
const char* esp_name = "BUZZER-3";
|
||||
|
||||
// Déclaration des broches
|
||||
#define BUTTON_PIN D8
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
void setup_wifi() {
|
||||
delay(10);
|
||||
Serial.println();
|
||||
Serial.print("Connexion au WiFi...");
|
||||
WiFi.hostname(esp_name);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connecté");
|
||||
Serial.print("Adresse IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
Serial.print("Connexion au broker MQTT...");
|
||||
if (client.connect(esp_name)) {
|
||||
Serial.println("connecté");
|
||||
} else {
|
||||
Serial.print("échec, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println("; nouvelle tentative dans 5 secondes");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
pinMode(BUTTON_PIN, INPUT_PULLUP);
|
||||
|
||||
setup_wifi();
|
||||
client.setServer(mqtt_server, 1883);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
|
||||
// Vérifier si le bouton est pressé
|
||||
if (digitalRead(BUTTON_PIN) == HIGH) {
|
||||
Serial.println("Bouton pressé, envoi du message...");
|
||||
client.publish(mqtt_topic, mqtt_message);
|
||||
delay(200); // Anti-rebond pour éviter les publications multiples
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user