OpenHAB, Raspberry PI, MQTT and an ESP8266

I can confirm, at least on Ubuntu, the apt-get does create the openhab user and group for you.

1 Like

If you install it using apt-get, on Raspberry Pi you would use:

sudo service openhab start
sudo service openhab stop
sudo service openhab restart
sudo service openhab status

With apt-get install the config files go to /etc/openhab, the logs go to /var/log/openhab, and the home install directory is /usr/share/openhab. You can watch the logs on screen using tail -f /var/log/openhab/openhab.log.

To be complete for future readers who are on Ubuntu 15+ or a system that uses systemd rather than upstart the commands are:

sudo systemctl start openhab
sudo systemctl stop openhab
sudo systemctl restart openhab
sudo systemctl status openhab

If you install it using apt-get, openhab will be configured to start as a service on boot.

3 Likes

Which should I use for Raspbian? And what is the difference, Sorry for my ignorance but I am very new to Linux.

I tried this and it does not automatically start openhab after I reboot. Any suggestions?

For esp8266 i sugest you this firmware http://www.wifi-iot.ru/
No need esp8266 programming, all you need is to select modules, push the button to create firmware and then flash the esp.

Raspbian is set up to use Upstart. Ubuntu 15 by default uses systemd (older versions use Upstart). This isn’t really something you have to worry about except to be aware of what system you are on so you know what commands to use.

If you installed using apt-get then it should have been configured to start at boot automatically. The service openhab start command is what you use to control the service manually after booting.

If you didn’t install using apt-get or something went wrong during the install, you can add a service (something you can successfully control using the commands listed above) with the command:

update-rc.d openhab defaults

Ok so I reformatted the PI and restarted from scratch this was my work flow

sudo raspi-config #EXPAND PARTITION AND CHANGE DEVICE NAME

#Start AUTOCONFIG
sudo apt-get update
sudo apt-get upgrade
    
#INSTALL OpenHAB
wget -qO - 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -
echo "deb http://dl.bintray.com/openhab/apt-repo stable main" | sudo tee /etc/apt/sources.list.d/openhab.list
sudo apt-get update
sudo apt-get install openhab-runtime

#BootOnStartup
sudo update-rc.d openhab defaults

#Rebooted and made sure it was running using: sudo service openhab status 

#INSTALL MOSQUITO
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
rm mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

#Test MOSQUITO
Window 1: mosquitto_sub -d -t hello/world
Window 2: mosquitto_pub -d -t hello/world -m "Greetings from Terminal window 2"


#Samba:
sudo apt-get install samba samba-common-bin
sudo nano /etc/samba/smb.conf
# Uncomment "wins support = no" and change to yes
# Add this to the bottom
[OpenHAB]
comment = OpenHAB
browseable = Yes
path = /etc/openhab
writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
public = no
force user = root

#Change Password
sudo smbpasswd -a pi

#Install HABmin
sudo /etc/init.d/openhab stop #stop habmin
cd /etc/openhab
sudo wget https://github.com/cdjackson/HABmin/releases/download/0.1.3-snapshot/habmin.zip
sudo unzip habmin.zip
sudo rm habmin.zip
sudo /etc/init.d/openhab start #start habmin

I cannot get the habmin webpage to work any Suggestions?

HABmin needs to go in /usr/share/openhab/addons for the jar file and /usr/share/openhab/webapps for everything else.

When openHAB starts up you will see a HABmin has started in the logs.

With an apt-get install, configs are in /etc/openhab, logs are in /var/log/openhab, addons (eg bindings ) are installed to /usr/share/openhab/addons, the webapps folder where habmin and the icons for the sitemap live as well as the binaries and scripts are also in the /usr/share/openhab folder.

YEP! That worked perfect Thank you! Ok so now that I’ve got my install squared away. back to my original question. How do I program my ESP8266 so that I can control it with openhab? I’m using Arduino to flash the ESP unless there is an easier way I should be doing it?

I don’t know Jack about ESP8266, but I do know the most common approach I’ve seen is to have the remote sensors send and receive messages over the wireless to a gateway Arduino or raspberry pi which pub/subs the messages via MQTT. So openHAB only actually needs to know about MQTT.

