OpenHAB is stable. The learning curve is steep if you have little or no Java background. There are lots of examples.
No code changes at all required from the posted example.
MQTT: I was running the Mosquitto-3.1 Debian repo version and decided to upgrade to the Mosquitto-1.4.3 version.
The results seem to be worth it. I was getting 'half open' TCP communications sometimes on node startup with the old version. The new version has 2-way communications first time, every time so far...
I did an apt-get purge of the old mosquitto and used this for the new version install. I changed the version number as required. http://www.xappsoftware.com/wordpress/2014/10/27/installing-mosquitto-on-raspberry-pi/
PubSubClient library for the esp8266: My previous setup used the 3 year old knolleary/pubsubclient library. It worked, but the better choice these days is the lmroy/pubsubclient library. https://github.com/Imroy/pubsubclient
The new PubSubClient library only works with the newest versions of ESP8266/Arduino.
I had been holding off updating, but the mqtt pubsubclient left me no choice. I upgraded to Staging version Aug 10, 2015.
The code for the esp8266-12 on the yellow carrier board got an overhaul as well:
/*
mqtt_openHAB_115_3
Example program for esp8266-12 on the yellow carrier board.
Used to interact over local intranet with a mosquitto v1.4.3 server. OpenHAB also running on server computer.
Ver.3 Support mosquitto 1.4.3 and the @lmroy PubSubClient library for esp8266/arduino.
Implement lmroy's wifi and mqtt initialization within the main loop method.
Ver.2 Changed mqtt messaging structure to support multiple esp boards addressed by openHAB at different IP addresses
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
ADC_MODE(ADC_TOUT);
const char* ssid = "your ssid";
const char* password = "your password";
IPAddress mqttServer( 192, 168, 1, 114 );//Hardcode of mosquitto server IP on local intranet connected computer.
byte IP[] = { 192, 168, 1, 115 };//Hardcode for this esp8266 node
byte Netmask[] = { 255, 255, 255, 0 };//Local router info
byte Gateway[] = { 192, 168, 1, 1 }; //Local router info
WiFiClient wifiClient;
PubSubClient mqttClient(mqttServer);
long millis_now; // For non-blocking delay
long millis_prev; // For non-blocking delay
long LEDredValue = 0;
long LEDgreenValue = 0;
long LEDblueValue = 0;
long lightsensor = 0;
long lightsensor_prev = 0;
int pir = 0;
int pir_flag = 0;
//----------------------------------------------------------------------------
// Callback function
//----------------------------------------------------------------------------
void mqttCallback(const MQTT::Publish& pub) {
String msgString = pub.payload_string();
if (String(pub.topic()) == "openHAB/esp_115/RED") {
LEDredValue = round(msgString.toInt() * 10.2);
analogWrite(15,LEDredValue);
}
if (String(pub.topic()) == "openHAB/esp_115/GREEN") {
LEDgreenValue = round(msgString.toInt() * 10.2);
analogWrite(12,LEDgreenValue);
}
if (String(pub.topic()) == "openHAB/esp_115/BLUE") {
LEDblueValue = round(msgString.toInt() * 10.2);
analogWrite(13,LEDblueValue);
}
if (String(pub.topic()) == "openHAB/esp_115/led1") {
if (msgString == "ON") digitalWrite( 2, 0 ); //red leds are inverted output
else digitalWrite( 2, 1 );
}
if (String(pub.topic()) == "openHAB/esp_115/led2") {
if (msgString == "ON") digitalWrite( 0, 0 ); //red leds are inverted output
else digitalWrite( 0, 1 );
}
if (String(pub.topic()) == "openHAB/esp_115/led3") {
if (msgString == "ON") digitalWrite( 4, 0 ); //red leds are inverted output
else digitalWrite( 4, 1 );
}
if (String(pub.topic()) == "openHAB/esp_115/led4") {
if (msgString == "ON") digitalWrite( 5, 0 ); //red leds are inverted output
else digitalWrite( 5, 1 );
}
if (String(pub.topic()) == "openHAB/esp_115/led5") {
if (msgString == "ON") digitalWrite( 14, 0 ); //red leds are inverted output
else digitalWrite( 14, 1 );
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void setup()
{
// ESP-12 yellow carrier board GPIO assignments
pinMode(A0, INPUT); // analog light sensor input
pinMode(2, OUTPUT); // 00000X discrete red led position
pinMode(0, OUTPUT); // 0000X0 discrete red led position
pinMode(4, OUTPUT); // 000X00 discrete red led position
pinMode(5, OUTPUT); // 00X000 discrete red led position
pinMode(14,OUTPUT); // 0X0000 discrete red led position
pinMode(16,INPUT); // PIR sensor
pinMode(15,OUTPUT); // RGB led red
pinMode(12,OUTPUT); // RGB led green
pinMode(13,OUTPUT); // RGB led blue
// Initialize GPIO 'inverted' led outputs to off
digitalWrite(2, 1); // red leds are inverted output
digitalWrite(0, 1); // red leds are inverted output
digitalWrite(4, 1); // red leds are inverted output
digitalWrite(5, 1); // red leds are inverted output
digitalWrite(14,1); // red leds are inverted output
digitalWrite(15,0); // RGB led red not inverted
digitalWrite(12,0); // RGB led green not inverted
digitalWrite(13,0); // RGB led blue not inverted
Serial.begin(115200);
delay(10);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
Serial.println();
Serial.print("WiFi connecting to ");
Serial.print(ssid);
Serial.println("...");
yield();
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected.");
WiFi.config(IPAddress(IP[0],IP[1],IP[2],IP[3] ),
IPAddress(Gateway[0],Gateway[1],Gateway[2],Gateway[3] ),
IPAddress(Netmask[0],Netmask[1],Netmask[2],Netmask[3] ));
WiFi.mode(WIFI_STA);
Serial.println("ESP8266 IP address: ");
Serial.println(WiFi.localIP());
}
if (WiFi.status() == WL_CONNECTED) {
if (!mqttClient.connected()){
yield();
if (mqttClient.connect("espClient_115")){
mqttClient.publish("esp_115","Hello from esp8266 mqtt client");
mqttClient.set_callback(mqttCallback);
mqttClient.subscribe("openHAB/esp_115/#");
Serial.println("Connected to mqtt server.");
}
}
if (mqttClient.connected()){
yield();
mqttClient.loop(); // Keep mqtt connection open and allow callbacks to work
pir = digitalRead(16); // Read PIR
if (pir == 1 && pir_flag == 0) {
pir_flag = 1;
mqttClient.publish("esp_115/pir_sensor", "1");
}
if (pir == 0 && pir_flag == 1) {
pir_flag = 0;
mqttClient.publish("esp_115/pir_sensor", "0");
}
millis_now = millis();
if (millis_now > (millis_prev + 5000)){
millis_prev = millis_now;
lightsensor = analogRead(A0);
if ((lightsensor > lightsensor_prev + 15) ||
(lightsensor < lightsensor_prev - 15)){
char sensor[8];
lightsensor_prev = lightsensor;
itoa(lightsensor,sensor,10); // Integer to string
mqttClient.publish("esp_115/light_sensor", sensor);
}
}
}
}
}
Somewhat (OK, very) surprisingly with so many changes at one time, everything is working nicely so far.
Time will tell how stable it really is. I now have five esp-12 nodes talking to the openHAB program through the mosquitto mqtt broker for a total of 45 digital I/O points and 5 analog inputs for pennies per point. Things are looking pretty good at the moment for affordable home automation!
I found this to be a good overview for learning the mqtt way: http://www.hivemq.com/mqtt-essentials-wrap-up/