Nextion display plugin

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Allycat1972
Normal user
Posts: 25
Joined: 19 Apr 2016, 11:12

Nextion display plugin

#1 Post by Allycat1972 » 28 Apr 2016, 17:10

Hi guys. Great work as always. I want to add a Nextion touch screen to display Central Heating controls on my ESP, however it sends / receives commands over serial communications. Is there any plan to extend the serial commands to include GPIO control / state, etc? Or can someone provide an additional device plugin to communicate with a Nextion display?

Many thanks! Andy. :D

HarpieC
Normal user
Posts: 22
Joined: 20 Mar 2016, 20:33

Re: Nextion display plugin

#2 Post by HarpieC » 10 May 2016, 08:21

Highly apreciate! As these Displays comes with their own mcu on board the esp part should be very lightweight!
First a bridge between mqtt and the display would be nice.
Chris

Allycat1972
Normal user
Posts: 25
Joined: 19 Apr 2016, 11:12

Re: Nextion display plugin

#3 Post by Allycat1972 » 04 Aug 2016, 12:16

Can I bump this because I would really like to use ESPEasy instead of another software.

Is there an existing addon which can send / receive serial data and process commands like GPIO/12/1, etc

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#4 Post by ledfreak3d » 23 Oct 2016, 13:33

this would be a awesome addition
I can help with testing if needed
would be awesome for simple control and status readout
Domoticz Synology Esp easy Location Netherlands /Hoorn

DonJuanito
New user
Posts: 7
Joined: 16 Mar 2016, 20:01

Re: Nextion display plugin

#5 Post by DonJuanito » 23 Oct 2016, 18:14

Hi. Have a look at this tread: http://www.esp8266.nu/forum/viewtopic.p ... 2net#p9941.

The idea with a Nextion display should be to have a device which can be read from or written to. These read/write would have to be translated to the ESP serial port which is connected to the Nextion display.
It should work...
I don't have time to play with that now, so please, if you succeed, can you post your results? It would be appreciated...

Allycat1972
Normal user
Posts: 25
Joined: 19 Apr 2016, 11:12

Re: Nextion display plugin

#6 Post by Allycat1972 » 02 Nov 2016, 17:33

Thanks, but that only seems to read the serial data. Am I missing something, or is there already a possibility to send serial data? :?:

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#7 Post by BertB » 02 Nov 2016, 18:02

Ser2Net allows for bi-directional communication. You can for instance use it to get serial data from RFLINK gateway into Domoticz, but it requires a client (telnet?) that can read them. Domoticz has a RFLINK gateway with LAN interface module to do that.
Since the Nextion display can act both as an input device (touch screen buttons) as well as an output device with a special protocol, and so far there is no Domoticz module, some crafting on the sofware is needed.

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#8 Post by BertB » 02 Nov 2016, 19:32


User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#9 Post by ledfreak3d » 02 Nov 2016, 19:49

yes I have those boards on scargils site
had 10 of them made
its not realy hard to send commands to nextion
but it would need a standard gui
ive changed the original nextion library so it supports software serial on any posst on the esp8266
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#10 Post by BertB » 02 Nov 2016, 20:37

Right. Now we need some hero to put it all together. I would like to have a plugin that can read data in my Domoticz and present it on the screen and vice versa, transfers button pushes to actions on Domoticz. Stricktly spoken, it is not a true ESPEasy function.

kr0815
Normal user
Posts: 136
Joined: 18 Nov 2015, 18:24

Re: Nextion display plugin

#11 Post by kr0815 » 03 Nov 2016, 00:30

Maybe OffTopic, but why don´t you use a cheap android phone instead a nextion display, price is not much higher, you get it incl. a housing, resolution is better.

I have one running since almost 2 years, using that software: http://netio.davideickhoff.de/de/

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#12 Post by BertB » 03 Nov 2016, 23:42

kr0815 wrote:Maybe OffTopic, but why don´t you use a cheap android phone instead a nextion display, price is not much higher, you get it incl. a housing, resolution is better.

