import
This commit is contained in:
commit
78401ace13
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
.vscode/*
|
||||||
|
arduino/sonoffentrance/sonoffentrance.h
|
||||||
|
.travis.yml
|
||||||
|
lib/*
|
||||||
|
.pioenvs
|
||||||
|
.piolibdeps
|
||||||
|
.clang_complete
|
||||||
|
.gcc-flags.json
|
||||||
|
platformio.ini
|
||||||
34
README.md
Normal file
34
README.md
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
Introduction
|
||||||
|
============
|
||||||
|
Switch sonoff pour controler la lumière du salon (stupid LEDs).
|
||||||
|
Fonctionnement simple : situé après l'interrupteur, il permet d'éteindre la lumière du salon si celle-ci est allumée, ou de l'allumer si celle-ci a été éteinte par le sonoff.
|
||||||
|
Si l'interrupteur est utilisé pour éteindre la lumière, il ne permet pas de la rallumer.
|
||||||
|
|
||||||
|
L'état dans HA n'est pas forcé et dans le code (void setupSonOff() au moment du setup()) on mets l'état du relay à HIGH, c'est à dire que la lumière s'allume dès que le sonoff est alimenté.
|
||||||
|
|
||||||
|
Matériel
|
||||||
|
========
|
||||||
|
[Sonoff Switch (ou Basic)](https://www.itead.cc/smart-home/sonoff-wifi-wireless-switch.html?acc=70efdf2ec9b086079795c442636b55fb)
|
||||||
|
Un TTL-Serial USB
|
||||||
|
|
||||||
|
Source
|
||||||
|
======
|
||||||
|
Firmware : [KmanOz/Sonoff-HomeAssistant](https://github.com/KmanOz/Sonoff-HomeAssistant)
|
||||||
|
Particulièrement cette [version](https://github.com/KmanOz/Sonoff-HomeAssistant/tree/master/arduino/ESPsonoff-v1.01pOTA).
|
||||||
|
p car on s'en fout d'avoir un dht22 (version t).
|
||||||
|
|
||||||
|
TODO : lui utilise la lib [Imroy/pubsubclient](https://github.com/Imroy/pubsubclient) qui permet :
|
||||||
|
I use the lmroy version of this excellent mqtt library, mainly because it supports QOS1 and keepalive settings right from within the sketch. No other modifications to library files are necessary to achieve a rock solid connection to your mqtt broker.
|
||||||
|
À voir si nécessaire ou non.
|
||||||
|
|
||||||
|
Ordre des PINs
|
||||||
|
==============
|
||||||
|

|
||||||
|
[source](https://randomnerdtutorials.com/how-to-flash-a-custom-firmware-to-sonoff/)
|
||||||
|
```
|
||||||
|
| VIN (3.3v) |
|
||||||
|
| RX |
|
||||||
|
OUT | TX | IN
|
||||||
|
| GND |
|
||||||
|
| GPIO14 |
|
||||||
|
```
|
||||||
209
arduino/sonoffentrance/sonoffentrance.cpp
Normal file
209
arduino/sonoffentrance/sonoffentrance.cpp
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
// Bouton
|
||||||
|
#include <Ticker.h>
|
||||||
|
|
||||||
|
// Wifi
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
|
// MQTT
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
// OTA
|
||||||
|
#include <ESP8266mDNS.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
|
||||||
|
// RemoteDebug
|
||||||
|
#include <RemoteDebug.h>
|
||||||
|
|
||||||
|
#include "sonoffentrance.h"
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(SERIAL_SPEED);
|
||||||
|
Serial.println("\nresetting");
|
||||||
|
|
||||||
|
setupSonOff();
|
||||||
|
|
||||||
|
// WIFI
|
||||||
|
setupWifi();
|
||||||
|
|
||||||
|
// OTA
|
||||||
|
setupOTA();
|
||||||
|
|
||||||
|
// RemoteDebug
|
||||||
|
Debug.begin(REMDEB_CLIENT);
|
||||||
|
|
||||||
|
// MQTT
|
||||||
|
client.setServer(MQTT_SERVER, MQTT_PORT);
|
||||||
|
client.setCallback(callbackMQTT);
|
||||||
|
testConnectMQTT();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupSonOff() {
|
||||||
|
pinMode(LED_PIN, OUTPUT);
|
||||||
|
pinMode(RELAY_PIN, OUTPUT);
|
||||||
|
pinMode(BUTTON_PIN, INPUT);
|
||||||
|
relayState = true;
|
||||||
|
btn_timer.attach(0.05, button);
|
||||||
|
}
|
||||||
|
|
||||||
|
// OTA
|
||||||
|
void setupOTA()
|
||||||
|
{
|
||||||
|
ArduinoOTA.setHostname(OTA_CLIENT); // on donne une petit nom a notre module
|
||||||
|
ArduinoOTA.setPassword(OTA_PASSWORD);
|
||||||
|
ArduinoOTA.onStart([]() {
|
||||||
|
Debug.println("OTA Starting");
|
||||||
|
Serial.println("OTA Starting");
|
||||||
|
blinkLED(LED_PIN, 400, 2);
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
});
|
||||||
|
ArduinoOTA.onEnd([]() {
|
||||||
|
Debug.println("\nOTA End");
|
||||||
|
Serial.println("\nOTA End");
|
||||||
|
});
|
||||||
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||||
|
Debug.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
||||||
|
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
|
||||||
|
digitalWrite(LED_PIN, LOW);
|
||||||
|
delay(5);
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
});
|
||||||
|
ArduinoOTA.onError([](ota_error_t error) {
|
||||||
|
Serial.printf("OTA Error[%u]: ", error);
|
||||||
|
Debug.printf("OTA Error[%u]: ", error);
|
||||||
|
blinkLED(LED_PIN, 40, 2);
|
||||||
|
if (error == OTA_AUTH_ERROR) {
|
||||||
|
Serial.println("Auth Failed");
|
||||||
|
Debug.println("Auth Failed");
|
||||||
|
} else if (error == OTA_BEGIN_ERROR) {
|
||||||
|
Serial.println("Begin Failed");
|
||||||
|
Debug.println("Begin Failed");
|
||||||
|
} else if (error == OTA_CONNECT_ERROR) {
|
||||||
|
Serial.println("Connect Failed");
|
||||||
|
Debug.println("Connect Failed");
|
||||||
|
} else if (error == OTA_RECEIVE_ERROR) {
|
||||||
|
Serial.println("Receive Failed");
|
||||||
|
Debug.println("Receive Failed");
|
||||||
|
} else if (error == OTA_END_ERROR) {
|
||||||
|
Serial.println("End Failed");
|
||||||
|
Debug.println("End Failed");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// WIFI
|
||||||
|
void setupWifi()
|
||||||
|
{
|
||||||
|
Serial.print("Connexion a ");
|
||||||
|
Serial.print(WIFI_SSID);
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println(" OK");
|
||||||
|
Serial.print("IP : ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
// MQTT
|
||||||
|
void testConnectMQTT()
|
||||||
|
{
|
||||||
|
while (!client.connected()) {
|
||||||
|
Debug.print("Connexion au serveur MQTT... ");
|
||||||
|
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
|
||||||
|
Debug.print("OK\nSend Current State");
|
||||||
|
mqttSendState();
|
||||||
|
|
||||||
|
Debug.print("OK\nSubscribe");
|
||||||
|
client.subscribe(MQTT_COMMAND);
|
||||||
|
|
||||||
|
Debug.println(" OK");
|
||||||
|
} else {
|
||||||
|
Debug.print("KO, erreur : ");
|
||||||
|
Debug.print(client.state());
|
||||||
|
Debug.println(", on attend 5 secondes avant de recommencer");
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Déclenche les actions à la réception d'un message
|
||||||
|
void callbackMQTT(char* topic, byte* payload, unsigned int length)
|
||||||
|
{
|
||||||
|
String stopic = String(topic);
|
||||||
|
|
||||||
|
unsigned int i = 0;
|
||||||
|
for(i = 0; i < length; i++) {
|
||||||
|
message_buff[i] = payload[i];
|
||||||
|
}
|
||||||
|
message_buff[i] = '\0';
|
||||||
|
String msgString = String(message_buff);
|
||||||
|
|
||||||
|
Debug.print("Received [" + stopic + "] : ");
|
||||||
|
Debug.println(msgString);
|
||||||
|
|
||||||
|
if (stopic == MQTT_COMMAND) {
|
||||||
|
if (msgString == "ON") {
|
||||||
|
relayState = true;
|
||||||
|
} else if (msgString == "OFF") {
|
||||||
|
relayState = false;
|
||||||
|
} else if (msgString == "reset"){
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
mqttSendState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void mqttSendState()
|
||||||
|
{
|
||||||
|
client.publish(MQTT_STATE, (relayState) ? "ON": "OFF", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// OTA
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
|
||||||
|
// RemoteDebug
|
||||||
|
Debug.handle();
|
||||||
|
|
||||||
|
// MQTT
|
||||||
|
testConnectMQTT();
|
||||||
|
client.loop();
|
||||||
|
|
||||||
|
if (relayState) {
|
||||||
|
digitalWrite(LED_PIN, LOW);
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
} else {
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void blinkLED(int pin, int duration, int n) {
|
||||||
|
for(int i = 0; i < n; i++) {
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
delay(duration);
|
||||||
|
digitalWrite(LED_PIN, LOW);
|
||||||
|
delay(duration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void button() {
|
||||||
|
if (!digitalRead(BUTTON_PIN)) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (count > 1 && count <= 40) {
|
||||||
|
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
|
||||||
|
digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));
|
||||||
|
}
|
||||||
|
else if (count > 40) {
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
49
arduino/sonoffentrance/sonoffentrance.example.h
Normal file
49
arduino/sonoffentrance/sonoffentrance.example.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#define SERIAL_SPEED 115200
|
||||||
|
|
||||||
|
// OTA
|
||||||
|
#define OTA_PASSWORD "XXX"
|
||||||
|
#define OTA_CLIENT "sonoff_living"
|
||||||
|
|
||||||
|
// DebugRemote
|
||||||
|
#define REMDEB_CLIENT "sonoff_living"
|
||||||
|
RemoteDebug Debug;
|
||||||
|
|
||||||
|
// WIFI
|
||||||
|
#define WIFI_SSID "XXX"
|
||||||
|
#define WIFI_PASSWORD "XXX"
|
||||||
|
|
||||||
|
WiFiClient espClient;
|
||||||
|
|
||||||
|
#define BUTTON 0
|
||||||
|
#define RELAY 12
|
||||||
|
#define LED 13
|
||||||
|
|
||||||
|
// MQTT
|
||||||
|
#define MQTT_SERVER "XXX"
|
||||||
|
#define MQTT_PORT 1883
|
||||||
|
#define MQTT_USER "XXX"
|
||||||
|
#define MQTT_PASS "XXX"
|
||||||
|
|
||||||
|
#define MQTT_CLIENT "sonoff_living"
|
||||||
|
#define MQTT_COMMAND "sonoff_living/switch"
|
||||||
|
#define MQTT_STATE "sonoff_living/status"
|
||||||
|
|
||||||
|
char message_buff[100];
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
// Bouton
|
||||||
|
Ticker btn_timer;
|
||||||
|
unsigned long count = 0;
|
||||||
|
|
||||||
|
bool relayState;
|
||||||
|
|
||||||
|
void setup();
|
||||||
|
void loop();
|
||||||
|
void blinkLED(int pin, int duration, int n);
|
||||||
|
void button();
|
||||||
|
void setupOTA();
|
||||||
|
void setupWifi();
|
||||||
|
void testConnectMQTT();
|
||||||
|
void callbackMQTT(char* topic, byte* payload, unsigned int length);
|
||||||
|
void setupSonOff();
|
||||||
|
void mqttSendState();
|
||||||
0
arduino/sonoffentrance/sonoffentrance.ino
Normal file
0
arduino/sonoffentrance/sonoffentrance.ino
Normal file
22
platformio.example.in
Normal file
22
platformio.example.in
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
; PlatformIO Project Configuration File
|
||||||
|
;
|
||||||
|
; Build options: build flags, source filter
|
||||||
|
; Upload options: custom upload port, speed and extra flags
|
||||||
|
; Library options: dependencies, extra library storages
|
||||||
|
; Advanced options: extra scripting
|
||||||
|
;
|
||||||
|
; Please visit documentation for the other options and examples
|
||||||
|
; http://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
|
[env:sonoff]
|
||||||
|
platform=espressif8266
|
||||||
|
; source: http://www.instructables.com/id/Use-Homie-Firmware-to-Drive-Sonoff-Switch-Module-E/
|
||||||
|
; https://github.com/arendst/Sonoff-MQTT-OTA-Arduino/issues/231
|
||||||
|
board=esp01_1m
|
||||||
|
framework=arduino
|
||||||
|
upload_port=<IP>
|
||||||
|
upload_flags=--auth="<PASS>"
|
||||||
|
|
||||||
|
[platformio]
|
||||||
|
src_dir=arduino/sonoffentrance
|
||||||
|
lib_dir=~/Arduino/libraries
|
||||||
Loading…
x
Reference in New Issue
Block a user