// www.spiketech.be
#include <PxMatrix.h>#include "Arduino.h"#include <ESP8266WiFi.h>#include "EspMQTTClient.h"#include <Adafruit_GFX.h>
//Fonts voor het project#include <Fonts/FreeSerifItalic24pt7b.h>#include <Fonts/kongtext4pt7b.h>#include <Fonts/FreeMonoBold12pt7b.h>#include <Fonts/FreeMono9pt7b.h>#include <Fonts/Picopixel.h>#include <Fonts/TomThumb.h>
// Pins for LED MATRIX#include <Ticker.h>Ticker display_ticker;#define P_LAT 16#define P_A 5#define P_B 4#define P_C 15#define P_D 12#define P_OE 2
#define matrix_width 64#define matrix_height 32
// This defines the 'on' time of the display is us. The larger this number,// the brighter the display. If too large the ESP will crashuint8_t display_draw_time=10; //30-70 is usually fine
//PxMATRIX display(32,16,P_LAT, P_OE,P_A,P_B,P_C);PxMATRIX display(64,32,P_LAT, P_OE,P_A,P_B,P_C,P_D);//PxMATRIX display(64,64,P_LAT, P_OE,P_A,P_B,P_C,P_D,P_E);
// Some standard colorsuint16_t myRED = display.color565(255, 0, 0);uint16_t myGREEN = display.color565(0, 255, 0);uint16_t myBLUE = display.color565(0, 0, 255);uint16_t myWHITE = display.color565(255, 255, 255);uint16_t myYELLOW = display.color565(255, 255, 0);uint16_t myCYAN = display.color565(0, 255, 255);uint16_t myMAGENTA = display.color565(255, 0, 255);uint16_t myBLACK = display.color565(0, 0, 0);
//berichten uit NodeRedconst uint8_t MESG_SIZE = 255;
char curMessage[MESG_SIZE];char newMessage[MESG_SIZE];char curMessage1[MESG_SIZE];char newMessage1[MESG_SIZE];char curMessage2[MESG_SIZE];char newMessage2[MESG_SIZE];char curMessage3[MESG_SIZE];char newMessage3[MESG_SIZE];
bool newMessageAvailable = false;
EspMQTTClient client( "xxxxx", // your SSID "xxx", // Your network pasword "192.168.1.xx", // MQTT Broker server ip "xxxx", // MQTT Broker users Can be omitted if not needed "xxxxx", // MQTT Broker pasword Can be omitted if not needed "P3", // Client name that uniquely identify your device 1883 // The MQTT port, default to 1883. this line can be omitted);
//#ifdef ESP8266// ISR for display refreshvoid display_updater(){ display.display(display_draw_time);}
//mqtt NODE RED DATAvoid onConnectionEstablished(){ client.subscribe("p3/injectie", [](const String & payload) { sprintf(curMessage, payload.c_str()); }); client.subscribe("p3/verbruik", [](const String & payload) { sprintf(curMessage1, payload.c_str()); }); client.subscribe("p3/soc", [](const String & payload) { sprintf(curMessage2, payload.c_str()); }); client.subscribe("p3/battpower", [](const String & payload) { sprintf(curMessage3, payload.c_str()); }); }
void display_update_enable(bool is_enable){#ifdef ESP8266 if (is_enable) display_ticker.attach(0.004, display_updater); else display_ticker.detach();#endif
#ifdef ESP32 if (is_enable) { timer = timerBegin(0, 80, true); timerAttachInterrupt(timer, &display_updater, true); timerAlarmWrite(timer, 4000, true); timerAlarmEnable(timer); } else { timerDetachInterrupt(timer); timerAlarmDisable(timer); }#endif}
void setup() {
Serial.begin(9600); // Define your display layout here, e.g. 1/8 step, and optional SPI pins begin(row_pattern, CLK, MOSI, MISO, SS) display.begin(16); // if your display is not correct change this number //display.begin(8, 14, 13, 12, 4);
//display.setBrightness(50);
curMessage[0] = newMessage[0] = '\0';curMessage1[0] = newMessage1[0] = '\0';curMessage2[0] = newMessage2[0] = '\0';curMessage3[0] = newMessage3[0] = '\0';
//Some message before we receive anything for NodeRed sprintf(curMessage, "Spike");sprintf(curMessage1, "Tech");sprintf(curMessage2, "C");sprintf(curMessage3, "2022");
display.clearDisplay(); display.setTextColor(myCYAN); display.setCursor(8,0); display.setFont(&kongtext4pt7b); display.print(curMessage); display.setTextColor(myMAGENTA); display.setCursor(20,16); display.print(curMessage1); display_update_enable(true);
scroll_text(16,50,"www.SpikeTech.be!",96,96,250); delay(0500);display.clearDisplay();}union single_double{ uint8_t two[2]; uint16_t one;} this_single_double;
void scroll_text(uint8_t ypos, unsigned long scroll_delay, String text, uint8_t colorR, uint8_t colorG, uint8_t colorB){ uint16_t text_length = text.length(); display.setTextWrap(false); // we don't wrap text so it scrolls nicely display.setTextSize(1); display.setRotation(0); display.setTextColor(display.color565(colorR,colorG,colorB));
// Asuming 5 pixel average character width for (int xpos=matrix_width; xpos>-(matrix_width+text_length*5); xpos--) { display.setTextColor(display.color565(colorR,colorG,colorB)); display.clearDisplay(); display.setCursor(xpos,ypos); display.println(text); delay(scroll_delay); yield();
// This might smooth the transition a bit if we go slow // display.setTextColor(display.color565(colorR/4,colorG/4,colorB/4)); // display.setCursor(xpos-1,ypos); // display.println(text);
delay(scroll_delay/5); yield();
}}
uint8_t icon_index=0;
void loop() { client.loop(); Serial.println(curMessage); // injectie Serial.println(curMessage1); // verbruik Serial.println(curMessage2); // soc level batterij Serial.println(curMessage3); // power van batterij Serial.println(" ");
//Some white linesdisplay.drawLine(0,6,64,6,display.color565(200, 200, 200));display.drawLine(31,6,31,23,display.color565(200, 200, 200));display.drawLine(0,23,31,23,display.color565(200, 200, 200)); //bovenste tekst regel linksdisplay.setFont(&TomThumb);//display.setFont(&kongtext4pt7b); display.setTextColor(myYELLOW);display.fillRect(0, 0, 32, 5, display.color565(00, 00, 0));display.setCursor(2, 5); display.print("Elektr");
// regel 2 links
display.setFont(&kongtext4pt7b); display.setTextColor(myGREEN); display.fillRect(0, 8, 31, 7, display.color565(00, 0, 0));display.setCursor(2, 13); display.print(curMessage);
// regel 3 links // display.clearDisplay();
display.setFont(&kongtext4pt7b); display.setTextColor(myRED); display.fillRect(0, 16, 31, 6, display.color565(00, 00, 0));display.setCursor(2, 21); display.print(curMessage1);
// bovenste regel rechtsdisplay.setFont(&TomThumb); display.setTextColor(myCYAN);display.fillRect(32, 0, 32, 5, display.color565(0, 00, 0));display.setCursor(34, 5); display.print("Batt");//2e regel rechtsdisplay.setFont(&TomThumb); display.setTextColor(myBLUE);display.fillRect(40, 8, 32, 5, display.color565(0, 00, 0));display.setCursor(40, 13); display.print("SOC %");
display.setFont(&kongtext4pt7b); display.setTextColor(myCYAN); display.fillRect(32, 16, 32, 6, display.color565(0, 0, 0));display.setCursor(34, 21); display.print(curMessage2);
display.setFont(&kongtext4pt7b); display.setTextColor(myMAGENTA); display.fillRect(28, 25, 37, 6, display.color565(0, 0, 0));display.setCursor(28, 30); display.print(curMessage3);
delay(500);
}