Skip to content

Commit 3e22607

Browse files
authored
Merge pull request letscontrolit#1264 from TD-er/feature/nextion_port_from_playground
Feature/nextion port from playground
2 parents de51a13 + 4de7203 commit 3e22607

File tree

6 files changed

+328
-12
lines changed

6 files changed

+328
-12
lines changed

platformio.ini

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ platform = ${core_2_4_0.platform}
6363
[normal]
6464
platform = ${common.platform}
6565

66-
# Testing and dev take too much memory for using 2.4.x core library.
6766
[testing]
68-
platform = ${core_2_3_0.platform}
67+
platform = ${core_2_4_0.platform}
6968

7069
[dev]
71-
platform = ${core_2_3_0.platform}
70+
platform = ${core_2_4_0.platform}
7271

7372

7473
[esp8266_1M]

src/ESPEasy-Globals.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@
240240
#define CMD_WIFI_DISCONNECT 135
241241

242242
#if defined(PLUGIN_BUILD_TESTING) || defined(PLUGIN_BUILD_DEV)
243-
#define DEVICES_MAX 72
243+
#define DEVICES_MAX 75
244244
#else
245-
#define DEVICES_MAX 64
245+
#define DEVICES_MAX 50
246246
#endif
247247

248248
#if defined(ESP8266)
@@ -902,7 +902,11 @@ struct EventStruct
902902
};
903903

904904
#define LOG_STRUCT_MESSAGE_SIZE 128
905-
#define LOG_STRUCT_MESSAGE_LINES 20
905+
#if defined(PLUGIN_BUILD_TESTING) || defined(PLUGIN_BUILD_DEV)
906+
#define LOG_STRUCT_MESSAGE_LINES 10
907+
#else
908+
#define LOG_STRUCT_MESSAGE_LINES 15
909+
#endif
906910

