Build a Turbidity Sensor to Monitor and Control Microalgae Biomass Density

Setup MQTT server

MQTT (Message Queuing Telemetry Transport) is a messing protocol based on publish-subscribe method. Device hosting MQTT message in and out is called the broker. Like a real broker, it hosts the exchange between publishers (sellers) and subscribers (buyers). No money changes hands those. There are plenty of tutorials for this.

If you want to know to get start on Raspberry from the beginning, browse and have a look at this one.

Here is the recap:
  1. Raspberry Pi is the most popular device for this. Frist, install MQTT by:
  2. sudo apt-get install mosquitto mosquitto-clients
  3. Test if the MQTT server is working by opening two terminals on Raspberry, one type the first line and the other for the next:
  4. mosquitto_sub -h localhost -t "yourtopic"
    mosquitto_pub -t "yourtopic" -h localost -m "say something"
    First run with MQTT

    You should see the "say something" shows up on the first terminal. Walah! It works.

    The "#" could be use to listen to any topic, replace "yourtopic" by "#"

  5. Now you don't want someone could publish or subscribe to your MQTT server, you should set up a password, doing so by:
  6. cd ~ 
    mosquitto_passwrd -c pwfile mqtt_user
    set up password
  7. mqtt_user can be other names that you like, enter the password twice, and create the .conf file by:
  8. sudo nano /etc/mosquitto/mosquitto.conf
  9. Ddds these two lines to secure the MQTT server:
  10. allow_anonymous false
    password_file /home/pi/pwfile
  11. Then restart the mosquitto by:
  12. sudo systemctl restart mosquitto
  13. Make a test (each line on each terminal) by including the username with the password such as:
  14. mosquitto_sub -h localhost -t "yourtopic" -u "mqtt_user" -P "123456"
    mosquitto_pub -h localhost -t "yourtopic" -u "mqtt_user" -P "123456" -m "is this secured?"
    wrong password

    Refuse connection without the password, yes, that is what we want.

    correct password

    and connected to the topic with a right password.

  15. Also if the MQTT refuses the connection, try this to put the MQTT service to the background of Linux system by:
  16. mosquitto -d
Useful Resources:

I found these references are good to have a look. I learned something from these two reference as I writing this.


The MQTT broker should be ready now, check the status by sudo systemctl status mosquitto. Next, we will prepare a dash terminal to push the message to MQTT broker.