owned this note changed 3 years ago
Linked with GitHub

MQTT

MQTT was developed by Andy Stanford-Clark (IBM) and Arlen Nipper (Eurotech; now Cirrus Link) in 1999 for the monitoring of an oil pipeline through the desert.

The goals were to have a protocol, which is bandwidth-efficient and uses little battery power, because the devices were connected via satellite link and this was extremely expensive at that time.

The protocol uses a publish/subscribe architecture wich is event-driven and enables messages to be pushed to clients. The central communication point is the MQTT broker, it is in charge of dispatching all messages between the senders and the rightful receivers. Each client that publishes a message to the broker, includes a topic into the message.

The topic is the routing information for the broker. Each client that wants to receive messages subscribes to a certain topic and the broker delivers all messages with the matching topic to the client. Therefore the clients don’t have to know each other, they only communicate over the topic.

This architecture enables highly scalable solutions without dependencies between the data producers and the data consumers.
source

A topic is a simple string defined by the user that can have more hierarchy levels, which are separated by a slash.

mdef/input/team1/temperature
mdef/ouput/team2/motor

Wilcards can also be used in sigle leves ej. mdef/input/+/temperature will return temperatures of all teams.
Or in multilevels: mdef/output/# will return all outputs from all teams.

MQTT on Arduino IDE

  • Install PubSubClient (MQTT) libray:
    Open the Library manager in Arduino menu Sketch -> Include Library -> Manage Libraries and search for the PubSubClient library, install it.

You can find the full API documentation for the PubSubClient library here

For the first test you can load MQTT_esp8266 example from the library examples. In File→Examples→PubSubClient menu.

WiFi setup

Change WiFi settings and mqtt_server address.

const char* ssid = "Iaac-Wifi";
const char* password = "EnterIaac22@";
const char* mqtt_server = "mqtt.fablabbcn.org";

Receiving data

If you are suscribing to a topic (for actuator nodes) change subscription topic name on line 94. And modify the callback function around line 70 to control your actuator.

 if ((char)payload[0] == '1') {
    tone(D5, 1000);
  } else {
    noTone(D5);
  }

Publishing data

If you are publishing data (sensor nodes), modify the code in the loop function (after line 110) to get your sensor reading and publish it, also remember t.

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  unsigned long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;

    // read your sensor here
    value = analogRead(A0);
    
    snprintf (msg, MSG_BUFFER_SIZE, "%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);

    // Change your publishing topic here
    client.publish("outTopic", msg);
  }
}
Select a repo