There are many options. Like Rich said, you can use MQTT. One advantage of that approach is the Pi doesn’t need to know the ESP8266 IP address. The disadvantage is that it’s another point of failure and a separate server to manage.

For my ESP8266-based sensors, I set them up with an embedded web server and query them periodically with HTTP. It’s relatively simple (no middleware) and a web browser can be used for basic debugging. The downside of this approach is that openHAB must know the IP address of the ESP8266. That could be a problem with a large number of devices. They’d typically need to have static IP addresses. However, there is mDNS support for the ESP8266 so you could assign a host name to each device and the Pi would be able to resolve it to an address. I haven’t used mDNS yet, but I plan to experiment with it soon.

Another option is a simple TCP or UDP-based servers on the ESP8266 that could be accessed using the openHAB TCP binding. For example, a sensor could broadcast a temperature value periodically over UDP.

The ESP8266 could be more active and push values into openHAB using the Rest API. In this case all the devices would need to know the address of the openHAB server.

Many possibilities. At this point, I’m leaning towards the embedded web server (with mDNS) for my devices.

Sorry I wasn’t clear as I was in my original post. Am looking for an example code that I can load onto my ESP that will turn on and off 2 leds that I have hooked up to it on pin #0 and #13

My default.sitemap looks like this:

sitemap test label="test Sitemap"
    {
    	Frame label="MQTT" {
    	Switch item=mqttsw1 label="MQTT Switch 1"   
    	Switch item=mqttsw2 label="MQTT Switch 2"
    	}
    }

My default.items look like this

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mymosquitto:/testsw/1:command:on:default],>[mymosquitto:/testsw/1:command:off:default]"}
Switch mqttsw2 "Switch 2" (all) {mqtt=">[mymosquitto:/testsw/2:command:off:default],>[mymosquitto:/testsw/2:command:on:default]"}

What should my code look like that I’m loading onto my esp look like? I want to use MQTT for communication and I am using Arduino.

1 Like

The openHAB forum probably isn’t the right place for your question, as it’s nothing to do with openHAB.

Are you using the Arduino IDE configured for ESP8266?

Yes the Arduino IDE is configured for ESP8266 I have loaded a sample web server on it and got it to turn on and off just to prove it is configured correctly and can connect to my wifi. I just dont know how to get it to talk to OpenHAB

There are numerous ways to get them to communicate, that’s the beauty of openHAB. You’ve mentioned MQTT, but you could also use your existing web server and the HTTP binding instead which may be easier than you having to reprogram/code your ESP8266.

If you chose to keep your webserver and use the HTTP binding then the follow link should be a good start place.

https://github.com/openhab/openhab/wiki/Http-Binding

1 Like

I don’t really know anything about the web server I just put in some sample code from a website and it would turn on and off. I currently have Easy ESP installed on the 8266 but I can’t find any examples of how to get that to work either. Basically Im just looking for a solution there are tons out there but none of them tell you step by step what to do.

For simplicity’s sake, keep the webserver code on your ESP8266 and control it using openHAB with HTTP binding.

If you let me know the URLs for controlling it I can post some updated Switch configs. This assumes you only care about one way communication from openHAB -> ESP8266.

I,m in the same boat as you,

So i managed to get ESP8266 working with openhab over MQTT.
I’m using EasyESP as well. i followed this guide over here http://www.esp8266.nu/index.php/Tutorial_OpenHAB_Switch
But the problem was openhab did you want to send MQTT messages, apparently the way you install it matters,
Installing it using apt-get is the one that finally worked for me.

However i still had to manually copy the openhab.binding.mqtt.jar file into the addons folder and then use apt-get install method for mqtt to get it work. why it worked like that is beyond me but as long as it works, i wont question it.

Hope it helps.

Preface: I’m new to Raspberry Pi, OpenHAB, etc.

I followed Joseph’s process and it appeared to go well, however, HABmin doesn’t open when I go to http://localip:8080/habmin/index.html. Is there something more I need to do to be able to access openHAB?

BTW, I haven’t loaded any config/sitemap/etc files for openHAB yet.

Edit: Once I installed the demo files, HABmin worked. This thread was helpful in getting everything into the right folders: HABmin + Z-Wave binding