logo elektroda
logo elektroda
X
logo elektroda

ESP8266 Arduino: Adding Newline to String Responses for Smartphone App

heyka 3402 36
Best answers

How can I append a newline or carriage return to text sent from an ESP8266 in Arduino so a smartphone TCP/UDP app recognizes the response?

Append a line ending such as `\r\n` to the string before sending it, or use `println()` instead of `print()`, which adds the end-of-line automatically [#17955379][#17957189] In Arduino you can build the message by concatenating strings and numbers, for example `String msg = "W1_OFF\r\n";` or `String0 = String1 + String2 + String(number) + String3;` and then send it with `Serial.println(String0);` [#17957189] If you are working with C-style buffers, `sprintf()` is the recommended way to format the text before sending it [#17957189][#17957904]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17955009
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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
  • #2 17955379
    kaczakat
    Level 34  
    Posts: 1748
    Help: 317
    Rate: 229
    If you want to have special characters in the text you write them with "ƒ", e.g. "W1_ON", where r is return and n is new line.
    Helpful post? Buy me a coffee.
  • #3 17956468
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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.
  • #5 17957189
    kaczakat
    Level 34  
    Posts: 1748
    Help: 317
    Rate: 229
    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  
    Posts: 412
    Rate: 56
    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 .
  • ADVERTISEMENT
  • Helpful post
    #7 17957904
    kaczakat
    Level 34  
    Posts: 1748
    Help: 317
    Rate: 229
    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.
  • #8 17958507
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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
  • Helpful post
    #9 17958530
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #10 17958915
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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  
    Posts: 1748
    Help: 317
    Rate: 229
    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  
    Posts: 412
    Rate: 56
    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?
  • #14 17959267
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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  
    Posts: 412
    Rate: 56
    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
    .
  • ADVERTISEMENT
  • #16 17960944
    Anonymous
    Level 1  
  • #17 17961184
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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
    Anonymous
    Level 1  
  • #19 17961202
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    And one more question, does the NodeMCU support any external interrupts such as INT for uP e.g. AVR.
  • #21 17961211
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    Classes inherit. So it's object-oriented programming? This is already too high a level for me :( .
  • #22 17961216
    Anonymous
    Level 1  
  • #23 17961228
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    I am starting out with an Arduino.

    It used to be Bascom, C and now Arduino.
  • #24 17966506
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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  
    Posts: 1748
    Help: 317
    Rate: 229
    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  
    Posts: 412
    Rate: 56
    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  
    Posts: 3015
    Help: 259
    Rate: 1299
    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  
    Posts: 412
    Rate: 56
    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  
    Posts: 3015
    Help: 259
    Rate: 1299
    Throw out the if condition and leave the setDateTime function alone.
  • #30 17966926
    heyka
    Level 17  
    Posts: 412
    Rate: 56
    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.
Generated by the language model.

FAQ

TL;DR: DS1307 RTCs ship with 56 bytes of battery-backed user RAM, and “Both classes inherit the print() method” [Elektroda, khoam, post #17961199] Append "\r\n" to ESP8266 UDP packets so smartphone apps display them properly. Use sprintf() or Arduino String to merge numbers with text. [Maxim, DS1307 Datasheet]

Why it matters: Correct line endings, string handling, and timekeeping stop silent data losses and hard-to-trace bugs in IoT builds.

Quick Facts

• DS1307 address: 0x68; user RAM: 56 bytes; runs at 32 kHz [Maxim, DS1307 Datasheet] • NodeMCU I²C default pins: D1 (SCL) & D2 (SDA) [RoboIndia I2C Scanner] • lcd/Serial.print(x, d) prints x with d decimal places; d=0 gives integers [Arduino Reference] • ESP8266 UDP max payload: 1472 bytes on typical Wi-Fi MTU 1500 [Espressif SDK] • attachInterrupt() works on GPIO0,2,4,5,12–15; level or edge triggered [ElectronicWings NodeMCU Interrupts]

How do I make ESP8266 UDP responses visible in a smartphone controller app?

Most apps expect CR-LF line endings. Add "\r\n" at the end of every reply: Udp.write("W1_ON\r\n");. Test on a PC to confirm the app now shows the response [Elektroda, kaczakat, post #17955379]

What’s the shortest way to join text and numbers on Arduino?

Use sprintf with a fixed char buffer: char buf[30]; sprintf(buf,"napiecie,%d,natezenie,%d",napiecie,natezenie); [Elektroda, khoam, post #17958530] Or concatenate String objects: String msg = "napiecie,"+String(napiecie)+",natezenie,"+String(natezenie); [Elektroda, kaczakat, post #17957189]

Can I send an Arduino String directly with Udp.write()?

Yes. Convert with myString.c_str() and pass the length: Udp.write(myString.c_str(), myString.length());. The method needs a byte array plus size [Arduino WiFiUdp Reference].

How do I print only whole numbers from BME280 readings?

Specify the precision: lcd.print(bme.readHumidity(), 0); prints no decimals, while 1 keeps a single digit [Elektroda, khoam, post #17960944]

How can I switch the Adafruit BMP280 library from SPI to I²C on NodeMCU?

  1. Comment out the SPI constructor and enable Adafruit_BMP280 bme; 2. Call Wire.begin(); in setup. 3. Ensure SCL on D1 and SDA on D2. The sensor then initializes over I²C [Elektroda, heyka, post #17960677]

What is the safest way to scan I²C devices on NodeMCU?

Upload an I²C-scanner sketch (example: RoboIndia tutorial). It lists active 7-bit addresses; reverse D1/D2 labels in some board silkscreens [Elektroda, heyka, post #17959267]

Does NodeMCU support external interrupts like AVR INT pins?

Yes. Use attachInterrupt(digitalPinToInterrupt(pin), ISR, mode). Valid pins: 0,2,4,5,12–15. Modes: RISING, FALLING, CHANGE, LOW, HIGH [ElectronicWings NodeMCU Interrupts].

How do I set an arbitrary time on a DS1307 module?

Call clock.setDateTime(YYYY,MM,DD,HH,MM,SS); outside any if(condition). Example: clock.setDateTime(2023,5,25,14,30,00); [Elektroda, Slawek K., post #17966869]

What does the offset parameter in DS1307::readByte() do?

Offset selects which of the 56 RAM bytes (addresses 0x08–0x3F) you access. readByte(0) returns the first RAM byte, readByte(10) the eleventh, and so on [Elektroda, khoam, post #17974395]

Why does the library test #if ARDUINO >= 100 in its headers?

IDE 1.0 introduced Wire.write()/read(). Older 0.x cores used Wire.send()/receive(). The check includes the proper API for both toolchain eras [Elektroda, Slawek K., post #17973439]

Could using Arduino String crash my ESP8266?

Yes. Frequent concatenations fragment the heap; devices with <40 kB free RAM may reboot after hours of uptime [Espressif “Managing Heap Fragmentation”, 2016]. Experts advise fixed char buffers for long-running IoT tasks [Elektroda, kaczakat, post #17957189]

Quick 3-step: reset DS1307 time to build time

  1. Include and create DS1307 clock;. 2. In setup(), call clock.begin(); then clock.setDateTime(DATE,TIME);. 3. Upload once; the battery backs the time afterward. This matches the MCU compile timestamp [Elektroda, heyka, post #17966506]
Generated by the language model.
ADVERTISEMENT