I have one running since almost 2 years, using that software: http://netio.davideickhoff.de/de/
Less fun? :D

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#13 Post by ledfreak3d » 04 Nov 2016, 10:05

true lol

the big question is what kind of info do you want to display on the nextion screen

I would say atleast

the wifI SSID
the ip addres
system time
maybe system resources
sensors that are active
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#14 Post by BertB » 04 Nov 2016, 12:34

Nextion works with components that can be addressed and written to. Example: a text Block named t0 with a text attribute. So, if you enhance oled plugin and swap i2c for serial, or softserial, and add Fields for attribute names, you are on a possible track. At least, that is what I am trying, but I am not a skilled programmeren.
To get An impression try the the Nextion ide.

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#15 Post by ledfreak3d » 04 Nov 2016, 13:03

Yep indeed a own a couple so can test if needed
but would have to agree on a gui layout first
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#16 Post by BertB » 05 Nov 2016, 23:45

Something like this?

Device:
?
Name:
Delay:
Serial Tx Softserial would be nice, otherwise standard serial port.
Serial Rx
Baud Rate: Actually this is always 9600
Data bits:
Page Label IDX Variable
Line 1 : : : :
Line 2 : : : :
Line 3 : : : :
Line 4 : : : :
Line 5 : : : :
Line 6 : : : :
Line 7 : : : :
Line 8 : : : :
Display button:
Display Timeout:

