OpenHAB, Raspberry PI, MQTT and an ESP8266

I have been having a really hard time getting my raspberry pi and OpenHAB configured. Ive been able to get it installed so far but I have not had any luck. The steps I’ve followed so far are to install Raspbian log in with putty and run the following commands:

#UPDATE RASBERIAN
sudo apt-get update
sudo apt-get upgrade
sudo raspi-config #EXPAND PARTITION

#INSTALL OpenHAB
sudo mkdir /opt/openhab
cd /opt/openhab
sudo wget https://bintray.com/artifact/download/openhab/bin/distribution-1.7.1-runtime.zip
sudo unzip distribution-1.7.1-runtime.zip
sudo rm distribution-1.7.1-runtime.zip

#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
path = /opt/openhab
browseable = Yes
writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
public = no
force user = root
#Change Password
sudo smbpasswd -a pi

#BOOT ON STARTUP
sudo nano /etc/init.d/openhab
#COPY STARTUP SCRIPT BELOW INTO FILE
sudo chmod 777 /etc/init.d/openhab
sudo /etc/init.d/openhab start
/etc/init.d/openhab status
sudo /etc/init.d/openhab stop
sudo /etc/init.d/openhab restart
sudo update-rc.d openhab defaults
sudo reboot

    #STARTUP SCRIPT
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides:          openhab
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: OpenHAB Daemon
    # Description: Build your smart home in no time!
    ### END INIT INFO
    
    
    ROOT_DIR=/opt/openhab
    
    
    # set path to eclipse folder. If local folder, use '.'; otherwise, use /path/to/eclipse/
    eclipsehome="$ROOT_DIR/server";
    
    
    # set ports for HTTP(S) server
    HTTP_PORT=8080
    HTTPS_PORT=8443
    
    
    # get path to equinox jar inside $eclipsehome folder
    cp=$(find $eclipsehome -name "org.eclipse.equinox.launcher_*.jar" | sort | tail -1);
    
    
    
    
    # This next line determines what user the script runs as.
    # Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
    #USER=root
    DAEMON_USER=root
    DAEMON_NAME=openhab
    
    
    DAEMON=/usr/bin/java
    DAEMON_ARGS="-Djna.boot.library.path=/usr/lib/jni -Dgnu.io.rxtx.SerialPorts=/dev/ttyAMA0 -Dosgi.clean=true -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=$HTTP_PORT -Djetty.port.ssl=$HTTPS_PORT -Djetty.home=. -Dlogback.configurationFile=configurations/logback.xml -Dfelix.fileinstall.dir=addons -Djava.library.path=lib -Djava.security.auth.login.config=./etc/login.conf -Dorg.quartz.properties=./etc/quartz.properties -Dequinox.ds.block_timeout=240000 -Dequinox.scr.waitTimeOnBlock=60000 -Dfelix.fileinstall.active.level=4 -Djava.awt.headless=true -jar $cp $* -console "
    PIDFILE=/var/run/$DAEMON_NAME.pid
    
    
    . /lib/lsb/init-functions
    
    do_start() {      
      log_daemon_msg "Starting system $DAEMON_NAME daemon"
      start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --chdir $ROOT_DIR --exec $DAEMON -- $DAEMON_ARGS
      log_end_msg $?
    }
    do_stop() {
      log_daemon_msg "Stopping system $DAEMON_NAME daemon"
      start-stop-daemon --stop --pidfile $PIDFILE --retry 10
      log_end_msg $?
    }
    
    case "$1" in
    
        start|stop)
            do_${1}
            ;;
    
        restart|reload|force-reload)
            do_stop
            do_start
            ;;
    
        status)
            status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
            ;;
        *)
            echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
            exit 1
            ;;
    
    esac
    exit 0


#Get Bindings Directory
sudo mkdir addons_repo
cd addons_repo
sudo wget https://bintray.com/artifact/download/openhab/bin/distribution-1.7.1-addons.zip
sudo unzip distribution-1.7.1-addons.zip
sudo rm distribution-1.7.1-addons.zip


#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"

Everything up to this point works great. I have not been able to get anything successful past here though. I have a ESP8266 connected to my network with a simple sample webserver connected to it so I can turn an LED on and off by taking my browser to the ESP’s ip address. But that is as far as I have got. I can’t figure out how to get anything to work.

This is my default.sitemap

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

This is my default.items

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]"}

Ive been trying to figure out how to even test if the Rpi’s MQTT binding is getting the signal from the webpage and from there I can’t figure out what to put in my arduino sketch to send to the ESP8266 so that it will recognize the switch signals from the sitemap. I was hoping to write a step by step guide for anyone with this set up since I think its a pretty common one. Any help would be greatly appreciated

Thank you.

How do you expect to communicate with your ESP8266? Via the webpage or via MQTT or does your code implement both?

The reason I ask is that the webserver/HTTP requests and MQTT are two different protocols and it sounds like you’re mixing them up.

Sorry that was confusing I want to use MQTT For all communication between openhab and the ESP. I just used the sample webpage code to make sure that I had it hooked up right and that I was flashing it correctly. I want to use OpenHab to control everything.

I think it would be better to use the apt-get method for installing openHAB on Raspbian. Easier to maintain; fewer questions about file ownership, easier to start at boot, etc.

1 Like

Great thank you for the suggestions. This is really what I’m looking for is best practices. I am very new to Linux and OpenHAB, I have some basic programming skills but mostly with C. How would you initiate the startup if you use apt-get and what would the commands look like as opposed to what I have? I plan on doing a complete fresh install once I get everything working and I wanted to do a write up for others.

The system startup script is installed with sudo apt-get install openhab-runtime. The link given above has more. To install the MQTT binding, you would then run sudo apt-get install openhab-addon-binding-mqtt, also described at the link.

In Linux and other Unix variants, it is usually preferable to run a service like the openhab runtime as a regular user instead of as the root user. Before doing the apt-get installation method, you may need to create a user named openhab, under which the server will run:

sudo adduser openhab

Then proceed with the apt-get installation. (I might be wrong and the apt-get installation may create this user if it doesn’t already exist, but I’m not sure.)

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?