97 lines
2.6 KiB
Arduino
97 lines
2.6 KiB
Arduino
|
#include <ESP8266WiFi.h>
|
||
|
#include <PubSubClient.h>
|
||
|
#include <Adafruit_NeoPixel.h>
|
||
|
|
||
|
// Configurations WiFi et MQTT
|
||
|
const char* ssid = "fablab";
|
||
|
const char* password = "geek make code do";
|
||
|
const char* mqtt_server = "192.168.73.20";
|
||
|
const char* mqtt_topic = "vulture/buzzer/pressed/1";
|
||
|
const char* mqtt_message = "{\"buzzer_id\": 1, \"color\": \"#2867d4\"}";
|
||
|
//hostname wifi et client id mqtt
|
||
|
const char* esp_name = "BUZZER-1";
|
||
|
|
||
|
#define PIN D2 // Pin de données connectée à la bande WS2812B
|
||
|
#define NUM_LEDS 1 // Nombre de LEDs dans la bande
|
||
|
#define BRIGHTNESS 127 // Luminosité (127 = ~50%)
|
||
|
|
||
|
// Déclaration des broches
|
||
|
#define BUTTON_PIN D8
|
||
|
|
||
|
WiFiClient espClient;
|
||
|
PubSubClient client(espClient);
|
||
|
Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
|
||
|
|
||
|
void blinkWhileConnecting() {
|
||
|
static unsigned long previousMillis = 0;
|
||
|
static bool ledState = false;
|
||
|
const long interval = 300; // clignotement toutes les 300ms
|
||
|
|
||
|
while (WiFi.status() != WL_CONNECTED) {
|
||
|
unsigned long currentMillis = millis();
|
||
|
if (currentMillis - previousMillis >= interval) {
|
||
|
previousMillis = currentMillis;
|
||
|
ledState = !ledState;
|
||
|
strip.setPixelColor(0, ledState ? strip.Color(255, 255, 255) : 0); // Jaune ON/OFF
|
||
|
strip.show();
|
||
|
}
|
||
|
yield(); // permet à l'ESP de ne pas bloquer le watchdog
|
||
|
}
|
||
|
|
||
|
// Une fois connecté, on allume la LED en fixe
|
||
|
strip.setPixelColor(0, strip.Color(0, 0, 255));
|
||
|
strip.show();
|
||
|
}
|
||
|
|
||
|
void setup_wifi() {
|
||
|
delay(10);
|
||
|
Serial.println();
|
||
|
Serial.print("Connexion au WiFi...");
|
||
|
WiFi.hostname(esp_name);
|
||
|
WiFi.begin(ssid, password);
|
||
|
|
||
|
blinkWhileConnecting(); // Clignotement pendant la connexion
|
||
|
|
||
|
Serial.println("\nWiFi 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);
|
||
|
strip.begin();
|
||
|
strip.setPixelColor(0, strip.Color(255, 255, 255));
|
||
|
strip.show();
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|