Page represents the page number on the display. 0,1,2, ...
Label represents the name of an object on a page number on the display. t0 etc
IDX represents the device in Domoticz where information can be found.
Variable can hold local variable like Lux: [E7LUX#Lux#R] Lux or [clock#time]

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#17 Post by ledfreak3d » 06 Nov 2016, 15:45

yep I think something like that would be fine
Domoticz Synology Esp easy Location Netherlands /Hoorn

Allycat1972
Normal user
Posts: 25
Joined: 19 Apr 2016, 11:12

Re: Nextion display plugin

#18 Post by Allycat1972 » 06 Nov 2016, 15:58

Hi Everyone.

Really pleased to see this topic getting some hits. As BertB says the Nextion is programmable and you set buttons to do things like send a string to the serial port. It also receives data and changes values / image states, etc. to make the display seem interactive.

Basically what we need is the ESP to receive the data from the serial port and send it to the MQTT server (topic and values can be programmed in the Nextion) The ESP also needs to receive instructions from the MQTT server and send them to the serial port.

On the Nextion I have a value called item called 'TEMP'. You change the value shown on the Nextion by sending an instruction 'TEMP.val=22'
I also have a button on the Nextion called 'FAN'. When it's pressed the Nextion can send an instruction 'publish fanbutton, on'. You can program it to send anything really and it can even do basic if..then..else stuff.

The ESPEasy software has everything, except the serial to MQTT.

Again, great work.

ayasystems
Normal user
Posts: 39
Joined: 09 Aug 2016, 09:45

Re: Nextion display plugin

#19 Post by ayasystems » 06 Nov 2016, 21:05

I'm using Ser2Net to controll my nextion.
Cwh97MJWEAAuwwg.jpg
Cwh97MJWEAAuwwg.jpg (111.07 KiB) Viewed 306089 times
Domoticz send data to ESP using a python script.

Works really fine!!!

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#20 Post by ledfreak3d » 06 Nov 2016, 21:16

that looks very nice
would you we willing to provide some files and info on how to do this
then we can build from there
the nextions are realy awesome lots we can do with them
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#21 Post by BertB » 06 Nov 2016, 21:41

Looks great. How did you do it?

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#22 Post by BertB » 06 Nov 2016, 21:58

I made quick and probably very dirty combination of ser2net and OLED plugin.
I can now send system variables to the display, using strings like :page0.t0.txt="%systime%" and page0.g0.txt="%ip%"

ayasystems
Normal user
Posts: 39
Joined: 09 Aug 2016, 09:45

Re: Nextion display plugin

#23 Post by ayasystems » 06 Nov 2016, 22:20

ESP8266 as Ser2Net server...

Domoticz call to os command with the new data from lua:


Code: Select all


local function nextion(cmd,value)
   if(value=="") then
     comando = "/home/odroid/scripts/sentSer2Net.py "..cmd
   else
     comando = "/home/odroid/scripts/sentSer2Net.py "..cmd.." "..value
   end
   --print(comando)
   os.execute(comando)
end

Then my device call the function

Example my wh meter (into lua script

Code: Select all

if devicechanged['PZEM'] then
    --print(devicechanged['PZEM'])
    power,total = otherdevices_svalues['PZEM']:match("([^;]+);([^;]+)")
    nextion("tpower.val="..power,"")
end
Example my room themperature sensor

Code: Select all

if devicechanged['EntradaTemp'] then
--    print('EntradaTemp')
    d1,d2,d3=otherdevices_svalues['EntradaTemp']:match("([^;]+);([^;]+);([^]+);([^;]+)")
    nextion("tentrada.txt",d1)
    nextion("hentrada.txt",d2)
end

the python script:

Code: Select all

#!/usr/bin/env python
import socket
import sys

TCP_IP = '192.168.3.221'#ESP8266 IP
TCP_PORT = 8080#SER2NET PORT
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect((TCP_IP, TCP_PORT))
except socket.error, msg:
    sys.exit(1)
COMMAND = sys.argv[1]
if len(sys.argv) > 2:
    VALUE   = sys.argv[2]
    s.send(COMMAND)
    s.send("=\"")
    s.send(VALUE)
    s.send("\"")
    s.send(b'\xFF\xFF\xFF')
    s.close()
else:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((TCP_IP, TCP_PORT))
    s.send(COMMAND)
    s.send(b'\xFF\xFF\xFF')
    s.close()
Let my know if you have any dubt

Regards!

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#24 Post by BertB » 06 Nov 2016, 22:26

Thank you.
I will study that, but I admit that I do not at all understand that lua gibberish ;)

majklovec
Normal user
Posts: 10
Joined: 12 Jul 2016, 07:47

Re: Nextion display plugin

#25 Post by majklovec » 09 Nov 2016, 02:40

Dirty, easy, working.

Enjoy

Code: Select all

#include <Arduino.h>
#include <SoftwareSerial.h>

//#######################################################################################################
//#################################### Plugin 104: Nextion <info@sensorio.cz>
//###########################
//#######################################################################################################

#define PLUGIN_104
#define PLUGIN_ID_104 104
#define PLUGIN_NAME_104 "Display: Nextion"
#define PLUGIN_VALUENAME1_104 "code"
unsigned long Plugin_104_code = 0;
SoftwareSerial *nextion;

boolean Plugin_104(byte function, struct EventStruct *event, String &string) {
  boolean success = false;

  switch (function) {

    case PLUGIN_DEVICE_ADD: {
      Device[++deviceCount].Number = PLUGIN_ID_104;

      Device[deviceCount].Type = DEVICE_TYPE_DUAL;
      Device[deviceCount].VType = SENSOR_TYPE_LONG;
      Device[deviceCount].Ports = 0;
      Device[deviceCount].PullUpOption = false;
      Device[deviceCount].InverseLogicOption = false;
      Device[deviceCount].FormulaOption = false;
      Device[deviceCount].ValueCount = 1;
      Device[deviceCount].SendDataOption = true;
      Device[deviceCount].TimerOption = false;
      Device[deviceCount].GlobalSyncOption = true;

      break;
    }

    case PLUGIN_GET_DEVICENAME: {
      string = F(PLUGIN_NAME_104);
      break;
    }

    case PLUGIN_GET_DEVICEVALUENAMES: {
      strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0],
               PSTR(PLUGIN_VALUENAME1_104));
      break;
    }
    case PLUGIN_TEN_PER_SECOND: {
      char __buffer[80];

      uint16_t i;
      uint8_t c;

      while (nextion->available() > 0) {
        delay(10);
        c = nextion->read();

        if (0x65 == c) {
          if (nextion->available() >= 6) {
            __buffer[0] = c;
            for (i = 1; i < 7; i++) {
              __buffer[i] = nextion->read();
            }

            __buffer[i] = 0x00;

            if (0xFF == __buffer[4] && 0xFF == __buffer[5] &&
                0xFF == __buffer[6]) {
              Plugin_104_code =
                  ((__buffer[1] >> 8) & __buffer[2] >> 8) & __buffer[3];
              UserVar[event->BaseVarIndex] = __buffer[1] * 256 + __buffer[2];
              UserVar[event->BaseVarIndex + 1] = __buffer[3];
              String log = F("Nextion : code: ");
              log += __buffer[1];
              log += ",";
              log += __buffer[2];
              log += ",";
              log += __buffer[3];

              addLog(LOG_LEVEL_INFO, log);
              sendData(event);
            }
          }
        } else {

          if (c == 's') {
            i = 1;
            __buffer[0] = c;
            while (nextion->available() > 0) {
              __buffer[i++] = nextion->read();
              Serial.write((char)__buffer[i]);
            }
            Serial.println();
            __buffer[i] = 0x00;

            String log = F("Nextion : send command: ");
            log += __buffer;
            addLog(LOG_LEVEL_INFO, log);

            ExecuteCommand(VALUE_SOURCE_SYSTEM, __buffer);
          }
        }
      }

      success = true;
      break;
    }

    case PLUGIN_WEBFORM_LOAD: {
      char tmpString[128];
      sprintf_P(tmpString, PSTR("<TR><TD>RX:<TD><input type='text' "
                                "name='taskdevicepin1' value='%u'>"),
                Settings.TaskDevicePin1[event->TaskIndex]);
      string += tmpString;
      sprintf_P(tmpString, PSTR("<TR><TD>TX:<TD><input type='text' "
                                "name='taskdevicepin2' value='%u'>"),
                Settings.TaskDevicePin2[event->TaskIndex]);
      string += tmpString;
      success = true;
      break;
    }

    case PLUGIN_INIT: {
      LoadTaskSettings(event->TaskIndex);
      if (!nextion) {
        nextion = new SoftwareSerial(Settings.TaskDevicePin1[event->TaskIndex],
                                     Settings.TaskDevicePin2[event->TaskIndex]);
      }

      Serial.print("pin: ");
      Serial.println(Settings.TaskDevicePin1[event->TaskIndex]);

      nextion->begin(9600);

      success = true;
      break;
    }
    case PLUGIN_WRITE: {
      String tmpString = string;
      int argIndex = tmpString.indexOf(',');
      if (argIndex)
        tmpString = tmpString.substring(0, argIndex);
      if (tmpString.equalsIgnoreCase(F("NEXTION"))) {
        argIndex = string.indexOf(',');
        tmpString = string.substring(argIndex + 1);

        sendCommand(tmpString.c_str());

        Serial.println(tmpString);
        success = true;
      }
      break;
    }
  }
  return success;
}

