close-circle
Close

ESP8266 MQTT Support :)

A project log for analog.io - A full stack IoT platform

A full stack project dedicated to easily collecting, analyzing and sharing IoT sensor data.

Luke Beno 08/30/2015 at 04:420 Comments

Last night I talked about my adventures with ESP8266. In that project, I used the analog_io_lib to receive RF packets from a sensor node and forward them on to data.sparkfun.com using a http get request. This approach is ok, but I really much prefer MQTT for this sort of transaction. With MQTT, there are just so many more interesting things that I can do with message routing so I started to dig in.

As referenced all over the Interwebs, the goto library for MQTT on Arduino is PubSub. I also found a very useful github gist that did exactly what I was looking for!

So swapping in my WiFi credentials and MQTT broker info, I was able to very quickly put a neat example together. In this example, I have a Sensor node transmitting packets by proprietary RF, these packets are received by the nRF24<->ESP8266 bridge and then forwarded on to MQTT. On the analog.io web application, I created a terminal emulator that subscribes to the same MQTT topic and dumps it to the terminal. Heres a screen grab:

Heres the ESP8266 code:

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <analog_io.h>
#include <SPI.h>

const char* ssid = "...";
const char* password = "...";

char* topic = "99802051261768/stdout";
char* sub_topic = "99802051261768/stdin";
char* server = "lgbeno.analog.io";

analog_io radio(2, 15, 18); // CE, CSN, IRQ pins
const uint8_t rxaddr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x01 };
char inbuf[33];

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("---------msg rx-----------");
  Serial.print("Topic: ");
  Serial.println((char*) topic);
  Serial.print("Payload: ");
  Serial.println((char*) payload);
}


String macToStr(const uint8_t* mac)
{
  String result;
  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5)
      result += ':';
  }
  return result;
}

void setup() {
  Serial.begin(115200);
  delay(10);

  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  
  Serial.println();
  Serial.println();
  Serial.print("connecting to ");
  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());

  // Generate client name based on MAC address and last 8 bits of microsecond counter
  String clientName;
  clientName += "esp8266";
  //uint8_t mac[6];
  //WiFi.macAddress(mac);
  //clientName += macToStr(mac);
  //clientName += "-";
  //clientName += String(micros() & 0xff, 16);

  Serial.print("connecting to ");
  Serial.print(server);
  Serial.print(" as ");
  Serial.println(clientName);
  
  if (client.connect((char*) clientName.c_str())) {
    Serial.println("connected to MQTT broker");
    client.subscribe(sub_topic);
    Serial.print("Topic is: ");
    Serial.println(topic);
    
    if (client.publish(topic, "hello from ESP8266")) {
      Serial.println("Publish ok");
    }
    else {
      Serial.println("Publish failed");
    }
  }
  else {
    Serial.println("MQTT connect failed");
    Serial.println("Will reset and try again...");
    abort();
  }

  radio.begin();  // Defaults 1Mbps, channel 0, max TX power

  radio.setRXaddress((void*)rxaddr);
  
  radio.enableRX();  // Start listening
}

void loop() {
  while (!radio.available(true))
  {
    delay(10);
  }
  if (radio.read(inbuf)) {
    Serial.print("Received packet: ");
    Serial.println(inbuf);
    String payload = inbuf;
      
    if (client.connected()){
      Serial.print("Sending payload: ");
      Serial.println(payload);
      
      if (client.publish(topic, (char*) payload.c_str())) {
        Serial.println("Publish ok");
      }
      else {
        Serial.println("Publish failed");
      }
    }
  }
}

Anyone is welcome to give the terminal a try! Please just be respectful.

Discussions