|
| 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 |
0 commit comments