Skip to content

Instantly share code, notes, and snippets.

@ben-willmore
Created November 6, 2023 12:35
Show Gist options
  • Save ben-willmore/de0024e22cb61126473ce244d2cb9884 to your computer and use it in GitHub Desktop.
Save ben-willmore/de0024e22cb61126473ce244d2cb9884 to your computer and use it in GitHub Desktop.
/***************************************************
Velux controller for ESP8266.
Requires an original Velux remote control switch and an MQTT server.
See link below for how to connect the ESP8266 to the switch:
https://www.reddit.com/r/homeassistant/comments/pzhkia/hack_velux_kli3xx_to_use_blinds_without_gateway/
Derived from https://github.com/adafruit/Adafruit_MQTT_Library
Original code written by Tony DiCola for Adafruit Industries.
Adapted for Velux blinds by Ben Willmore.
MIT license.
****************************************************/
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <Adafruit_MQTT_FONA.h>
#define PULSE_TIME_MS 500
// Connect the Velux remote control switch open/close signals
// to these pins on the ESP8266:
#define PIN_OPEN 5
#define PIN_CLOSE 4
// for wifi/mqtt
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "MY_SSID"
#define WLAN_PASS "MY_WIFI_PASSWORD"
/************************* Adafruit.io Setup *********************************/
#define MQTT_SERVER "192.168.XX.XX"
#define MQTT_SERVERPORT 1883 // use 8883 for SSL
#define MQTT_USERNAME "MY_MQTT_USERNAME"
#define MQTT_KEY "MY_MQTT_PASSWORD"
#define DEVICE_ID "velux-controller"
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiClientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY);
// Setup a feed for subscribing to changes.
Adafruit_MQTT_Subscribe command_feed = Adafruit_MQTT_Subscribe(&mqtt, DEVICE_ID "/blinds/set");
Adafruit_MQTT_Publish state_feed = Adafruit_MQTT_Publish(&mqtt, DEVICE_ID "/blinds", MQTT_QOS_0);
/****************************** Feeds ***************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(PIN_OPEN, OUTPUT);
digitalWrite(PIN_OPEN, HIGH);
pinMode(PIN_CLOSE, OUTPUT);
digitalWrite(PIN_CLOSE, HIGH);
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&command_feed);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &command_feed) {
digitalWrite(LED_BUILTIN, LOW);
if (strcmp((char *)command_feed.lastread, "OPEN") == 0) {
Serial.print("open\n");
digitalWrite(PIN_OPEN, LOW);
delay(PULSE_TIME_MS);
digitalWrite(PIN_OPEN, HIGH);
state_feed.publish("open", 1);
} else {
Serial.print("close\n");
digitalWrite(PIN_CLOSE, LOW);
delay(PULSE_TIME_MS);
digitalWrite(PIN_CLOSE, HIGH);
state_feed.publish("closed", 1);
}
digitalWrite(LED_BUILTIN, HIGH);
}
}
if(! mqtt.ping()) {
mqtt.disconnect();
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment