Automated Chicken Coop pt. 6 – Remotely Controlling The Coop pt. 5 “MQTT Overview, & LAMP”

In the last post we started setting up the Raspberry Pi to act as the data storage and command control of the automated coop system.  One thing we installed was Mosquitto – a MQTT Broker.

So what is MQTT?

MQTT works on a subscription model.  Lets go further into this by using the following diagram.

 

MQTT messages are sent with Topics.  For this example, lets say the topic is “ChickenCoop”.  Device 1 publishes a message with the topic “ChickenCoop”.  Since Devices 4-6 are Subscribed to ChickenCoop, they receive the message that Device 1 sent.  If Device 1 was also Subscribed to the topic ChickenCoop, it would also receive a copy of the message it just sent.

Lets look back at the commands we used to test the MQTT Broker earlier.


mosquitto_sub -h localhost -t "test"

mosquitto_sub = indicates we are subscribing to a topic.  A subscriber is known as a client.

-h localhost = since we were subscribing to a topic on the actual broker machine, we could refer to it as localhost.  Later on we will reference the hostname of the MQTT broker that we set up in the last post.

-t “test” = this indicated what topic we wanted to subscribe to.


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

mosquitto_pub = indicates we are publishing a message to a topic.

-h localhost = the name/location of the MQTT Broker.  Like in the subscription example above, we could refer to this as localhost because we were publishing to a topic while on the Broker machine.

-t “test” = this indicates what topic to post the message to

-m “Hello World!” = this was the actual message sent to the MQTT Broker on the topic “test”

-q 1 = this indicates the quality of service or QoS of the message.  There are 3 QoS levels for an MQTT Broker\Clients.

  • At most once (0) – send the message to the clients at most one time
  • At least once (1) – send the message to the clients at least one time
  • Exactly once (2) – send the message exactly one time.

For more information on QoS levels with an MQTT Broker you can go to the following website.  For now, just understand that levels 1 and 2 require a acknowledgement from the client that the message has been received.

MQTT Essentials Part 6: Quality of Service 0, 1 & 2

One thing to note is our automated chicken coop can only publish messages using QoS 0, meaning it does not know whether our Broker or the Clients received the message.  It can however subscribe to QoS level 1.

When we set up the NodeMCU to communicate with the MQTT Broker, we wont use the commands we used for testing our Broker.  But it is important to understand, that while we are not using these exact commands, in the background these commands are what is being transmitted over the network to the Broker.

 

Storing Information Sent To Our Server

You might want to store information from your Chicken Coop as it performs actions and transmits messages over MQTT.

As was stated above, because we will only be able to publish messages from the coop using QoS 0, you should not expect to get messages that are sensitive.  There is no guarentee of a message being delivered to the Broker.  An example of this would be if the Raspberry Pi was unplugged.  If the NodeMCU tried to send a message, the Raspberry Pi MQTT Broker would not receive the message, and no steps would be taken by the NodeMCU to resend the message.  Keep this in mind when deciding what data you would like to send back to the MQTT Broker.

But how do we store the information we DO receive?  The Broker discards the message after knowing it was received by the clients after all, and the clients dont necessarily have to store the message.

Enter LAMP and further on –  Node-Red.  LAMP is a webserver package for Linux, its name stands for Linux Apache MySQL PHP.  In fact, you are reading this post from a webserver running LAMP right NOW!  Using MySQL, we have a database to store information sent to us.  This information can then be retrieved and analyzed at a later point.

LAMP will also provide us other functions as we expand the project later on, but for now, we are interested only in the MySQL portion of LAMP.  So lets go ahead and install LAMP on our raspberry pi.

Installing Apache, MySQL, and PHP

Open a Terminal and enter each of the following commands

Install Apache

sudo apt-get install apache2 -y

Install MySQL

sudo apt-get install mysql-server -y

***During installation you’ll be prompted to provide a password for the MySQL root account.  Write this password down and do not forget it this is extremely important***

Install PHP

sudo apt-get install php5 -y

Now – open a web browser on the Raspberry Pi and lets make sure that everything installed correctly.

APACHE TEST

Type http://localhost into the browser address bar and press enter.  You should see a page similar to the following image.  This image came from a Raspberry Pi 3 running Ubuntu Linux, but the layout should be the same non-the-less.

 

PHP TEST

Type http://localhost/info.php into the browser address bar and press enter.  You should see a page similar to the following image.

 

MySQL Test

For MySQL, go back into the Terminal and type the following command

sudo service mysql status

If MySQL is active you should see a line with “Active” displayed in green.

If you do not type the following command to start MySQL and then check the status once again.

sudo service mysql start

Congratulations! You just installed your very first webserver on a Raspberry Pi!!!  While you may wonder why we installed more than just MySQL at this point, Apache and PHP will come in handy when we create a reporting system to give us statistics on the returned data from the coop.  The reporting system we will create will be accessible from any computer or device (such as a smart phone) connected to your local network!

Leave a Reply

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