Automated Chicken Coop pt. 5 – Remotely Controlling the Coop pt. 4 “ENTER THE RASPBERRY PI 3”

Remotely Controlling our Chicken Coop pt4

Up until now we’ve simply prepared the NodeMCU for programming, before we move on to programming it to do as we want we need to consider how we are going to send commands and retrieve data from our system.  While you could set this up with any PC or Laptop, I thought it best to create a small server.  For this project, and future projects – the Raspberry PI 3 is perfect for our purposes.  It is a low powered computer that can run several Linux distros as well as its own Linux distro Raspbian.  It also comes with WiFi and LAN connectivity and a host of other features.

So get yourself a Raspberry Pi 3, 32GB+ SD Card, and power supply then follow the following instructions to install Raspbian on the Pi3.  The instructions are for the former Raspberry Pi 2 B+, but are still relevant to this step.

Setting up your Raspberry PI

So you’ve got your Raspberry Pi 3 up and running with a fresh installation of Raspbian.  You’re logged in and have it connected to your network either by LAN or WiFi (see link below for how to set up WiFi).

How to Setup Wi-Fi and Bluetooth on the Raspberry Pi 3

Once connected, go into your router/modem and set the IP of the raspberry pi to Static.  We never want the IP address of the Raspberry PI to change on the network.  How this is done varies from router/modem, so I suggest looking up information online on how to perform this step on your particular device.

Our next step is to ensure that the raspberry pi is up to date, then install an MQTT broker.  MQTT is the protocol we will use to communicate with our Coop because it is lightweight and easy to configure.  We will also perform an installation of LAMP so we have a MySQL Database to store information retrieved from the Coop to.

Open a Terminal window (see image), type the following command and press Enter.  When prompted type “Y” and let the Pi update.  Depending on the speed of your internet connection this may take some time.

sudo apt-get update


Once done, you will have updated your Raspbian installation to the most up-to-date version.

Next we need to install our MQTT broker, known as EMQTTD.  But before we do that, lets assign our Raspberry Pi’s hostname.  In the terminal window type the following command:

sudo raspi-config

This will bring you to an old school type of menu.  Select “Advanced Options” then select “Hostname”.  Give your Raspberry Pi an unique and descriptive hostname.  For instance I provided mine with the hostname “Raspberry-MQTT”.

Exit the menu and restart your Raspberry Pi 3.  Once it is back up reopen the terminal and you should see pi@”YourPiHostname”.  Now we are ready to install EMQTTD.


EMQTTD requires that you build and install from source on a Raspberry Pi.   If you’ve never installed a Linux package in this manner, you’re possibly in for a big headache.  Even I had issues installing EMQTTD, but I will provide you with as much experience as I can towards doing so.

EMQTTD documentation and issues/solutions can be found at the following github page.

Open your terminal and enter each of these lines, pressing ENTER after each line and letting it install the various packages.

sudo apt-get update
sudo apt-get install wget 
sudo apt-get install libssl-dev 
sudo apt-get install ncurses-dev 
tar -xzvf otp_src_19.2.tar.gz 
cd otp_src_19.2/ 
./configure make sudo make install 
cd .. 
rm otp_src_19.2.tar.gz sudo 
rm -R otp_src_19.2/ 
git clone 
cd emq-relx && 
make cd _rel/emqttd && 
./bin/emqttd console 

I could never get EMQTTD to install as a normal service that would be easily started on boot.  But I developed a work around.  Open a new terminal window and type in

cd /etc/init.d

This will place you inside the init.d folder of your root directory.  Now when we performed the git clone of EMQTTD it created a folder in our pi (default user)’s home directory.  What we are going to do is create a script that references the service in that directory and include it in our rc.local file later.

Now that you are inside the init.d folder type:

sudo nano

Nano is a text editor inside terminal.  When we typed after it we told it to create a file named, we will put our script in this file.  So looking at your editor you should see nothing but the commands listed at the bottom.  Type the following into the editor, being mindful of spaces, then press Ctrl+X to exit.  You’ll be prompted to save, press “Y” then press enter to save with the name we specified earlier.

#Start the EMQTTD Service On Startup
sudo /home/pi/emq-relx/_rel/emqttd/bin/emqttd start

Finally, we are going to place a reference to this file in a file named rc.local.  rc.local is called each time the PI starts up.  Meaning our script will be run at boot and our EMQTTD Broker will start as well.


cd /etc

now we are in the /etc directory.  We are going to open the rc.local file for changes so type:

sudo nano rc.local

Once again we are in the nano editor this time looking at the rc.local file.  At the bottom of the file, but above the line that reads “exit 0” type the following lines.

#start emqttd broker on startup
sudo /etc/init.d/


The Raspberry Pi 3 has a bug when it comes to updating the time of the PI.  Since there is no hardware clock on the Raspberry PI 3 like there is on say – your laptop, when you disconnect the power from your Raspberry PI 3 after shutting it down, it cannot keep track of the time.  So the Raspberry Pi 3 reaches out to Time Servers on boot, to get the current time based on the localization settings in raspi-config.  Unfortunately – it only works when trying to update using a LAN cable by default.  So if you were using WiFi, your raspberry pi would never update the time to the correct time.

So here’s the fix.  Below the lines where you added our reference (but above the “exit 0″ line, type the following to enable the Raspberry PI 3 to update the time via WiFi.

#Fix WiFi NTP (network time protocol)

sudo /sbin/iptables -t mangle -I POSTROUTING 1 -o wlan0 -p udp --dport 123 -j TOS --set-tos 0x00


Press Ctrl + X and then press Y to save.  Press Enter to confirm and reboot your Raspberry Pi.

After Reboot

To test your MQTT installation open a second terminal window and type the following command.

mosquitto_sub -h localhost -t "test"

In your first terminal window type the following command.

mosquitto_pub -h localhost -t "test" -m "Hello World!" -q 1

If you look back into your second terminal window “Hello World!” should be displayed.  Congratulations!  You’ve installed the EMQTTD MQTT Broker successfully.

In the next section of this series – we will further discuss the MQTT model, install LAMP,  and set up our NodeMCU to communicate with the server.

Leave a Reply

Your email address will not be published. Required fields are marked *