fab-lab.eu
Published

The MRD - #MQTT REMOTE DISPLAY Based on ePaper

You need a display showing data, pictures and more - but you don't want to wire an extra display to your hardware, or have it remote?

IntermediateFull instructions provided1 hour8,278
The MRD - #MQTT REMOTE DISPLAY Based on ePaper

Things used in this project

Hardware components

Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
×1
Adafruit tri-color e-Paper Feather WIng
×1

Story

Read more

Code

MRD Arduino code

Arduino
please install the needed libs ... eg. Adafruit EPD lib and others ... marked areas to change the code: "// CHANGE HERE >>>>>>>>>" to your needs.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#include <SD.h>
#include "Adafruit_EPD.h"

#ifdef ESP8266
   #define SD_CS    2
   #define SRAM_CS 16
   #define EPD_CS   0
   #define EPD_DC   15
#endif
#ifdef ESP32
  #define SD_CS       14
  #define SRAM_CS     32
  #define EPD_CS      15
  #define EPD_DC      33  
#endif
#define EPD_RESET   -1 // can set to -1 and share with microcontroller Reset!
#define EPD_BUSY    -1 // can set to -1 to not use a pin (will wait a fixed delay)
/* Uncomment the following line if you are using 2.13" tricolor EPD */
Adafruit_IL0373 epd(212, 104 ,EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY);
/* Uncomment the following line if you are using 2.13" monochrome 250*122 EPD */
//Adafruit_SSD1675 epd(250, 122, EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY);

String pax = "";

String matrixausgabe_text  = " "; // Ausgabetext als globale Variable

volatile int matrixausgabe_index = 0;// aktuelle Position in Matrix

//-------------- definition mqtt-object ueber WiFi
WiFiClient   espClient; 
PubSubClient mqttclient(espClient);

//--------- list of mqtt callback functions 
#define MAX_MQTT_SUB 10 // maximal 10 subscriptions erlaubt
typedef void (*mqtthandle) (byte*,unsigned int);
typedef struct {       // Typdeklaration Callback
  String topic;        // mqtt-topic
  mqtthandle fun;      // callback function 
}
subscribe_type; 
subscribe_type mqtt_sub[MAX_MQTT_SUB];
int mqtt_sub_count=0;

String MQTT_Rx_Payload = "" ;
//--------- mqtt callback function 
void mqttcallback(char* to, byte* pay, unsigned int len) {
  String topic   = String(to);
  String payload = String((char*)pay);
  MQTT_Rx_Payload=payload.substring(0,len);
  Serial.println("\ncallback topic:" + topic + ", payload:" + MQTT_Rx_Payload);
  for (int i=0;i<mqtt_sub_count;i++) { // durchsuche alle subscriptions, bis topic passt 
    if (topic==mqtt_sub[i].topic) 
      mqtt_sub[i].fun(pay,len);         // Aufruf der richtigen callback-Funktion
  }
}

//------------ reconnect mqtt-client
void mqttreconnect() { // Loop until we're reconnected 
  if (!mqttclient.connected()) { 
    while (!mqttclient.connected()) { 
      Serial.print("Attempting MQTT connection...");
      // CHANGE HERE >>>>>>>>>
      // connect = <application_name>, <application_name>, <access key of the application> 
      if (mqttclient.connect("pax_test" , "pax_test", "YOURAPPLICATIONACCESSKEY" )) {
        Serial.println("connected");
        for (int i=0;i<mqtt_sub_count;i++) { // subscribe topic
          mqttclient.subscribe(mqtt_sub[i].topic.c_str());
          Serial.println("\nsubscribe");
          Serial.print(mqtt_sub[i].topic);
        }
      } 
      else { 
        Serial.print("failed, rc=");
        Serial.print(mqttclient.state());
        Serial.println(" try again in 5 seconds");
        delay(5000);
      }
    }
  } 
  else { 
    mqttclient.loop(); 
  }
}

void mqtt_callback_topic_pax_test_devices_pax_test1_up_pax(byte* pay, unsigned int len){ // ---------- my callbackfunction mqtt
  String payload = String((char*)pay); // payload als String interpretieren
  MQTT_Rx_Payload=payload.substring(0,len);    // mit Länge von len Zeichen
  Serial.println("\n in callback payload:" + MQTT_Rx_Payload +"len:"+String(len));

  pax = MQTT_Rx_Payload;
  // only do update the e-paper when there is new data
  epd.clearBuffer();
  epd.setCursor(10, 10);
  epd.setTextSize(1);
  epd.setTextColor(EPD_BLACK);
  epd.print("This is the awesome PAXCOUNTER");
  epd.setCursor(50, 70);
  epd.setTextSize(3);
  epd.setTextColor(EPD_RED);
  epd.print(pax);
  epd.display();
}



void setup(){ // Einmalige Initialisierung
  Serial.begin(115200);
  //----------------------------------MQTT-Client 
  // CHANGE HERE >>>>>>>>>
  mqttclient.setServer("eu.thethings.network", 1883);
  mqttclient.setCallback(mqttcallback);

  //--------- prepare mqtt subscription 
  mqtt_sub_count++; // add new element 
  if (mqtt_sub_count < MAX_MQTT_SUB) { 
    // CHANGE HERE >>>>>>>>>
    // topic = <application_name>/deives/<device_name>/up/<value>
    mqtt_sub[mqtt_sub_count-1].topic = "pax_test/devices/pax_test1/up/pax";
    mqtt_sub[mqtt_sub_count-1].fun = mqtt_callback_topic_pax_test_devices_pax_test1_up_pax; //callback function
  } 
  else Serial.println(" err max. mqtt subscription");

  //------------ WLAN initialisieren 
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  delay(100);
  Serial.print ("\nWLAN connect to:");
  // CHANGE HERE >>>>>>>>>
  Serial.print("YOURSSID");
  WiFi.begin("YOURSSID","YOURPWD");
  while (WiFi.status() != WL_CONNECTED) { // Warte bis Verbindung steht 
    delay(500); 
    Serial.print(".");
  };
  Serial.println ("\nconnected, meine IP:"+ WiFi.localIP().toString());
  matrixausgabe_text = " Meine IP:" + WiFi.localIP().toString();
  matrixausgabe_index=0;

  epd.begin();
  epd.setTextWrap(true);
  epd.setTextSize(1);
}

void loop() { // Kontinuierliche Wiederholung 

  mqttreconnect();
  
}

Credits

fab-lab.eu

fab-lab.eu

22 projects • 275 followers
Maker!

Comments