Skip to content

Commit

Permalink
updated waveshare code to latest (07.11.2022)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yattien committed Jan 20, 2023
1 parent 9456bb1 commit b65ad59
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 20
- Do not reset WiFi settings for MAX_CONNECTION_FAILURES, default is one try
- Updated waveshare code to 07.11.2022 (supports 2in13 v3, 2in13b v4, 3in52, 2in7 v2)

## Version v19
- Updated waveshare code to pre-version 19.07.2021 (supports 7in5 v2)
Expand Down
65 changes: 50 additions & 15 deletions ESPEInk_ESP8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ WiFiClient espClient;
PubSubClient mqttClient(espClient);

// -----------------------------------------------------------------------------------------------------
const int FW_VERSION = 19; // for OTA
const int FW_VERSION = 20; // for OTA
// -----------------------------------------------------------------------------------------------------
const char *CONFIG_FILE = "/config.json";
const float TICKS_PER_SECOND = 80000000; // 80 MHz processor
Expand All @@ -60,13 +60,26 @@ void setup() {
// pinMode(LED_BUILTIN, OUTPUT); // won't work, waveshare uses D2 as DC
// digitalWrite(LED_BUILTIN, HIGH);

getConfig();
bool isConfigValid = getConfig();
initMqttClientName();
initAccessPointName();

ctx.initWifiManagerParameters();
bool wifiSetup = setupWifi();
if (wifiSetup) {
// // first check if there is a valid config file
// if (!shouldSaveConfig && !isConfigValid) {
// WiFi.disconnect();
// delay(3000);
//
// WiFiManager wifiManager;
// wifiManager.resetSettings();
// wifiManager.erase(true);
//
// ESP.restart();
// delay(3000);
// }

ctx.updateParameters();
isMqttEnabled = ctx.isMqttEnabled();

Expand All @@ -76,17 +89,14 @@ void setup() {
Serial.printf(" MQTT CommandTopic: %s\r\n", ctx.mqttCommandTopic);
Serial.printf(" sleep time: %ld\r\n", ctx.sleepTime);
Serial.printf(" firmware base URL: %s\r\n", ctx.firmwareUrl);
} else {
Serial.println(" Using configuration:");
ctx.connectionErrorCount++;
}
saveConfig();

if (!wifiSetup) {
ctx.connectionErrorCount++;
if (ctx.connectionErrorCount > MAX_CONNECTION_FAILURES) {
Serial.printf(" Failed to connect %d times, resetting WIFI settings.");
ctx.connectionErrorCount = 0;
saveConfig();

WiFi.disconnect();
delay(3000);
Expand All @@ -109,15 +119,20 @@ void setup() {
}

// -----------------------------------------------------------------------------------------------------
void getConfig() {
bool getConfig() {
bool configRead = true;
if (SPIFFS.begin()) {
if (SPIFFS.exists(CONFIG_FILE)) {
Serial.println(" Reading config file...");
File configFile = SPIFFS.open(CONFIG_FILE, "r");
if (configFile) {
DynamicJsonDocument jsonDocument(512);
DeserializationError error = deserializeJson(jsonDocument, configFile);
size_t size =configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonDocument jsonDocument(1024);
DeserializationError error = deserializeJson(jsonDocument, buf.get());
if (!error) {
Serial.println(" Parsed JSON config.");
strlcpy(ctx.mqttServer, jsonDocument["mqttServer"] | "", sizeof ctx.mqttServer);
ctx.mqttPort = jsonDocument["mqttPort"] | 1883;
strlcpy(ctx.mqttUser, jsonDocument["mqttUser"] | "", sizeof ctx.mqttUser);
Expand All @@ -130,15 +145,25 @@ void getConfig() {
ctx.connectionErrorCount = jsonDocument["connectionErrorCount"] | 0;

Serial.println(" Config file read.");
configFile.close();

} else {
Serial.println(" Failed to load json config.");
Serial.printf(" Failed to load json config: %s\r\n", error.c_str());
configFile.close();
SPIFFS.remove(CONFIG_FILE);
Serial.println(" Config file removed.");
configRead = false;
}
configFile.close();
}
} else {
Serial.println(" Config file not found! Using defaults.");
configRead = false;
}
} else {
Serial.println(" Failed to mount FS (probably initial start), continuing w/o config...");
}

return configRead;
}

// -----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -168,7 +193,7 @@ bool setupWifi() {
if (!connected) {
Serial.println(" Failed to connect.");
} else {
Serial.printf(" Connected to WiFi, got IP address: %s\r\n", WiFi.localIP().toString().c_str());
Serial.printf(" Connected to WiFi, got IP address: %s\r\n", WiFi.localIP().toString().c_str());
}

return connected;
Expand All @@ -190,12 +215,17 @@ void requestMqttParameters(WiFiManager *wifiManager) {
// -----------------------------------------------------------------------------------------------------
void saveConfig() {
if (shouldSaveConfig) {
// if (SPIFFS.begin()) {
// SPIFFS.format();
// Serial.println(" SPIFFS formated.");
// }

Serial.println(" Saving config...");
File configFile = SPIFFS.open(CONFIG_FILE, "w");
if (!configFile) {
Serial.println(" Failed to open config file for writing.");
}
DynamicJsonDocument jsonDocument(512);
DynamicJsonDocument jsonDocument(1024);
jsonDocument["mqttServer"] = ctx.mqttServer;
jsonDocument["mqttPort"] = ctx.mqttPort;
jsonDocument["mqttUser"] = ctx.mqttUser;
Expand All @@ -211,6 +241,9 @@ void saveConfig() {
}
configFile.close();
Serial.println(" Config saved.");

serializeJson(jsonDocument, Serial);
Serial.println();
}
}

Expand Down Expand Up @@ -429,12 +462,14 @@ void callback(char* topic, byte* message, unsigned int length) {

// -----------------------------------------------------------------------------------------------------
void reconnect() {
Serial.println("Connecting to MQTT...");
Serial.printf("Connecting to MQTT: %s:%d...\r\n", ctx.mqttServer, ctx.mqttPort);
while (!mqttClient.connected()) {
// clientID, username, password, willTopic, willQoS, willRetain, willMessage, cleanSession
if (!mqttClient.connect(ctx.mqttClientName, ctx.mqttUser, ctx.mqttPassword, NULL, 0, 0, NULL, 0)) {
delay(1000);
Serial.println(" Connecting failed, try reconnect in 5s.");
delay(5000);
} else {
Serial.println(" Connected.");
boolean rc = mqttClient.subscribe(ctx.mqttUpdateStatusTopic, 1);
if (rc) {
Serial.printf(" Subscribed to %s\r\n", ctx.mqttUpdateStatusTopic);
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Erweiterung des ESP8266-Waveshare-Treibers um Wifi-Einrichtungsassistent, Deepsl
![GitHub issues](https://img.shields.io/github/issues-raw/Yattien/ESPEInk_ESP8266)

# Waveshare-Treiberversion
[19.07.2021](https://www.waveshare.com/wiki/File:E-Paper_ESP8266_Driver_Board_Code.7z)
[07.11.2022](https://www.waveshare.com/wiki/File:E-Paper_ESP8266_Driver_Board_Code.7z)

# Installation
Das fertige Image kann per OTA (siehe auch OTA-Beispiel-Sketche) oder auch per [`esptool`](https://github.com/espressif/esptool) auf den ESP8266 geladen werden.
Expand Down
1 change: 1 addition & 0 deletions data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"mqttServer":"192.168.1.6","mqttPort":1883,"mqttUser":"","mqttPassword":"","mqttClientName":"ESPEInk_ecfabcc5ba9d","mqttUpdateStatusTopic":"stat/doorDisplay/needUpdate","mqttCommandTopic":"cmd/doorDisplay/upload","sleepTime":60,"firmwareUrl":"","connectionErrorCount":0}
9 changes: 7 additions & 2 deletions epd.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ void EPD_Reset()
#include "epd2in7.h"
#include "epd2in66.h"
#include "epd3in7.h"
#include "epd3in52.h"
#include "epd4in01f.h"
#include "epd4in2.h"
#include "epd5in65f.h"
Expand Down Expand Up @@ -297,10 +298,10 @@ void EPD_loadC()
Serial.print("\r\n EPD_loadC");
int index = 0;
String p = server.arg(0);

// Serial.println(p);
// Get the length of the image data begin
int DataLength = p.length() - 8;

EPD_Send_2(0x44, 0, 15); //SET_RAM_X_ADDRESS_START_END_POSITION LO(x >> 3), LO((w - 1) >> 3)
EPD_Send_4(0x45, 0, 0, 249, 0); //SET_RAM_Y_ADDRESS_START_END_POSITION LO(y), HI(y), LO(h - 1), HI(h - 1)

Expand Down Expand Up @@ -584,6 +585,10 @@ EPD_dispInfo EPD_dispMass[] = {
{EPD_Init_5in83_V2, EPD_loadAFilp, -1, 0, EPD_showC, "5.83 inch V2" }, // 36
{EPD_4IN01F_init, EPD_loadG, -1, 0, EPD_4IN01F_Show, "4.01 inch F" }, // 37
{EPD_Init_2in7b_V2, EPD_loadA, 0x26, EPD_loadAFilp, EPD_Show_2in7b_V2, "2.7 inch B V2" }, // 38
{EPD_Init_2in13_V3, EPD_loadC, -1, 0, EPD_2IN13_V3_Show, "2.13 inch V3" }, // 39
{EPD_2IN13B_V4_Init, EPD_loadC, 0x26, EPD_loadC, EPD_2IN13B_V4_Show, "2.13 inch B V4"}, // 40
{ EPD_3IN52_Init, EPD_loadA, -1, 0, EPD_3IN52_Show, "3.52 inch" },// 41
{ EPD_2IN7_V2_Init, EPD_loadA, -1 , 0, EPD_2IN7_V2_Show, "2.7 inch V2" },// 42
};

/* Initialization of an e-Paper ----------------------------------------------*/
Expand Down
146 changes: 132 additions & 14 deletions epd2in13.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @version V1.0.0
* @date 23-January-2018
* @brief This file describes initialisation of e-Papers:
* 2.13,
* 2.13 and 2.13_V3,
* 2.13b and 2.13c,
* 2.13d.
*
Expand Down Expand Up @@ -36,6 +36,28 @@ const unsigned char lut_full_2in3v2[] = {
0x15,0x41,0xA8,0x32,0x30,0x0A,
};

const unsigned char WS_20_30_2IN13_V3[159] ={
0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x80, 0x4A, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x40, 0x4A, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0xF, 0x0, 0x0, 0xF, 0x0, 0x0, 0x2,
0xF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x0, 0x0, 0x0,
0x22, 0x17, 0x41, 0x0, 0x32, 0x36
};

const unsigned char lut_vcomDC_2in13d[] = {
0x00, 0x08, 0x00, 0x00, 0x00, 0x02, 0x60, 0x28, 0x28, 0x00, 0x00,
0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x12, 0x12, 0x00,
Expand Down Expand Up @@ -108,24 +130,83 @@ int EPD_Init_2in13()
EPD_Send_1(0x4E, 0x00);
EPD_Send_2(0x4F, 0xF9, 0x00);

int Width, Height;
Width = (122 % 8 == 0)? (122 / 8 ): (122 / 8 + 1);
Height = 250;
EPD_SendCommand(0x24);
for (int j = 0; j < Height; j++) {
for (int i = 0; i < Width; i++) {
EPD_SendData(0XFF);
}
}
EPD_SendCommand(0x22);
EPD_SendData(0xC7);
EPD_SendCommand(0x20);
while (digitalRead(BUSY_PIN) == 1) delay(100);
int Width, Height;
Width = (122 % 8 == 0)? (122 / 8 ): (122 / 8 + 1);
Height = 250;
EPD_SendCommand(0x24);
for (int j = 0; j < Height; j++) {
for (int i = 0; i < Width; i++) {
EPD_SendData(0XFF);
}
}
EPD_SendCommand(0x22);
EPD_SendData(0xC7);
EPD_SendCommand(0x20);
while (digitalRead(BUSY_PIN) == 1) delay(100);

return 0;
}
}

int EPD_Init_2in13_V3()
{
Serial.print("\r\nEPD_Init_2in13 V3");
EPD_Reset();
delay(100);
while (digitalRead(BUSY_PIN) == 1) delay(10);
EPD_SendCommand(0x12);
while (digitalRead(BUSY_PIN) == 1) delay(10);
EPD_Send_3(0x01, 0XF9, 0X00, 0X00);
EPD_Send_1(0X11, 0X03);
EPD_Send_2(0X44, 0X00, 0X0F);
EPD_Send_4(0x45, 0x00, 0x00, 0x00, 0xF9);
EPD_Send_1(0x4E, 0x00);
EPD_Send_2(0x4F, 0X00, 0X00);
EPD_Send_1(0x3C, 0x05);
EPD_Send_2(0x21, 0x00, 0x80);
EPD_Send_1(0x18, 0x80);

while (digitalRead(BUSY_PIN) == 1) delay(100);
int count;
EPD_SendCommand(0x32);
for(count = 0; count < 153; count++)
EPD_SendData(WS_20_30_2IN13_V3[count]);
EPD_Send_1(0x3f, WS_20_30_2IN13_V3[153]);
EPD_Send_1(0x03, WS_20_30_2IN13_V3[154]);
EPD_Send_3(0x04, WS_20_30_2IN13_V3[155], WS_20_30_2IN13_V3[156], WS_20_30_2IN13_V3[157]);
EPD_Send_1(0x2C, WS_20_30_2IN13_V3[158]);

int Width, Height;
Width = (122 % 8 == 0)? (122 / 8 ): (122 / 8 + 1);
Height = 250;
EPD_SendCommand(0x24);
for (int j = 0; j < Height; j++) {
for (int i = 0; i < Width; i++) {
EPD_SendData(0XFF);
}
}

EPD_SendCommand(0x22);
EPD_SendData(0xC7);
EPD_SendCommand(0x20);
while (digitalRead(BUSY_PIN) == 1) delay(10);
return 0;
}

/* Show image and turn to deep sleep mode ------*/
void EPD_2IN13_V3_Show()
{
Serial.print("\r\n EPD_2IN13_V3_Show");
// Refresh
EPD_Send_1(0x22, 0xC7); //DISPLAY_UPDATE_CONTROL_2
EPD_SendCommand(0x20); //MASTER_ACTIVATION
while (digitalRead(BUSY_PIN) == 1) delay(10);

// Sleep
EPD_Send_1(0x10, 0x01); //DEEP_SLEEP_MODE
EPD_WaitUntilIdle();
}

int EPD_Init_2in13b()
{
EPD_Reset();
Expand Down Expand Up @@ -173,6 +254,43 @@ void EPD_2IN13B_V3_Show()
EPD_Send_1(0X07, 0xa5); //deep sleep
}

int EPD_2IN13B_V4_Init(void)
{
EPD_Reset();
delay(10);

EPD_WaitUntilIdle_high();
EPD_SendCommand(0x12); //SWRESET
EPD_WaitUntilIdle_high();

EPD_Send_3(0x01, 0xf9, 0x00, 0x00); //Driver output control

EPD_Send_1(0x11, 0x03); //data entry mode

EPD_Send_2(0X44, 0X00, 0X0F);
EPD_Send_4(0x45, 0x00, 0x00, 0x00, 0xF9);
EPD_Send_1(0x4E, 0x00);
EPD_Send_2(0x4F, 0X00, 0X00);

EPD_Send_1(0x3C, 0x05); //BorderWavefrom
EPD_Send_1(0x18, 0x80); //Read built-in temperature sensor
EPD_Send_2(0x21, 0x80, 0x80); // Display update control

EPD_WaitUntilIdle_high();

EPD_SendCommand(0x24);

return 0;
}

void EPD_2IN13B_V4_Show()
{
EPD_SendCommand(0x20); //DISPLAY REFRESH
delay(2);
EPD_WaitUntilIdle_high();

EPD_Send_1(0X10, 0x01);
}

int EPD_Init_2in13d()
{
Expand Down
Loading

0 comments on commit b65ad59

Please sign in to comment.