Hi Arduino user,
as my knowledge is limited (to some experiance to python and javascript coding with some raspberry pi projects) i need your help for my new project.
I bought a waveshare E-Paper ESP8266 Driver board (https://www.waveshare.com/wiki/E-Paper_ESP8266_Driver_Board) and a 2,9" b/w/r e-paper Module (https://www.waveshare.com/wiki/2.9inch_e-Paper_Module_(B)).
I read some topics and got close to my target.
I want to connect the ESP8266 via wifi and MQTT to my ioBroker (running on pi3 in the same network).
Here is my problem:
- I write static text on e-paper by display.setFullWindow() - works
- i refresh dynamic data received by mqtt with partial refresh display.setPartialWindow - works partly. This only works, if y position of partial refresh is between 0-31. Every higher value then 31 end in blanking a area on top of the display, full width and 25% height (see pictures)
Pictures: (as new user i only can upload one picture)
Here is my code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define ENABLE_GxEPD2_GFX 0
#include <GxEPD2_3C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW029Z10
const char* ssid = "A";
const char* password = "8";
const char* mqttServer = "192.168.yy.xx";
const int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";
const char* topic1 = "esp/PVaktuelleLeistung";
const char* topic2 = "esp/currentPowerConsumption";
WiFiClient espClient;
PubSubClient client(espClient);
void connectToMQTT() {
if (client.connect("ESP8266Client") ) {
Serial.println("MQTT connected");
}
}
void setup() {
display.init(115200);
helloWorld2();
//helloFullScreenPartialMode();
display.hibernate();
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.publish("esp/test", "Hello from ESP8266");
client.subscribe(topic1);
client.subscribe(topic2);
}
//e-Paper Test
const char HelloWorld[] = "Hello Worls";
void helloWorld()
{
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
int16_t tbx, tby; uint16_t tbw, tbh;
display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
// center the bounding box by transposition of the origin:
uint16_t x = ((display.width() - tbw) / 2) - tbx;
uint16_t y = ((display.height() - tbh) / 2) - tby;
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
display.setCursor(x, y);
display.print(HelloWorld);
}
while (display.nextPage());
}
void helloWorld2()
{
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(GxEPD_BLACK);
int16_t tbx, tby; uint16_t tbw, tbh;
//display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
// center the bounding box by transposition of the origin:
//uint16_t x = ((display.width() - tbw) / 2) - tbx;
//uint16_t y = ((display.height() - tbh) / 2) - tby;
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
//display.setCursor(x, y);
display.setCursor(1, 16);
//display.setTextColor(GxEPD_RED);
display.print("PV-Leistung: ");
//display.setTextColor(GxEPD_BLACK);
display.setCursor(1, 81);
display.print("Verbrauch aktuell: ");
display.setCursor(1, 121);
display.print("aktualisiert: ");
}
while (display.nextPage());
}
void Display_write_partial2(String str_In, uint16_t x, uint16_t y, uint16_t colorText, uint16_t colorBack)
{
int int_Len = str_In.length();
char chr_Text[int_Len];
str_In.toCharArray(chr_Text, int_Len+1);
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
display.setTextColor(colorText);
int16_t tbx, tby; uint16_t tbw, tbh;
display.getTextBounds(chr_Text, x, y, &tbx, &tby, &tbw, &tbh);
// center the bounding box by transposition of the origin:
//uint16_t x = ((display.width() - tbw) / 2) - tbx;
//uint16_t y = ((display.height() - tbh) / 2) - tby;
display.setPartialWindow(x, y, tbw, tbh); //x,y,width,height
Serial.print("x:");
Serial.println(x);
Serial.print("y:");
Serial.println(y);
Serial.print("tbx:");
Serial.println(tbx);
Serial.print("tby:");
Serial.println(tby);
Serial.print("tbw:");
Serial.println(tbw);
Serial.print("tbh:");
Serial.println(tbh);
display.firstPage();
do
{
if (tby >= 0) {
display.setCursor(x, y+tbh);
} else {
display.setCursor(x, tby*(-1));
}
display.fillScreen(colorBack);
display.print(chr_Text);
}
while (display.nextPage());
}
void helloFullScreenPartialMode()
{
//Serial.println("helloFullScreenPartialMode");
const char fullscreen[] = "full screen update";
const char fpm[] = "fast partial mode";
const char spm[] = "slow partial mode";
const char npm[] = "no partial mode";
display.setPartialWindow(0, 0, display.width(), display.height());
display.setRotation(1);
display.setFont(&FreeMonoBold9pt7b);
if (display.epd2.WIDTH < 104) display.setFont(0);
display.setTextColor(GxEPD_BLACK);
const char* updatemode;
if (display.epd2.hasFastPartialUpdate)
{
updatemode = fpm;
}
else if (display.epd2.hasPartialUpdate)
{
updatemode = spm;
}
else
{
updatemode = npm;
}
// do this outside of the loop
int16_t tbx, tby; uint16_t tbw, tbh;
// center update text
display.getTextBounds(fullscreen, 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t utx = ((display.width() - tbw) / 2) - tbx;
uint16_t uty = ((display.height() / 4) - tbh / 2) - tby;
// center update mode
display.getTextBounds(updatemode, 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t umx = ((display.width() - tbw) / 2) - tbx;
uint16_t umy = ((display.height() * 3 / 4) - tbh / 2) - tby;
// center HelloWorld
display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
uint16_t hwx = ((display.width() - tbw) / 2) - tbx;
uint16_t hwy = ((display.height() - tbh) / 2) - tby;
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
display.setCursor(hwx, hwy);
display.print(HelloWorld);
display.setCursor(utx, uty);
display.print(fullscreen);
display.setCursor(umx, umy);
display.print(updatemode);
}
while (display.nextPage());
//Serial.println("helloFullScreenPartialMode done");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
payload[length] = '\0'; // Null terminator used to terminate the char array
String msg = (char*)payload;
msg += " Watt";
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println(); //e-Paper Test
Serial.println(msg); //e-Paper Test
Serial.println();
Serial.println("-----------------------");
if (strcmp(topic, topic1) == 0) {
Serial.println("richtiger Topic");
Display_write_partial2(msg, 131, 0, GxEPD_BLACK, GxEPD_WHITE);//e-Paper Test
//Display_write_partial2(msg, 131, 32, GxEPD_BLACK, GxEPD_WHITE);//e-Paper Test ab y>=32 geht der partial refresh nicht richtig
}
if (strcmp(topic, topic2) == 0) {
//if (strcmp(topic, "esp/currentPowerConsumption") == 0) {
Serial.println("richtiger Topic");
Display_write_partial2(msg, 199, 70, GxEPD_BLACK, GxEPD_WHITE);//e-Paper Test
//Display_write_partial2(msg, 199, 31, GxEPD_BLACK, GxEPD_WHITE);//e-Paper Test
}
}
void loop() {
client.loop();
if (!client.connected()) {
connectToMQTT();
}
}
Maybe someone can help me or give me useful tipps or sources where i can get further informations.
Thanks in advance.
AxLED