void sendCommand(const char *cmd) {
  nextion->print(cmd);
  nextion->write(0xff);
  nextion->write(0xff);
  nextion->write(0xff);
}


User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#26 Post by ledfreak3d » 09 Nov 2016, 08:30

ill give it a go when I am home tonight se what we an do / improve on it

thanks for the work done majklove
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#27 Post by BertB » 09 Nov 2016, 11:02

@majklovec. Looks great and it compiles well and even seems to live, but what does it exactly do?

With serial log level 4 I get:
ADC : Analog value: 0
EVENT: Anain#Analog=0.00
HTTP : connecting to 192.168.0.243 using port 8080
/json.htm?type=command&param=udevice&idx=1&svalue=0.00
HTTP/1.1 200 OK

HTTP : Success
Content-Length: 24

Content-Type: application/json;charset=UTF-8

Cache-Control: no-cache

Pragma: no-cache

Access-Control-Allow-Origin: *



{
"status" : "ERR"
}
HTTP : closing connection
EVENT: Clock#Time=Wed,09:54
UDP : 18:fe:34:f2:01:4e,192.168.0.70,10
UDP : 18:fe:34:f2:01:7b,192.168.0.69,9
UDP : 18:fe:34:9e:74:46,192.168.0.61,1

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#28 Post by BertB » 09 Nov 2016, 11:04

