Browse Source

import

master
Julien Cabillot 3 years ago
committed by JC
commit
848a7fd583
9 changed files with 139 additions and 0 deletions
  1. 1
      .gitignore
  2. BIN
      IMG_0214.jpg
  3. 27
      README.md
  4. 10
      ha_configuration.yml
  5. BIN
      logo.png
  6. 12
      mqttled.example.h
  7. BIN
      mqttled.fzz
  8. 89
      mqttled.ino
  9. BIN
      mqttled_bb.png

1
.gitignore

@ -0,0 +1 @@
mqttled.h

BIN
IMG_0214.jpg

Before After
Width: 800  |  Height: 534  |  Size: 137 KiB

27
README.md

@ -0,0 +1,27 @@
Introduction
============
Simple blink qui toggle la led à chaque appel MQTT.
Pour mettre la LED du port D0 à ON :
```bash
mosquitto_pub -d -u "<USER>" -P "<PASS>" -t "<TOPIC>" -m "ON"
```
Pour la mettre à Off :
```bash
mosquitto_pub -d -u "<USER>" -P "<PASS>" -t "<TOPIC>" -m "OFF"
```
Un exemple de configuration pour home-assistant se trouve dans [ha_configuration.yml](ha\_configuration.yml).
Matériel
========
* 1x ESP8266 Lolin (Nodemcu v3)
* 1x Breadboard
* 1x Resistance 220Ω (jusqu'à 1kΩ)
* 1x LED
Médias
======
![Fritzing BreadBoard](mqttled_bb.png)
![IRL](IMG_0214.jpg)

10
ha_configuration.yml

@ -0,0 +1,10 @@
switch:
platform: "mqtt"
name: "Test LED"
command_topic: "homeassistant/switch1" #Topic sur lequel on publie l'état de l'interrupteur
payload_on: "ON" # A vous de choisir le message envoyé lorsque l'interrupteur est allumé 
payload_off: "OFF" # et éteint
optimistic: true # Mettez à true pour maintenir l'état
qos: 0
retain: true
value_template: '{{ value.x }}'

BIN
logo.png

Before After
Width: 300  |  Height: 300  |  Size: 13 KiB

12
mqttled.example.h

@ -0,0 +1,12 @@
// LED
#define LED_PIN D0
// WIFI
#define wifi_ssid "XXX"
#define wifi_password "XXX"
// MQTT
#define mqtt_server "XXX"
#define mqtt_port 1883
#define mqtt_user "XXX"
#define mqtt_password "XXX"

BIN
mqttled.fzz

89
mqttled.ino

@ -0,0 +1,89 @@
// C'est moche mais c'est pour récuperer EVERY_N_SECONDS :)
#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WIFI
WiFiClient espClient;
// MQTT
char message_buff[100];
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
Serial.println("\nresetting");
// WIFI
setupWifi();
// MQTT
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callbackMQTT);
testConnectMQTT();
client.subscribe("homeassistant/switch1");
pinMode(LED_PIN, OUTPUT);
}
// WIFI
void setupWifi() {
Serial.println();
Serial.print("Connexion a ");
Serial.print(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("IP : ");
Serial.println(WiFi.localIP());
}
// MQTT
void testConnectMQTT() {
while (!client.connected()) {
Serial.print("Connexion au serveur MQTT... ");
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("OK");
} else {
Serial.print("KO, erreur : ");
Serial.print(client.state());
Serial.println(", on attend 5 secondes avant de recommencer");
delay(5000);
}
}
}
void callbackMQTT(char* topic, byte* payload, unsigned int length) {
unsigned int i = 0;
Serial.print("Received [" + String(topic) + "] : ");
for(i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
Serial.print(msgString);
if (msgString == "ON") {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
void loop() {
testConnectMQTT();
client.loop();
EVERY_N_SECONDS(180) {
Serial.print("MQTT Subscribe refresh");
client.subscribe("homeassistant/select1");
Serial.println(" done");
}
}

BIN
mqttled_bb.png

Before After
Width: 903  |  Height: 756  |  Size: 134 KiB
Loading…
Cancel
Save