logo elektroda
logo elektroda
X
logo elektroda

ESP8266 Arduino: Adding Newline to String Responses for Smartphone App

heyka 1977 36
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17955009
    heyka
    Level 17  
    Hello, I am testing a WiFi TCP/UDP controller program on a smartphone, I send a command from the smartphone and the ESP8266 responds. That it responds correctly I know because when I send commands from the PC the responses come without a problem, but in the smartphone in the program I do not see the response. For example, the ESP8266 responds with a string like this: "W1_OFF". I suspect that you need to add an end of line character or Enter or maybe both to the end of the text sent from the ESP. Program in ESP8266 written in arduino. How do you add a newline or an enters character at the end of text sent from the arduino?

    Added after 20 [minutes]: .


    Sending text:
    Code: C / C++
    Log in, to see the code


    Using the command:

    Code: C / C++
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #3 17956468
    heyka
    Level 17  
    Thank you for your response.

    The WiFi TCP/UDP Controller is not receiving data from the ESP, I've been trying various ways and nothing and with characters at the end and without unfortunately without success.

    I have a question, what if I wanted to send a string built as follows:

    int voltage = 5;
    int natexenir =1;

    and the string would look like :
    voltage, value of voltage, natexenir, value of natexenir

    What I mean is how to build strings from pre-made strings plus single characters plus numeric values.
  • #4 17956659
    khoam
    Level 42  
    heyka wrote:
    What I mean is how to build strings from ready-made strings plus single characters plus numeric values.
    .
    Here is the basic information: https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/

    Added after 9 [minutes]:

    Or alternatively operate on C-style text strings using the sprintf() function:
    http://www.cplusplus.com/reference/cstdio/sprintf/
  • #5 17957189
    kaczakat
    Level 34  
    Strings in Arduino can be added and converted, e.g. String0=String1+String2+String(number)+String3; and then Serial.println(String0);. It is safer, however, to use a static char array as a buffer for everything and in it locate individual characters one by one, e.g. with the sprintf function Link . In either case you have to bear in mind that RAM is not made of rubber, and in AVR it is severely limited. Nevertheless, with Strings surprises happen more often, with a static array you will immediately see that you have overstepped and cut something off.
    Helpful post? Buy me a coffee.
  • #6 17957477
    heyka
    Level 17  
    Code: C / C++
    Log in, to see the code
    .

    The command above sends data as a string through the port.

    How do I send it over UDP?

    command

    Code: C / C++
    Log in, to see the code


    sends an array of char, and I mean to send a string c .
  • Helpful post
    #7 17957904
    kaczakat
    Level 34  
    There are functions in Arduino to convert between a char and String array. You've already had links given to the Arduino Reference, and there's a list of functions/methods with examples at the bottom.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #8 17958507
    heyka
    Level 17  
    Such an example:

    Code: C / C++
    Log in, to see the code
    .

    is this what the text in the buf variable will look like?

    voltage,5,intensity,1
  • ADVERTISEMENT
  • Helpful post
    #9 17958530
    khoam
    Level 42  
    heyka wrote:
    is this what the text in the buf variable will look like?
    .
    No, it should be:
    Code: C / C++
    Log in, to see the code
    .
    if the text should look like this:
    heyka wrote:
    tension,5,intensity,1
    .

    and the buffer buf[] must be larger to accommodate all characters of the text.
  • #10 17958915
    heyka
    Level 17  
    Great, thanks it's OK.

    I have the NodeMCU module and library for the BPM 280 running over SPI

    Code: C / C++
    Log in, to see the code
    .

    What needs to be changed to fire it on I2C?
  • #11 17958934
    kaczakat
    Level 34  
    I just connected it to I2C, I didn't even check the operation over SPI. Try changing what you commented in the sketch to I2C (actually the example is under I2C) and connect it under the I2C pins of the Arduino. If something didn't click check if the I2C scanner detects it on the bus.
    Helpful post? Buy me a coffee.
  • #12 17959175
    heyka
    Level 17  
    kaczakat wrote:
    I just connected it to I2C, I didn't even check the operation over SPI. Try changing what you commented in the sketch to I2C (actually the example is under I2C) and connect it under the I2C pins of the Arduino. If something didn't work check if the I2C scanner detects it on the bus.
    .

    I don't have an Arduino board, just a NodeMcu, and this board doesn't have hardware I2C, is it possible to run the I2C scanner?
  • ADVERTISEMENT
  • #14 17959267
    heyka
    Level 17  
    The scanner will work, although on the page there is a reverse description of D1-SCL and D2 -SDA, it read me the address 0x76

    Added after 5 [minutes]:

    It works, BMP280.h was address Ox77, I changed it to 0x76 and it works. :)

    Added after 1 [minute]:

    Now it's time for the BME280 and I2C.
  • #15 17960677
    heyka
    Level 17  
    Maybe it will be useful to someone, I know no discovery but ready to upload and test.

    Working test programs, NodeMCU v3 board, modules work over I2C
    In the BMP280.h file you need to set the module address, in my case

    #define BMP280_ADDRESS (0x76)

    BMP280

    Code: C / C++
    Log in, to see the code
    .

    BME280

    Code: C / C++
    Log in, to see the code


    OLED display, address 0x3C and #define OLED_RESET -1

    Code: C / C++
    Log in, to see the code
    .

    Added after 1 [hour] 54 [minutes]:

    I am displaying temperature, humidity and pressure on an OLED display, the quantities are displayed to me with two decimal characters, how do I truncate one or two characters occurring after the decimal point?

    For example, I display humidity using the function:

    Code: C / C++
    Log in, to see the code
    .
  • #16 17960944
    khoam
    Level 42  
    heyka wrote:
    how do I truncate one or two characters occurring after a comma?
    .
    Code: C / C++
    Log in, to see the code
    .
    Source
  • #17 17961184
    heyka
    Level 17  
    Thanks, of course it works as I wanted it to.

    The link my colleague gave me is for Serial.print, but the formatting method works for both lcd.print and dispaly.print, hence my question:
    - is this some kind of standardisation of sending sending strings whether via serial port or on oled etc?
    -function bme.readPressure() for bme280 returns pressure as a string what is this string, c-string?
  • #18 17961199
    khoam
    Level 42  
    heyka wrote:
    The link my colleague gave me is for Serial.print, but the formatting method works both for lcd.print and for dispaly.print, hence my question:
    .
    Both classes inherit the print() and println() methods from the base class Print , so the method calls are identical.
  • #19 17961202
    heyka
    Level 17  
    And one more question, does the NodeMCU support any external interrupts such as INT for uP e.g. AVR.
  • #21 17961211
    heyka
    Level 17  
    Classes inherit. So it's object-oriented programming? This is already too high a level for me :( .
  • #22 17961216
    khoam
    Level 42  
    heyka wrote:
    Classes inherit. So it's object-oriented programming?
    .
    If you program with Arduino HAL and C++ then there is no turning back :) .
  • #23 17961228
    heyka
    Level 17  
    I am starting out with an Arduino.

    It used to be Bascom, C and now Arduino.
  • #24 17966506
    heyka
    Level 17  
    I'm now taking the RTC DS1307, https://abc-rc.pl/product-pol-6190-Modul-czas...s&utm_medium=google_shopping&curr=PLN (can't insert link in tags), using test programs from http://www.jarzebski.pl/arduino/komponenty/zegar-czasu-rzeczywistego-rtc-ds1307.html

    Header file for the library

    Code: C / C++
    Log in, to see the code
    .

    I have a question, the first initialisation function of the module looks as follows

    Code: C / C++
    Log in, to see the code
    .

    while its call in the sketch looks like:

    Code: C / C++
    Log in, to see the code
    .

    Where did the 'clock' come from?

    I have this sketch

    Code: C / C++
    Log in, to see the code


    function
    Code: C / C++
    Log in, to see the code


    sets the time from the computer at compile time,

    How do you set any other arbitrary time?

    I tried like this

    Code: C / C++
    Log in, to see the code


    but it doesn't compile :(

    Please help.

    Added after 49 [minutes]:

    Code: C / C++
    Log in, to see the code
    .

    compiles but does not set this time.
  • Helpful post
    #25 17966728
    kaczakat
    Level 34  
    Read up on C++, because asking every letter on the forum will take you a long time to create anything.
    In the same way as you create a variable "int variable;" and then use it in code "variable++;" the same way you create objects "DS1307 clock;" and then use functions/methods on them.
    For a start I recommend M. Zelent's course on C++ and object-oriented C++, it's on Yutube, and some book will complement this introduction.
    And with setting the time you are supposed to give two arguments to the function (separated by commas), and you are trying to give six - seriously though this video course.
    Helpful post? Buy me a coffee.
  • #26 17966846
    heyka
    Level 17  
    It's supposed to rain all day so, I'll take a C++ course.

    I would like to go back to the time setting for a moment, I have 3 functions in the header file (strange because they all have identical names):

    Code: C / C++
    Log in, to see the code
    .

    Isn't the first one for setting the time in a format like I wanted to do, i.e. giving 6 arguments.

    As for the third one, should you create arrays of type Char and insert them when calling the function?
  • #27 17966869
    Slawek K.
    Level 35  
    Setting the time using one of the functions from this library https://github.com/jarzebski/Arduino-DS1307 might look like this :
    Code: C / C++
    Log in, to see the code
    .

    First - during compilation
    Second - using the timestamp, i.e. the number of seconds from 01.01.1970 to now
    Third - by giving 6 arguments in the right order, i.e. year, month, day, hour, minute, second

    Greetings
  • #28 17966887
    heyka
    Level 17  
    Slawek K. wrote:
    .
    Third - by providing 6 arguments in the correct order i.e. year, month, day, hour, minute, second



    Code: C / C++
    Log in, to see the code
    .

    The test code from the author's page, should change the time in the layout, but it does not, it still remains the current one that was set at compilation.
  • Helpful post
    #29 17966902
    Slawek K.
    Level 35  
    Throw out the if condition and leave the setDateTime function alone.
  • #30 17966926
    heyka
    Level 17  
    Slawek K. wrote:
    Drop the if condition and leave the setDateTime function alone.
    .

    Thanks.

Topic summary

The discussion revolves around the issue of the ESP8266 not sending string responses correctly to a smartphone app via WiFi TCP/UDP. The user suspects that adding newline or carriage return characters to the end of the response strings may resolve the issue. Various solutions are proposed, including using special characters for newlines, constructing strings with numeric values using the sprintf() function, and the importance of buffer size when handling strings in Arduino. The conversation also touches on the use of the NodeMCU module and libraries for sensors like the BMP280 and DS1307 RTC, discussing their initialization and data handling methods.
Summary generated by the language model.
ADVERTISEMENT