When I create a button in Domoticz and I put http://192.168.0.72/control?cmd=NEXTION,t0.txt="hoi" in the OnAction, I get the string in the display in textbox t0.
And when a send http://192.168.0.72/control?cmd=NEXTION,z0.val=10, I see a gauge item move to value 10
Great!

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#29 Post by ledfreak3d » 09 Nov 2016, 19:38

yep plugin works fine
now to see how we can get something usefull on there ;)
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#30 Post by BertB » 09 Nov 2016, 20:55

ledfreak3d wrote:yep plugin works fine
now to see how we can get something usefull on there ;)
Do you also get this error I mentioned above?

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#31 Post by ledfreak3d » 09 Nov 2016, 21:17

nope no error
ive created a quick hmi file for testing and I can send value's and text to the corresponding test and value fields
Domoticz Synology Esp easy Location Netherlands /Hoorn

majklovec
Normal user
Posts: 10
Joined: 12 Jul 2016, 07:47

Re: Nextion display plugin

#32 Post by majklovec » 09 Nov 2016, 21:23

Bert, yeah that's it.

It works even the other way around. 2 options are available:

1) Try to make a button in the Nextion and check on "Touch release event" - "Send component id"
See the event sent.

2) Or do not check "Send component id" and use printto send whatever event back to ESPEasy.

Only send command is supported (actually all starting with 's' as seen in the code)

Code: Select all

print "sendTo 2,event,givemesomewater"
Via print you are able to send even the variables (choose String type), for example - RGB sliders, by concating them and printing aftewhile.

Code: Select all

va0.txt=va0.txt+"g"
print va0.txt
For converting numeric values to string use Nextion function cov:

Code: Select all

cov va0.txt,j0.val,0
So this way you are able to control actuators from Nextion.

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#33 Post by ledfreak3d » 09 Nov 2016, 21:54

thanks for the explanation majklovec

any idea how we can display esp easy stats like ip addres system time
or a sensor value from espeasy ?

is there a way to assign a virtual button on the nextion to a virtual / dummy switch on esp easy or in domoticz itself
Domoticz Synology Esp easy Location Netherlands /Hoorn

majklovec
Normal user
Posts: 10
Joined: 12 Jul 2016, 07:47

Re: Nextion display plugin

#34 Post by majklovec » 09 Nov 2016, 22:03

@ledfreak3d

Just use rules and send the command to nextion:

Code: Select all

