Sending Data to MudPi with MQTT

MudPi has the ability to communicate through multiple protocols. One of the options is MQTT which is a common choice across IoT devices.

You will first need choose a MQTT broker or set one up of your own. If you used the MudPi installer then this would have been installed for you.

Install MQTT Broker

Check if MQTT broker is running with the command:

sudo systemctl status mosquitto

If a broker is not installed you can install it on most linux distros using the following commands:

sudo apt-get install mosquitto mosquitto-clients
sudo systemctl enable mosquitto

Using MQTT Pub/Sub Tools

If you have a broker installed locally you can use the mosquitto_pub and mosquitto_sub commands to interact with MQTT through the terminal.

Publish Data

To publish an event over MQTT containing data you can use the mosquitto_pub command to publish the event. The -t flag is followed by the topic to broadcast on while -m is followed by the data to send. The command below will publish the number 10 on the mudpi/example/topic topic:

mosquitto_pub -h 127.0.0.1 -t "mudpi/example/topic" -m "10"

Subscribe to a Topic

Using the mosquitto_sub command you can subscribe to a topic and listen for data to be emitted. The -t flag is followed by the topic we want to listen to.

mosquitto_sub -h 127.0.0.1 -t "mudpi/example/topic"

Interact with MQTT using Python

You can also publish data over MQTT with Python through the support of paho-mqtt. The script below will show you an example of how to create a connection to a MQTT broker and use a client to publish an event. The data 10 is being sent over the topic mudpi/example/topic in this case.

import paho.mqtt.client as mqtt
broker_address="127.0.0.1"
client = mqtt.Client("SomeUniqueClientName")
client.connect(broker_address)
client.loop_start()
client.publish("mudpi/example/topic","10")
time.sleep(4) # Wait for message to be sent.
client.loop_stop()

Publish Over MQTT with ESP32, Arduino or ESP8266

One of the main uses of MQTT in MudPi is to communicate with various IoT devices. It can be a great way to get data from an Arduino, ESP32, or ESP8266 microcontroller.

A common choice is to use the PubSubClient found here. This library can also be installed into Arduino IDE using the library manager.

Here is an example of a program you could flash to a ESP32 and get a random number from 0-10 broadcast over the mudpi/example/topic topic.

#include <WiFi.h>
//#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "SSID HERE";
const char* password = "YOUR PASSWORD";

//Change to IP address of Pi
const char* mqtt_server = "192.168.1.100";
const char* mqtt_topic_pub = "mudpi/example/topic";

WiFiClient wifi_client;
PubSubClient client(wifi_client);
long last_updated_at = 0;
int value = 0;

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

void setup_wifi() {
  delay(100);
  Serial.println();
  Serial.print("Connecting to network ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Connecting MQTT...");
    // Attempt to connect
    if (client.connect("UniqueClientNameHere")) {
      Serial.println("Success");
    } else {
      Serial.print("Failed, rc=");
      Serial.println(client.state());
      Serial.println(" Retrying on 5s...");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - last_updated_at > 5000) {
    last_updated_at = now;
    
    value = random(10);   
    
    char value_string[8];
    dtostrf(value, 1, 2, value_string);
    Serial.print("Value: ");
    Serial.println(value_string);
    client.publish(mqtt_topic_pub, value_string);
  }
}

If you want to flash this to an ESP8266 then just uncomment the other Wifi library at the top.

MudPi Smart Garden
Getting Started with a Raspberry Pi from Scratch
1 Preparing the SD Card 4:02
2 Booting Up & Configuring Raspbian 4:48
3 SSH and SSH Keys (Video Coming Soon)
4 Updating Python on Raspbian & Installing Useful Packages (Video Coming Soon)