Unable to get variables from wifimanager sketch to hardwareserial

Hi,

I want to get two variables from a webform from the wifimanager sketch into a hardwareserial setting. I first had to convert the values char Baudrate to int BAUD and char Comsetting to uint32_t COMSETTING. When I fill in the form with different settings for baudrate and comsettings, the output from the hardware serial to the comport over wifi (accessable via telnet) differs.

When i use the fixed setting:
COM[1]->begin(UART_BAUD1, SERIAL_PARAM1, SERIAL1_RXPIN, SERIAL1_TXPIN, true);
#define UART_BAUD1 BAUD // Baudrate UART1
#define SERIAL_PARAM1 SERIAL_7N1 // Data/Parity/Stop UART1
#define SERIAL1_RXPIN 18 // receive Pin UART1
#define SERIAL1_TXPIN 15 // transmit Pin UART1
#define SERIAL1_TCP_PORT 23 // Wifi Port UART1

everything works fine and I get the output from the hardwareserial in a telnetsession.

I think there is something wrong with the conversion of the SERIAL_PARAM1 SERIAL_7N1 wich I want to set via the webform and the setting that sets the baudrate.

I added the sketches as an attachment

config.h (2.51 KB)

ESP32-Serial-Bridge.ino (3.45 KB)

WiFiManagerwithCustomParameters.ino (6.96 KB)

void comsetup_setup()
  {
    if (Comsetting == "7E1"){
    #define SERIAL_PARAM1 SERIAL_7E1
  }
    if (Comsetting == "8N1"){
    #define SERIAL_PARAM1 SERIAL_8N1
  }
}

That is not how you compare strings in C/C++

Also, #defines are a compile time constant, that code runs at execution time so you can't change Comsetting on the fly and expect your code to change compile time constants.

blh64:

void comsetup_setup()

{
    if (Comsetting == "7E1"){
    #define SERIAL_PARAM1 SERIAL_7E1
  }
    if (Comsetting == "8N1"){
    #define SERIAL_PARAM1 SERIAL_8N1
  }
}



That is not how you compare strings in C/C++

Also, #defines are a compile time constant, that code runs at execution time so you can't change Comsetting on the fly and expect your code to change compile time constants.

I understand. I'm new to this. What would be a better way to get the parameters for the hardwareserial from the web form?

From 'HardwareSerial.h':

// Define config for Serial.begin(baud, config);
#define SERIAL_5N1 0x00
#define SERIAL_6N1 0x02
#define SERIAL_7N1 0x04
#define SERIAL_8N1 0x06
#define SERIAL_5N2 0x08
#define SERIAL_6N2 0x0A
#define SERIAL_7N2 0x0C
#define SERIAL_8N2 0x0E
#define SERIAL_5E1 0x20
#define SERIAL_6E1 0x22
#define SERIAL_7E1 0x24
#define SERIAL_8E1 0x26
#define SERIAL_5E2 0x28
#define SERIAL_6E2 0x2A
#define SERIAL_7E2 0x2C
#define SERIAL_8E2 0x2E
#define SERIAL_5O1 0x30
#define SERIAL_6O1 0x32
#define SERIAL_7O1 0x34
#define SERIAL_8O1 0x36
#define SERIAL_5O2 0x38
#define SERIAL_6O2 0x3A
#define SERIAL_7O2 0x3C
#define SERIAL_8O2 0x3E

So:

  uint8_t settings;

  settings = SERIAL_8N1;
  Serial.begin(115200, settings);

You can use 'if' statements to control which value is assigned to the 'settings' variable. But, as already pointed out, your attempt to compare c-strings is incorrect. Fix that first.

You can ignore the if statement, i didn’t refer to it in the sketch, I forgot to remove it.

When you look at the attached sketch you see that I have a variable in the webform Comsetting (char) wich I convert with uint32_t COMSETTING = atol(Comsetting); to uint32_t.

I then try to use the converted COMSETTING to set the hardwareserial.

So when I fill in the Comsetting webform with SERIAL_8N1 (char) it is converted to COMSETTING (uint32_t)

I think the Baudrate (char) to BAUD (int) already works but I’m not sure.

In your WiFiManagerwithCustomParameters.ino sketch, you copy the input field and convert it to a long int and then use that

  //...

  strcpy(Comsetting, custom_Comsetting.getValue());
  //...

  uint32_t COMSETTING = atol(Comsetting);
  //...
  COM[1]->begin(BAUD, COMSETTING, SERIAL1_RXPIN, SERIAL1_TXPIN, true);

in your Bridge sketch, you are comparing Comsetting to strings (incorrectly)