On UNIT01#TEMPERATURE do
     NEXTION,z0.val=[UNIT01#TEMPERATURE]
endon
I believe the same way %internal variables% can be sent.

I don't know domoticz, so I can't serve.

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#35 Post by ledfreak3d » 10 Nov 2016, 08:36

ahh yea that makes sense

ill see if I can build a test gui today and send over some data to the display that are usefull
my mane intererest would be sending data to domoticz to get it to do stuff
but I am sure we will find a way for that to

will post the results
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#36 Post by BertB » 10 Nov 2016, 09:07

I would also like te possibility to display data like Oled Does.

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#37 Post by ledfreak3d » 10 Nov 2016, 11:13

humz been testing this morning via rules
but have not been able to get values displayed yet
can send command via http command line but the same command via rules does nothing
Domoticz Synology Esp easy Location Netherlands /Hoorn

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Nextion display plugin

#38 Post by papperone » 10 Nov 2016, 11:41

I'm goign to order a Nextion to experiment and probaly wall mounting it as GUI for my home automation.
I'm just puzzled which size to order, 3.5" and 4.2" seems ok size-wise but really expensive, specially if you go to enhanced version.
Which one you owns/use (size and normal or enhance) and what are your advise??
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#39 Post by ledfreak3d » 10 Nov 2016, 11:56

I own a 2.4 and a 3,5 inch the basic versions

work perfectly for most things
Domoticz Synology Esp easy Location Netherlands /Hoorn

ayasystems
Normal user
Posts: 39
Joined: 09 Aug 2016, 09:45

Re: Nextion display plugin

#40 Post by ayasystems » 10 Nov 2016, 12:28

I think that tcp could not be the best way because if esp8266 is not alive it will raise a time out on each comunication and error...

The best way cloud be use udp to send the data to espeasy or use esp to read and parse json data

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#41 Post by ledfreak3d » 11 Nov 2016, 08:15

no results yet but working on it
Domoticz Synology Esp easy Location Netherlands /Hoorn

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#42 Post by BertB » 12 Nov 2016, 13:03

I did some ehm pick and mix and drag an drop on the code from Majklovec and OLED plugin and now it reads up to 8 lines of text from the plugin.

Code: Select all

#include <Arduino.h>
#include <SoftwareSerial.h>

//#######################################################################################################
//#################################### Plugin 104: Nextion <info@sensorio.cz>
//###########################
//#######################################################################################################

#define PLUGIN_104
#define PLUGIN_ID_104 104
#define PLUGIN_NAME_104 "Display: Nextion"
#define PLUGIN_VALUENAME1_104 "code"
unsigned long Plugin_104_code = 0;
SoftwareSerial *nextion;

boolean Plugin_104(byte function, struct EventStruct *event, String &string) {
  boolean success = false;
  static byte displayTimer = 0;

  switch (function) {

    case PLUGIN_DEVICE_ADD: {
      Device[++deviceCount].Number = PLUGIN_ID_104;

      Device[deviceCount].Type = DEVICE_TYPE_DUAL;
      Device[deviceCount].VType = SENSOR_TYPE_LONG;
      Device[deviceCount].Ports = 0;
      Device[deviceCount].PullUpOption = false;
      Device[deviceCount].InverseLogicOption = false;
      Device[deviceCount].FormulaOption = false;
      Device[deviceCount].ValueCount = 1;
      Device[deviceCount].SendDataOption = true;
      Device[deviceCount].TimerOption = true;
      Device[deviceCount].GlobalSyncOption = true;

      break;
    }

    case PLUGIN_GET_DEVICENAME: {
      string = F(PLUGIN_NAME_104);
      break;
    }

    case PLUGIN_GET_DEVICEVALUENAMES: {
      strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0],
               PSTR(PLUGIN_VALUENAME1_104));
      break;
    }
    
    case PLUGIN_TEN_PER_SECOND: {
      char __buffer[80];

      uint16_t i;
      uint8_t c;

      while (nextion->available() > 0) {
        delay(10);
        c = nextion->read();

        if (0x65 == c) {
          if (nextion->available() >= 6) {
            __buffer[0] = c;
            for (i = 1; i < 7; i++) {
              __buffer[i] = nextion->read();
            }

            __buffer[i] = 0x00;

            if (0xFF == __buffer[4] && 0xFF == __buffer[5] &&
                0xFF == __buffer[6]) {
              Plugin_104_code =
                  ((__buffer[1] >> 8) & __buffer[2] >> 8) & __buffer[3];
              UserVar[event->BaseVarIndex] = __buffer[1] * 256 + __buffer[2];
              UserVar[event->BaseVarIndex + 1] = __buffer[3];
              String log = F("Nextion : code: ");
              log += __buffer[1];
              log += ",";
              log += __buffer[2];
              log += ",";
              log += __buffer[3];

              addLog(LOG_LEVEL_INFO, log);
              sendData(event);
            }
          }
        } else {

          if (c == 's') {
            i = 1;
            __buffer[0] = c;
            while (nextion->available() > 0) {
              __buffer[i++] = nextion->read();
              Serial.write((char)__buffer[i]);
            }
            Serial.println();
            __buffer[i] = 0x00;

            String log = F("Nextion : send command: ");
            log += __buffer;
            addLog(LOG_LEVEL_INFO, log);

            ExecuteCommand(VALUE_SOURCE_SYSTEM, __buffer);
          }
        }
      }

      if (Settings.TaskDevicePin3[event->TaskIndex] != -1) {
        if (!digitalRead(Settings.TaskDevicePin3[event->TaskIndex])) {
          Plugin_104_displayOn();
          displayTimer = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
        }
      }

      success = true;
      break;
    }

    case PLUGIN_ONCE_A_SECOND: {
      if ( displayTimer > 0) {
        displayTimer--;
        if (displayTimer == 0)
          Plugin_104_displayOff();
        }
        break;
      }


    case PLUGIN_WEBFORM_SAVE: {
      String plugin1 = WebServer.arg("plugin_104_timer");
      Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
      String plugin2 = WebServer.arg("plugin_104_rxwait");
      Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin2.toInt();
      String plugin3 = WebServer.arg("plugin_104_events");
      Settings.TaskDevicePluginConfig[event->TaskIndex][2] = plugin3.toInt();

      char deviceTemplate[8][64];
      for (byte varNr = 0; varNr < 8; varNr++) {
        char argc[25];
        String arg = F("Plugin_104_template");
        arg += varNr + 1;
        arg.toCharArray(argc, 25);
        String tmpString = WebServer.arg(argc);
        strncpy(deviceTemplate[varNr], tmpString.c_str(), sizeof(deviceTemplate[varNr]));
      }

//      Settings.TaskDeviceID[event->TaskIndex] = 1; // temp fix, needs a dummy value

      SaveCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
    
      success = true;
      break;
    }


    case PLUGIN_WEBFORM_LOAD: {
      char tmpString[128];
      sprintf_P(tmpString, PSTR("<TR><TD>RX:<TD><input type='text' "
                                "name='taskdevicepin1' value='%u'>"),
                Settings.TaskDevicePin1[event->TaskIndex]);
      string += tmpString;
      sprintf_P(tmpString, PSTR("<TR><TD>TX:<TD><input type='text' "
                                "name='taskdevicepin2' value='%u'>"),
                Settings.TaskDevicePin2[event->TaskIndex]);
      string += tmpString;

      char deviceTemplate[8][64];
      LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
      for (byte varNr = 0; varNr < 8; varNr++)
        {
          string += F("<TR><TD>Line ");
          string += varNr + 1;
          string += F(":<TD><input type='text'li size='50' maxlength='50' name='Plugin_104_template");
          string += varNr + 1;
          string += F("' value='");
          string += deviceTemplate[varNr];
          string += F("'>");
        }

      string += F("<TR><TD>Display button:<TD>");
      addPinSelect(false, string, "taskdevicepin3", Settings.TaskDevicePin3[event->TaskIndex]);

      sprintf_P(tmpString, PSTR("<TR><TD>Display Timeout:<TD><input type='text' name='plugin_104_timer' value='%u'>"), Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
      string += tmpString;

      success = true;
      break;
    }

    case PLUGIN_INIT: {

      LoadTaskSettings(event->TaskIndex);
//      if ((ExtraTaskSettings.TaskDevicePluginConfigLong[0] != 0) && (ExtraTaskSettings.TaskDevicePluginConfigLong[1] != 0)) {
      if (Settings.TaskDevicePin1[event->TaskIndex] != -1) {
        pinMode(Settings.TaskDevicePin1[event->TaskIndex], OUTPUT);
        digitalWrite(Settings.TaskDevicePin1[event->TaskIndex], LOW);
        delay(500);
        digitalWrite(Settings.TaskDevicePin1[event->TaskIndex], HIGH);
        pinMode(Settings.TaskDevicePin1[event->TaskIndex], INPUT_PULLUP);
      }

      displayTimer = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
      if (Settings.TaskDevicePin3[event->TaskIndex] != -1)
          pinMode(Settings.TaskDevicePin3[event->TaskIndex], INPUT_PULLUP);


      
      if (!nextion) {
        nextion = new SoftwareSerial(Settings.TaskDevicePin1[event->TaskIndex],
                                     Settings.TaskDevicePin2[event->TaskIndex]);
      }

      Serial.print("pin: ");
      Serial.println(Settings.TaskDevicePin1[event->TaskIndex]);

      nextion->begin(9600);

      success = true;
      break;
    }

    case PLUGIN_READ: {
        char deviceTemplate[8][64];
        LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));

        for (byte x = 0; x < 8; x++) {
          String tmpString = deviceTemplate[x];
          if (tmpString.length()) {
            String newString = parseTemplate(tmpString, 16);
            Serial.println(newString);

            sendCommand(newString.c_str());
            }
        }
        success = false;
        break;
      }


    case PLUGIN_WRITE: {
      String tmpString = string;
      int argIndex = tmpString.indexOf(',');
      if (argIndex)
        tmpString = tmpString.substring(0, argIndex);
      if (tmpString.equalsIgnoreCase(F("NEXTION"))) {
        argIndex = string.indexOf(',');
        tmpString = string.substring(argIndex + 1);

        sendCommand(tmpString.c_str());

        Serial.println(tmpString);
        success = true;
      }
      break;
    }
  }
  return success;
}




