Hey guys,
i started to work with openHAB. I would say i am a beginner. But dont worry, i learn fast
At the moment i am testing, what would be the best way to write my project right.
Following components are to include:
Amazon Alexa
MQTT Server to Communicate with ESP8266 with everything in my flat like lamps, windows, switches etc. I am an electican, so i know about the risk of 230V AC.
Switches publishes to the MQTT Sever and Lamps are subscribed in Topics like:
âopenHAB/Garden/Lamp1/â i combined Alexa and it worked to switch, dimm and change color.
Sketch is like this:
#include <ESP8266Wifi.h>
#include <PubSubClient.h>
#include <NeoPixel.h>
/*--------------------------------------------------*/
//Variablen definieren
int Rot;
int Gruen;
int Blau;
int Helligkeit;
int Geschwindigkeit;
/*--------------------------------------------------*/
//W-Lan Zugangsdaten SmartHomeServer
const char* ssid = "Router";
const char* password = "1234567890";
//IP Konfiguration SmartHomeServer
IPAddress ip(192,168,1,11);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
//W-Lan verbindung zum SmartHomeServer
void verbinde_zu_WLan() {
Serial.print("Verbinde zu W-Lan:");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
/*--------------------------------------------------*/
// MQTT Server
const char* mqtt_server = "192.168.1.2";
// Name des MQTT Clients
String clientID = "Garten_Lampe 1-";
// WiFi und MQTT Objekte initialisieren
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// Verbinde zu MQTT Broker
void verbinde_zu_MQTT() {
while (!client.connected()) {
Serial.println("Verbinde zu MQTT...");
clientID += String(random(0xffff), HEX);
if (client.connect(clientID.c_str())) {
Serial.println("Connected to MQTT Broker!");
//MQTT abonnements definieren
client.subscribe("openHAB/Garten/Lampe1/Programm");
client.subscribe("openHAB/Garten/Lampe1");
client.subscribe("openHAB/Garten/Lampe1/Geschwindigkeit");
}
else {
Serial.print("Connection to MQTT Broker failed");
}
delay(2000);
}
}
/*--------------------------------------------------*/
/*--------------------------------------------------*/
//MQTT Daten einlesen und verarbeiten
void callback(char* topic, byte* payload, unsigned int length) {
//Nachrichten empfangen und in Serial Monitor anzeigen
Serial.println();
Serial.print("Nachricht angekommen in Thema: ");
Serial.println(topic);
//Serial.print("Nachricht:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("---------------------");
/*--------------------------------------------------*/
payload[length] = '\0';
//Programm auswerten
if (String(topic) == "openHAB/Garten/Lampe1/Programm") {
//Wert als String speichern
String Programm = String((char*)payload);
Serial.println(Programm);
//Programm verarbeiten
if (String(Programm) == "Regenbogen") {
Serial.println("Regenbogen aktiv");
}
if (String(Programm) == "") {}
}
/*--------------------------------------------------*/
//Lampe auswerten
if (String(topic) == "openHAB/Garten/Lampe1") {
//Wert in String umwandeln
String Lampe = String((char*)payload);
Serial.println(Lampe);
//Programm verarbeiten
if (String(Lampe) == "ON") {
Serial.println(Lampe + " aktiv");
}
else if (String(Lampe) == "OFF") {
Serial.println(Lampe + " aktiv");
}
else if(Lampe.length() > 3) {
Rot = Lampe.substring(0, Lampe.indexOf(",")).toInt();
Serial.print("Rot - ");
Serial.println(Rot);
Gruen = Lampe.substring(Lampe.indexOf(",") + 1, Lampe.lastIndexOf(',')).toInt();
Serial.print("Gruen - ");
Serial.println(Gruen);
Blau = Lampe.substring(Lampe.lastIndexOf(',') + 1).toInt();
Serial.print("Blau - ");
Serial.println(Blau);
onecolor(strip.Color(Rot,Gruen,Blau));
}
else {
Helligkeit = Lampe.toInt();
Serial.print("Helligkeit - ");
Serial.println(Helligkeit);
}
}
/*--------------------------------------------------*/
if (String(topic) == "openHAB/Garten/Lampe1/Geschwindigkeit") {
String Geschwindigkeit = String((char*)payload);
Serial.println(Geschwindigkeit);
Geschwindigkeit = Geschwindigkeit.toInt();
Serial.println(Geschwindigkeit);
}
}
/*--------------------------------------------------*/
void setup() {
//Serielle Schnittstelle konfigurieren
Serial.begin(115200);
//erstmalige W-Lan verbindung aufbauen
verbinde_zu_WLan();
//MQTT Callback definieren
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
//Strip starten
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
/*--------------------------------------------------*/
void loop() {
if (!client.connected()) {
verbinde_zu_MQTT();
}
client.loop();
}
The mqtt.items is like this:
Color MQTT_Switch_Dimm "Licht Wohnzimmer" <colorlight> ["Lighting"] {mqtt=">[mosquitto:openHAB/Wohnzimmer/Lampe/dimmen:command:*:default]"}
Now i want to call programs like a rainbow effect with alexa. But i am not sure, how to write the item tag and the channel. Is it possible to define the item, so alexa just send a text? I dont want to say âswitch program rainbow on/offâ. Is it possible to say âAlexa, Garden Lamp1 Rainbowâ and MQTT publish will be âopenHAB/Garden/Lamp1/Programâ with payload ârainbowâ as text?
I hope i did not to much wrong with my first post. And sry for my bad english. In my sketch you will see my mother language