This is probably a question for @PerryBebbington - but I don't want to keep annoying just him.
I've been using his method for serial communication between an ESP32 and a Nextion successfully, though I just started a new project and tried doing the same thing and for some reason my Arduino isn't doing anything with the serial communications.
I checked the Nextion programmer to see that the serial communications were being sent out, they're fine. Tried switching my RX/TX wires around etc, just to rule out something stupid, but I'm thinking it's to do with the ESP32 or my programming.
Hoping someone can take a quick look at what I've got (so far) and see if there's any blatant issues:
Hardware: ESP32 WROOM-32U (devkit C), Nextion NX8048K050_011
Code:
//#include <Arduino.h>
#include "TickTwo.h"
/* ESP32 GPIO */
#include <soc/soc.h>
#include <soc/rtc_cntl_reg.h>
#include <driver/gpio.h>
/* ********** */
/* FILESYSTEM */
#include <vfs_api.h>
#include <FS.h>
#include <FSImpl.h>
#include <SPIFFS.h>
/* ********** */
/* ESP32 WIFI/HTTP *
#include <esp_pm.h>
#include <esp_wifi.h>
#include <esp_wifi_types.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include <ESPNexUpload.h>
* *************** */
//WiFiMulti WiFiMulti;
/* TEMP/HUM SENSOR - DHT11 */
#include <DHTesp.h>
/* *********************** */
#define NEXT_RX 23 // Nextion RX pin
#define NEXT_TX 13 // Nextion TX pin
/* SENSOR DEFS - DHT11 */
DHTesp dhtSensor1;
DHTesp dhtSensor2;
int dhtPin1 = 4;
int dhtPin2 = 12;
TempAndHumidity sensor1Data;
TempAndHumidity sensor2Data;
/* ******************* */
void run_update();
void man_update();
/* TIMERS */
TickTwo timer1(run_update, 1000);
TickTwo timer2(man_update, 1000);
/* ****** */
/* VARS */
enum controls {OFF, RUN, MAN_SEL};
controls cntrl = OFF;
unsigned long timer_1;
float mcuVer = 1.0;
float guiVer = 1.0;
String mcu = "ESP32 WROOM-32U";
String gui = "NEXTION NX8048K050_011";
String splashString_1 = "MCU: v" + String(mcuVer,2) + " | " + "GUI: v" + String(guiVer,2);
String splashString_2 = mcu + " | " + gui;
float set_temp_max;
float set_temp_thresh;
float set_hum_min;
float set_hum_thresh;
float set_hours;
bool heatBOOL = false;
bool humBOOL = false;
float s1_temp;
float s1_hum;
float s2_temp;
float s2_hum;
/* *** */
/* COMM VOIDS */
void changeTxtField(byte boxNo, String text) {
Serial1.print(F("t"));
Serial1.print(boxNo);
Serial1.print(F(".txt=\""));
Serial1.print(text);
Serial1.print(F("\""));
HMICommandEnd();
};
void changeTxtFieldFloat(byte boxNo, float text) {
Serial1.print(F("t"));
Serial1.print(boxNo);
Serial1.print(F(".txt=\""));
Serial1.print(text);
Serial1.print(F("\""));
HMICommandEnd();
};
/* ********** */
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
disableCore0WDT();
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, 23, 13);
while (!Serial1) {
delay(1);
};
Serial1.print(F("baud=115200"));
HMICommandEnd();
Serial1.print(F("bauds=115200"));
HMICommandEnd();
SPIFFS.begin(true);
/* Power Pins (DHT11) */
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(15, OUTPUT);
digitalWrite(15, HIGH);
/* ****************** */
/* Sensor Setup */
dhtSensor1.setup(dhtPin1, DHTesp::DHT11);
dhtSensor2.setup(dhtPin2, DHTesp::DHT11);
/* ************ */
/* Core Handling */
TaskHandle_t c1_CNTRL;
TaskHandle_t c2_SCRN;
xTaskCreatePinnedToCore(c1_CNTRL_c,"Controller",10000,NULL,1,&c1_CNTRL,0);
delay(200);
xTaskCreatePinnedToCore(c2_SCRN_c,"Screen",10000,NULL,2,&c2_SCRN,1);
delay(200);
/* ************ */
timer_1 = millis();
};
void HMICommandEnd() {
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
};
/* *** */
void IRAM_ATTR controller() {
switch(cntrl) {
case OFF:
//none
vTaskDelay(1);
break;
case RUN:
timer1.update();
vTaskDelay(1);
break;
case MAN_SEL:
timer2.update();
vTaskDelay(1);
break;
};
};
/* *** */
/* *** */
void HMI_read() {
static uint8_t HMI_read_data[10];
static uint8_t HMI_read_data_i;
static uint8_t a5count;
uint8_t readSerial;
while (Serial1.available() > 0) {
readSerial = Serial1.read();
if (readSerial == 0xa5) {
++a5count;
if (a5count > 2) {
a5count = 0;
HMI_read_data_i = 0;
}
}
else {
a5count = 0;
};
HMI_read_data[HMI_read_data_i] = readSerial;
if (HMI_read_data_i == 5) {
switch (HMI_read_data[1]) {
case 0:
switch (HMI_read_data[2]) {
case 0:
Serial.println("Splash Page");
changeTxtField(1, splashString_1);
changeTxtField(2, splashString_2);
loadSettings();
break;
};
break;
case 1:
switch (HMI_read_data[2]) {
case 0:
Serial.println("Main Menu");
cntrl = OFF;
break;
case 1:
switch (HMI_read_data[3]) {
case 0:
Serial.println("3rd case ex.");
break;
case 1:
Serial.println("3rd case ex.");
break;
}
break;
}
break;
case 2:
Serial.println("Running Page");
timer1.start();
cntrl = RUN;
changeTxtField(12, String(set_temp_max,2) + " C");
changeTxtField(13, String(set_hum_min,2) + "%");
changeTxtField(14, String(set_temp_max,2) + " hrs");
break;
case 3:
switch (HMI_read_data[3]) {
case 0:
Serial.println("Manual Run Page");
heatBOOL = false;
humBOOL = false;
timer2.start();
cntrl = MAN_SEL;
break;
case 1:
Serial.println("Manual Heat Op BTN");
if (heatBOOL == false) {heatBOOL = true;};
if (heatBOOL == true) {heatBOOL = false;};
break;
case 2:
Serial.println("Manual Humidity Op BTN");
if (humBOOL == false) {humBOOL = true;};
if (humBOOL == true) {humBOOL = false;};
break;
};
break;
case 4:
Serial.println("Config Page");
break;
case 5:
Serial.println("Keyboard Page");
break;
};
++HMI_read_data_i;
if (HMI_read_data_i > 9) {
HMI_read_data_i = 9;
}
}
};
};
/* *** */
void getSensorData() {
sensor1Data = dhtSensor1.getTempAndHumidity();
sensor2Data = dhtSensor2.getTempAndHumidity();
s1_temp = sensor1Data.temperature;
s1_hum = sensor1Data.humidity;
s2_temp = sensor2Data.temperature;
s2_hum = sensor2Data.humidity;
};
void run_update() {
getSensorData();
changeTxtField(8, String(s1_temp,2) + " C");
changeTxtField(9, String(s1_hum,2) + "%");
changeTxtField(10, String(s2_temp,2) + " C");
changeTxtField(11, String(s2_hum,2) + "%");
};
void man_update() {
getSensorData();
changeTxtField(15, String(s1_temp,2) + " C");
changeTxtField(16, String(s1_hum,2) + "%");
changeTxtField(17, String(s2_temp,2) + " C");
changeTxtField(18, String(s2_hum,2) + "%");
};
void loadSettings() {
File setDir = SPIFFS.open("/settings.txt", "r");
if (!setDir) {
Serial.println("Couldn't Load Settings");
}
Serial.println("=======Load Settings=======");
for (int i = 1; i < 7; i++) {
String s = setDir.readStringUntil('\n');
if (i == 1) {
set_temp_max = s.toFloat();
Serial.println("MAX TEMP = ");
Serial.println(set_temp_max);
};
if (i == 2) {
set_temp_thresh = s.toFloat();
Serial.println("TEMP THRESHOLD = ");
Serial.println(set_temp_thresh);
};
if (i == 3) {
set_hum_min = s.toFloat();
Serial.println("MIN HUMIDITY = ");
Serial.println(set_hum_min);
};
if (i == 4) {
set_hum_thresh = s.toFloat();
Serial.println("HUMIDITY THRESHOLD = ");
Serial.println(set_hum_thresh);
};
if (i == 5) {
set_hours = s.toFloat();
Serial.println("RUN TIME (HOURS) = ");
Serial.println(set_hours);
};
};
setDir.close();
};
void writeSettings() {
File setDir1 = SPIFFS.open("/settings.txt", "w");
if (!setDir1) {
Serial.println("Couldn't Load Settings");
};
Serial.println("=======Save Settings=======");
for (int i = 1; i < 7; i++) {
if (i == 1) {
setDir1.print(set_temp_max);
setDir1.print('\n');
};
if (i == 2) {
setDir1.print(set_temp_thresh);
setDir1.print('\n');
};
if (i == 3) {
setDir1.print(set_hum_min);
setDir1.print('\n');
};
if (i == 4) {
setDir1.print(set_hum_thresh);
setDir1.print('\n');
};
if (i == 5) {
setDir1.print(set_hours);
setDir1.print('\n');
};
};
setDir1.close();
};
void loop() {vTaskDelete(NULL);};
void c1_CNTRL_c( void * pvParameters ) {
for (;;) {
controller();
vTaskDelay(1);
};
};
void c2_SCRN_c( void * pvParameters ) {
for (;;) {
HMI_read();
vTaskDelay(1);
};
};
I have some libraries commented out just because I'm not using them yet, it's a work in progress.