void Plugin_104_displayOn() {
}

void Plugin_104_displayOff() {
  
}

void sendCommand(const char *cmd) {
  nextion->print(cmd);
  nextion->write(0xff);
  nextion->write(0xff);
  nextion->write(0xff);
}

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#43 Post by BertB » 12 Nov 2016, 13:08

I also created a screen for the display itself.
setting up Nextion
setting up Nextion
Winscreen.png (54.22 KiB) Viewed 308684 times

BertB
Normal user
Posts: 1049
Joined: 25 Apr 2015, 14:39

Re: Nextion display plugin

#44 Post by BertB » 13 Nov 2016, 16:37

majklovec wrote:Bert, yeah that's it.

It works even the other way around. 2 options are available:

1) Try to make a button in the Nextion and check on "Touch release event" - "Send component id"
See the event sent.

2) Or do not check "Send component id" and use printto send whatever event back to ESPEasy.

Only send command is supported (actually all starting with 's' as seen in the code)

Code: Select all

print "sendTo 2,event,givemesomewater"
Via print you are able to send even the variables (choose String type), for example - RGB sliders, by concating them and printing aftewhile.

Code: Select all

va0.txt=va0.txt+"g"
print va0.txt
For converting numeric values to string use Nextion function cov:

Code: Select all