void comsetup_setup()
{
    if (Comsetting == "7E1"){
    #define SERIAL_PARAM1 SERIAL_7E1
  }
    if (Comsetting == "8N1"){
    #define SERIAL_PARAM1 SERIAL_8N1
  }
}

which is it? What exactly are the contents of the variable Comsetting? if it is a string like "8N1", then converting that to a long int will give you the value 8 which is totally wrong.

You need to define a variable

uint_8_t ComValue;  // don't know if this is the correct type

void comsetup_setup()
{
    if (strcmp(Comsetting,"7E1") == 0 ){
    ComValue = SERIAL_7E1;
  }
  if (strcmp(Comsetting,"8N1") == 0){
    ComValue = SERIAL_8N1;
  }
  //...
  COM[1]->begin(BAUD, ComValue, SERIAL1_RXPIN, SERIAL1_TXPIN, true);
}

rwanrooy:
You can ignore the if statement, i didn't refer to it in the sketch, I forgot to remove it.

When you look at the attached sketch you see that I have a variable in the webform Comsetting (char) wich I convert with uint32_t COMSETTING = atol(Comsetting); to uint32_t.

I then try to use the converted COMSETTING to set the hardwareserial.

So when I fill in the Comsetting webform with SERIAL_8N1 (char) it is converted to COMSETTING (uint32_t)

I think the Baudrate (char) to BAUD (int) already works but I'm not sure.

You are confusing a string "8N1" with a compiler constant SERIAL_8N1 with the value of said constant (0x06)

I just uploaded the new sketch with the following config:

uint32_t ComValue; // don't know if this is the correct type

void comsetup_setup()
{
if (strcmp(Comsetting,"7E1") == 0 ){
ComValue = SERIAL_7E1;
}
if (strcmp(Comsetting,"8N1") == 0){
ComValue = SERIAL_8N1;
}
}

COM[1]->begin(BAUD, ComValue, SERIAL1_RXPIN, SERIAL1_TXPIN, true);

I think it works now but I have to do some further testing. I will let you know if it works. For now, thanks for your support!

It worked for some time but I changed something in the sketch but I don’t know what. I am searching for it for some time now but don’t know what’s wrong.

I managed to set the ComValue and the comport setting changed to the configured value. Now I am not able to set it anymore with the 7E1 or 8N1. I attached the code again.

char Baudrate[40];
char Comsetting[60];
// char Duty_Cycle[34];

int BAUD = atoi(Baudrate); // zet Baudrate (char) om naar BAUD (int)

uint32_t ComValue; // set the type

void comsetup_setup()
{
if (strcmp(Comsetting,“7E1”) == 0 ){
ComValue = SERIAL_7E1;
}
if (strcmp(Comsetting,“8N1”) == 0){
ComValue = SERIAL_8N1;
}
}

COM[1]->begin(BAUD, ComValue, SERIAL1_RXPIN, SERIAL1_TXPIN, true);

config.h (2.64 KB)

ESP32-Serial-Bridge.ino (4.26 KB)

WiFiManagerwithCustomParameters.ino (6.45 KB)

Read the sticky post at the top of the forum. Don't add attachments when you can include your code within code tags. It helps people help you.

Sorry, I didn’t know that. But do you have a clue?

I want to rewrite a variable from char to uint32_t.

The code is:

char Comsetting[60];

uint32_t ComValue; // don't know if this is the correct type

void comsetup_setup()
{
if (strcmp(Comsetting,"7E1") == 0 ){
ComValue = SERIAL_7E1;
}
if (strcmp(Comsetting,"8N1") == 0){
ComValue = SERIAL_8N1;
}
}

When i print the configured value to the serial interface I get the following output:

11:24:13.888 -> Comsetting: 7E1
11:24:13.888 ->
11:24:13.888 -> ComValue: 0

I want to get the ComValue to SERIAL_7E1 when I enter 7E1 in a webform but as you can see it is rewritten to 0

We need to understand the meaning of the following declaration before we assign any value to it.

uint32_t ComValue;

The above declaration can also be written as:

unsigned long int ComVlaue;

The ‘unsigned long int’ type says that only a positive numerical value of the range (0x00000000 to 0xFFFFFFFF = 0 to 4294967294) could be stored in the variable ComValue.

Does this quantity: SERIAL_7E1 evaluate to a numerical value? If not, how can we assign/store this value in a variable like ‘unsigned long int ComValue’?

I want to set the hardwareserial setting:

COM[1]->begin(BAUD, ComValue, SERIAL1_RXPIN, SERIAL1_TXPIN, true);

comvalue has to be something like:

SERIAL_8N1 or
SERIAL_7E1

duplicate post:

@rwanrooy

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.