907911
struct LogStruct {
908912
LogStruct() : write_idx(0), read_idx(0) {

src/ESPEasy.ino

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ void setup()
254254
#endif
255255
addLog(LOG_LEVEL_INFO, log);
256256

257+
if (deviceCount + 1 >= PLUGIN_MAX) {
258+
addLog(LOG_LEVEL_ERROR, F("Programming error! - Increase PLUGIN_MAX"));
259+
}
260+
257261
if (Settings.UseRules)
258262
{
259263
String event = F("System#Wake");

src/ESPEasyWiFiEvent.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
void markGotIP() {
2+
lastGetIPmoment = millis();
3+
wifiStatus = ESPEASY_WIFI_GOT_IP;
4+
processedGetIP = false;
5+
}
6+
17
//********************************************************************************
28
// Functions called on events.
39
// Make sure not to call anything in these functions that result in delay() or yield()
@@ -50,12 +56,6 @@ void WiFiEvent(system_event_id_t event, system_event_info_t info) {
5056
}
5157
#else
5258

53-
void markGotIP() {
54-
lastGetIPmoment = millis();
55-
wifiStatus = ESPEASY_WIFI_GOT_IP;
56-
processedGetIP = false;
57-
}
58-
5959
void onConnected(const WiFiEventStationModeConnected& event){
6060
lastConnectMoment = millis();
6161
processedConnect = false;

src/_P075_Nextion.ino

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
//#######################################################################################################
2+
//################################### Plugin 075: Nextion <[email protected]> ###########################
3+
//################################### Created on the work of majklovec ###########################
4+
//#######################################################################################################
5+
#ifdef USES_P075
6+
7+
#include <ESPeasySoftwareSerial.h>
8+
9+
#define PLUGIN_075
10+
#define PLUGIN_ID_075 75
11+
#define PLUGIN_NAME_075 "Display - Nextion [TESTING]"
12+
#define PLUGIN_VALUENAME1_075 "code"
13+
14+
unsigned long Plugin_075_code = 0;
15+
int8_t Plugin_075_RXpin = -1;
16+
int8_t Plugin_075_TXpin = -1;
17+
18+
#define Nlines 12 // The number of different lines which can be displayed - each line is 32 chars max
19+
20+
ESPeasySoftwareSerial *nextion = NULL;
21+
22+
boolean Plugin_075(byte function, struct EventStruct *event, String &string) {
23+
boolean success = false;
24+
25+
switch (function) {
26+
27+
case PLUGIN_DEVICE_ADD: {
28+
Device[++deviceCount].Number = PLUGIN_ID_075;
29+
30+
Device[deviceCount].Type = DEVICE_TYPE_DUAL;
31+
Device[deviceCount].VType = SENSOR_TYPE_DUAL;
32+
Device[deviceCount].Ports = 0;
33+
Device[deviceCount].PullUpOption = true;
34+
Device[deviceCount].InverseLogicOption = false;
35+
Device[deviceCount].FormulaOption = false;
36+
Device[deviceCount].ValueCount = 2;
37+
Device[deviceCount].SendDataOption = true;
38+
Device[deviceCount].TimerOption = true;
39+
Device[deviceCount].GlobalSyncOption = true;
40+
41+
break;
42+
}
43+
44+
case PLUGIN_GET_DEVICENAME: {
45+
string = F(PLUGIN_NAME_075);
46+
break;
47+
}
48+
49+
case PLUGIN_GET_DEVICEVALUENAMES: {
50+
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0],PSTR(PLUGIN_VALUENAME1_075));
51+
break;
52+
}
53+
54+
case PLUGIN_TEN_PER_SECOND: {
55+
char __buffer[80];
56+
57+
uint16_t i;
58+
uint8_t c;
59+
String Vidx;
60+
String Nvalue;
61+
String Svalue;
62+
String Nswitch;
63+
64+
while (nextion->available() > 0) {
65+
delay(10);
66+
c = nextion->read();
67+
68+
if (0x65 == c) {
69+
if (nextion->available() >= 6) {
70+
__buffer[0] = c;
71+
for (i = 1; i < 7; i++) {
72+
__buffer[i] = nextion->read();
73+
}
74+
75+
__buffer[i] = 0x00;
76+
77+
if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6]) {
78+
Plugin_075_code = ((__buffer[1] >> 8) & __buffer[2] >> 8) & __buffer[3];
79+
UserVar[event->BaseVarIndex] = __buffer[1] * 256 + __buffer[2];
80+
UserVar[event->BaseVarIndex + 1] = __buffer[3];
81+
String log = F("Nextion : code: ");
82+
log += __buffer[1];
83+
log += ",";
84+
log += __buffer[2];
85+
log += ",";
86+
log += __buffer[3];
87+
88+
addLog(LOG_LEVEL_INFO, log);
89+
sendData(event);
90+
}
91+
}
92+
} else {
93+
94+
if (c == '|') {
95+
i = 1;
96+
__buffer[0] = c;
97+
c=0;
98+
while (nextion->available() > 0) {
99+
__buffer[i] = nextion->read();
100+
if (__buffer[i]==0x0d) break;
101+
i++;
102+
}
103+
__buffer[i] = 0x00;
104+
105+
String tmpString = __buffer;
106+
107+
int argIndex = tmpString.indexOf(",i");
108+
int argEnd = tmpString.indexOf(',', argIndex + 1);
109+
if (argIndex)
110+
Vidx = tmpString.substring(argIndex + 2,argEnd);
111+
112+
switch (__buffer[1]){
113+
114+
case 'u':
115+
116+
argIndex = argEnd;
117+
argEnd = tmpString.indexOf(',',argIndex + 1);
118+
if (argIndex)
119+
Nvalue = tmpString.substring(argIndex + 2,argEnd);
120+
121+
argIndex = argEnd;
122+
argEnd = tmpString.indexOf(0x0a);
123+
if (argIndex)
124+
Svalue = tmpString.substring(argIndex + 2,argEnd);
125+
126+
break;
127+
128+
case 's':
129+
130+
argIndex = argEnd;
131+
argEnd = tmpString.indexOf(0x0a);
132+
if (argIndex)
133+
Nvalue = tmpString.substring(argIndex + 2,argEnd);
134+
if (Nvalue=="On")
135+
Svalue='1';
136+
if (Nvalue=="Off")
137+
Svalue='0';
138+
139+
break;
140+
141+
}
142+
143+
UserVar[event->BaseVarIndex] = Vidx.toFloat();
144+
UserVar[event->BaseVarIndex+1] = Svalue.toFloat();
145+
146+
String log = F("Nextion : send command: ");
147+
log += __buffer;
148+
log += UserVar[event->BaseVarIndex];
149+
addLog(LOG_LEVEL_INFO, log);
150+
sendData(event);
151+
152+
ExecuteCommand(VALUE_SOURCE_SYSTEM, __buffer);
153+
}
154+
}
155+
}
156+
157+
success = true;
158+
break;
159+
}
160+
161+
case PLUGIN_ONCE_A_SECOND: {
162+
success = true;
163+
break;
164+
}
165+
166+
167+
case PLUGIN_WEBFORM_SAVE: {
168+
169+
String argName;
170+
171+
char deviceTemplate[Nlines][64];
172+
for (byte varNr = 0; varNr < Nlines; varNr++)
173+
{
174+
String arg = F("Plugin_075_template");
175+
arg += varNr + 1;
176+
String tmpString = WebServer.arg(arg);
177+
strncpy(deviceTemplate[varNr], tmpString.c_str(), sizeof(deviceTemplate[varNr])-1);
178+
deviceTemplate[varNr][63]=0;
179+
180+
// strncpy(deviceTemplate[varNr], WebServer.arg(argName).c_str(), sizeof(deviceTemplate[varNr]));
181+
}
182+
183+
SaveCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
184+
185+
success = true;
186+
break;
187+
}
188+
189+
case PLUGIN_WEBFORM_LOAD: {
190+
char deviceTemplate[Nlines][64];
191+
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
192+
193+
for (byte varNr = 0; varNr < Nlines; varNr++)
194+
{
195+
addFormTextBox(String(F("Line ")) + (varNr + 1), String(F("Plugin_075_template")) + (varNr + 1), deviceTemplate[varNr], 64);
196+
}
197+
198+
success = true;
199+
break;
200+
}
201+
202+
case PLUGIN_INIT: {
203+
204+
LoadTaskSettings(event->TaskIndex);
205+
206+
if (Settings.TaskDevicePin1[event->TaskIndex] != -1)
207+
{
208+
Plugin_075_RXpin = Settings.TaskDevicePin1[event->TaskIndex];
209+
}
210+
211+
if (Settings.TaskDevicePin2[event->TaskIndex] != -1)
212+
{
213+
Plugin_075_TXpin = Settings.TaskDevicePin2[event->TaskIndex];
214+
}
215+
216+
if (!nextion) {
217+
nextion = new ESPeasySoftwareSerial(Plugin_075_RXpin , Plugin_075_TXpin);
218+
}
219+
220+
nextion->begin(9600);
221+
222+
success = true;
223+
break;
224+
}
225+
226+
case PLUGIN_EXIT:
227+
{
228+
if (nextion)
229+
{
230+
delete nextion;
231+
nextion=NULL;
232+
}
233+
break;
234+
}
235+
236+
case PLUGIN_READ: {
237+
char deviceTemplate[Nlines][64];
238+
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
239+
String newString;
240+
241+
for (byte x = 0; x < Nlines; x++) {
242+
String tmpString = deviceTemplate[x];
243+
if (tmpString.length())
244+
{
245+
int rssiIndex = tmpString.indexOf("rssi");
246+
if(rssiIndex >= 0)
247+
{
248+
int barVal;
249+
newString = tmpString.substring(0, rssiIndex);
250+
int nbars = WiFi.RSSI();
251+
if (nbars < -100)
252+
barVal=0;
253+
else if (nbars >= -100 and nbars < -90)
254+
barVal=20;
255+
else if (nbars >= -90 and nbars < -80)
256+
barVal=40;
257+
else if (nbars >= -80 and nbars < -70)
258+
barVal=60;
259+
else if (nbars >= -70 and nbars < -60)
260+
barVal=80;
261+
else if (nbars >= -60)
262+
barVal=100;
263+
264+
newString += String(barVal,DEC);
265+
}
266+
else
267+
{
268+
newString = parseTemplate(tmpString, 0);
269+
}
270+
271+
sendCommand(newString.c_str());
272+
}
273+
}
274+
275+
success = false;
276+
break;
277+
}
278+
279+
280+
case PLUGIN_WRITE: {
281+
String tmpString = string;
282+
int argIndex = tmpString.indexOf(',');
283+
if (argIndex)
284+
tmpString = tmpString.substring(0, argIndex);
285+
if (tmpString.equalsIgnoreCase(F("NEXTION"))) {
286+
argIndex = string.indexOf(',');
287+
tmpString = string.substring(argIndex + 1);
288+
289+
sendCommand(tmpString.c_str());
290+
291+
Serial.println(tmpString);
292+
success = true;
293+
}
294+
break;
295+
}
296+
}
297+
return success;
298+
}
299+
300+
301+
void sendCommand(const char *cmd) {
302+
nextion->print(cmd);
303+
nextion->write(0xff);
304+
nextion->write(0xff);
305+
nextion->write(0xff);
306+
}
307+
308+
#endif // USES_P075

src/define_plugin_sets.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ To create/register a plugin, you have to :
410410
#define USES_P072 // HDC1080
411411
#define USES_P073 // 7DG
412412
#define USES_P074 // TSL2561
413+
#define USES_P075 // Nextion
413414
#endif
414415

415416

0 commit comments

Comments
 (0)