cov va0.txt,j0.val,0
So this way you are able to control actuators from Nextion.
I still do not understand how it works.
I can use the Send comonent id checkbox, but makes the ESP to send a large number to the Domoticz device.
But how can I make the ESP to send for instance a switch command?
When I create a button on Nextion with the command print "SendToHTTP 192.168.0.243,8080,/json.htm?type=command&param=switchlight&idx=174&switchcmd=On" nothing happens and most of the time the string is corrupted.

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#45 Post by ledfreak3d » 14 Nov 2016, 13:21

Plugin seems to work fine
now trying to see what variables work
see picture attached
Attachments
20161114_131702_resized.jpg
20161114_131702_resized.jpg (44.26 KiB) Viewed 308639 times
Domoticz Synology Esp easy Location Netherlands /Hoorn

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Nextion display plugin

#46 Post by xbmcnut » 17 Nov 2016, 04:10

ledfreak3d wrote:Plugin seems to work fine
now trying to see what variables work
see picture attached
What ESP board are you using in the photo?
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#47 Post by ledfreak3d » 17 Nov 2016, 07:00

Its a replica of scargils esp board had some made in black very handy for the nextions and any esp project
Domoticz Synology Esp easy Location Netherlands /Hoorn

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Nextion display plugin

#48 Post by xbmcnut » 17 Nov 2016, 07:52

ledfreak3d wrote:Its a replica of scargils esp board had some made in black very handy for the nextions and any esp project
Awesome. Can you suggest a source? I'd love to try some of these.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

User avatar
ledfreak3d
Normal user
Posts: 48
Joined: 21 Oct 2016, 12:41

Re: Nextion display plugin

#49 Post by ledfreak3d » 17 Nov 2016, 08:20

Hey Depends if you want bare boards or
boards that have been populated ;)
Domoticz Synology Esp easy Location Netherlands /Hoorn

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Nextion display plugin

#50 Post by xbmcnut » 17 Nov 2016, 08:25

ledfreak3d wrote:Hey Depends if you want bare boards or
boards that have been populated ;)
I can solder, no issue there I just don't have the time these days. Is a populated board available?
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests