logo elektroda
logo elektroda
X
logo elektroda

How to send float values between Arduino over UART?

omnixcrs 2436 52
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #31 18247101
    krisRaba
    Level 31  
    Generally now there is a meatloaf number two, because the standard UART is not a multidrop network, so in fact you should only directly connect 2 devices via UART or use a transceiver e.g. RS485, which makes it possible to connect more receivers to the communication lines. Or use two UARTs in one esp (if there is such an option there, because I don't know how many are derived), then you know with whom and what you are talking about, well and you have a point-to-point connection, not a multidrop. If you were using something like RS485, though, you could enter the address of the controllers and then you send the ID and the variable, and then you're able to recognise what the data is and from whom. Because just the bytes of the variable in the buffer won't tell you, and you can put those bytes into anything and the "system" won't stammer...
    And what topology do you have? The 3 esp are at what distance from each other? Because maybe you only need one that handles all the slaves over I2C and the whole thing is overkill? Because you probably have enough resources in a single esp.

    Added after 2 [minutes]:

    PS. if you have some longer distances, then pulling a bare UART out into the world is not a very good idea. And if short, you don't need 3 esp ;) .
  • ADVERTISEMENT
  • #32 18247123
    khoam
    Level 42  
    The problem is that Serial1 on the ESP8266 cannot be used to read data, but only to send it .
    http://arduino.esp8266.com/Arduino/versions/2.3.0/doc/reference.html#serial

    Added after 4 [minutes]: .

    You can use the so-called software-serial in the ESP8266: https://github.com/plerup/espsoftwareserial
    Then you can select different pins in the esp for the second serial port.
    At what distance from each other are these esp?
  • ADVERTISEMENT
  • #33 18247178
    omnixcrs
    Level 11  
    They are not at any distance. They are placed on a single board except that it is a multiprocessor circuit because of the implementation of different sentences in different modes.

    Added after 4 [minutes]: .

    I am not using any serial division at the moment.
  • ADVERTISEMENT
  • #34 18247196
    khoam
    Level 42  
    Just connect them via the I2C bus with the proviso that only one of them can be the master. You already have information on this in the post #21 .

    Obviously this single master will need to support other I2C slaves, namely the expander and the display.
  • #35 18247389
    krisRaba
    Level 31  
    That is, you could probably do it on one, you just need to organise the code better and split it functionally similar to how you now split it into several esp ;-) From what you write, you are operating on expanders anyway, the display you have with the controller etc.
    Such a pseudo-multicore, it's more for complex tasks where you can't fit in one, or for redundancy ;-) .
    Take a look at this to see if you're not overcomplicating ;-) .
    And if you want to have functionally split blocks, some multitasking takes care of that and one thread doesn't have to worry about what the other is doing independently, and if you need to, you push the variable between threads, not between microcontrollers ;-) .
  • #36 18265279
    omnixcrs
    Level 11  
    Hello gentlemen,
    I have followed your advice and simplified the circuit. I have one esp-12f handling one expander for buttons and lcd 2x16 over i2c and a NodeMcu V3 handling an expander for outputs. The connection between them is just 5v 0v tx and rx - nothing more. I2c are separate. Everything seems to work, but unfortunately I still have problems with uart communication - one moment it is good, the next just 0 etc. My device can work in two modes AUTO and MANUAL. In case of AUTO mode nodemuc sends variables to esp from time to time and here rather everything works, but in case of MANUAL mode communication must be in both directions and here it is worse. I have included below simplified communication codes, maybe you can find something:

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

    Code on esp:

    Code: C / C++
    Log in, to see the code
  • ADVERTISEMENT
  • #37 18265591
    khoam
    Level 42  
    Try using the Serial.availableForWrite() function:
    https://www.arduino.cc/reference/tr/language/...tions/communication/serial/availableforwrite/
    Below is an example of modified UART_send() function code for the ESP-12:
    Code: C / C++
    Log in, to see the code
    .
    There may be a side effect of not sending data despite the condition (mode_button == 1) being met , because there will not be enough space in the output buffer. You should handle such a situation by, for example, restarting the timer that calls UART_send().
  • #38 18266390
    omnixcrs
    Level 11  
    Ok I'll give it a try. And tell me how does the transmission over UART work exactly. E.g. if I send 48 bytes to esp this is how much space I take up in its buffer and now when I read them with Serial.readBytes they are removed from the buffer or do they still take up these 48 bytes?
  • #39 18266399
    khoam
    Level 42  
    They will be deleted if they have been read.

    Added after 10 [minutes]: .

    Serial.write() returns information about the number of "sent" bytes. You can also check this additionally.
    If the output buffer is overflowing, then Serial.write() blocks the program from running, hence why I suggested using the Serial.availableForWrite() function to check exactly whether a packet of data of a certain size can be sent.
  • #40 18266544
    omnixcrs
    Level 11  
    Well you're right, sometimes the programme would hang up on me. I now know why. And why does the send buffer overflow occur and how to avoid it?
  • #41 18266553
    khoam
    Level 42  
    omnixcrs wrote:
    And why does the send buffer overflow occur and how to avoid it ?
    .
    As I wrote earlier, the buffer has a limited size. You should not send more than is possible at any given time i.e. has not been received by the other party. The data rate over the UART is also limited.
  • #42 18266571
    omnixcrs
    Level 11  
    Well ok, but if I send every 35 seconds and receive every 20 then everything will always be received, yes?
  • #43 18266639
    khoam
    Level 42  
    omnixcrs wrote:
    No ok, but if I send every 35s and receive every 20 then everything will always be received, yes?
    .
    omnixcrs wrote:
    but in the case of MANUAL mode the communication must be two-way and here it is already worse.
    .
    Serial-Serial communication takes place in half-duplex mode. This means that at any given time the UART can either send data or receive data, but not simultaneously. As the UART cannot send data, it will be held in the output buffer until sending is possible.
  • #44 18266656
    krisRaba
    Level 31  
    Why is it so strange there? Is there some RS transceiver? Or such stupidly written libraries?
    After all, normally the UART laughs in full-duplex....
  • #45 18266679
    khoam
    Level 42  
    krisRaba wrote:
    Why is it so strange there? Is there some kind of RS transceiver? Or such a stupidly written library?
    .
    Instead of Serial.begin(512000) you could use e.g. Serial.begin(512000, SERIAL_8N1, SERIAL_FULL) :) .
    On both sides, of course. This option with SERIAL_FULL appeared in the ESP8266 and ESP32. It is not present in the standard Arduino HAL.
  • #46 18266684
    krisRaba
    Level 31  
    So, however, stupidly written libraries ;-)
  • #47 18266700
    khoam
    Level 42  
    omnixcrs wrote:
    No ok, but if I send every 35 s and receive every 20 then everything will always be received, yes?
    .
    There may also be some interference or dropouts. At what distance are these ESPs from each other?
  • #48 18266718
    krisRaba
    Level 31  
    By the way, why make the programme so complicated and make your life more difficult? In normal MCUs the reading is done when something arrives, not artificially waiting for unknown amounts of time.
    In between these 20s intervals you put everything to sleep so that it can't check whether data has already arrived?
  • #49 18266735
    khoam
    Level 42  
    krisRaba wrote:
    In normal MCUs the reading is done when something comes in, not artificially waiting for unknown amounts of time.
    .
    In this "normal" MCU you can also using handlers for interrupts from the UART.
    Here is a simple example:
    https://github.com/nkolban/Sample-ESP8266-App/tree/master/Sample%20FreeRTOS%20App

    Added after 32 [seconds]:

    krisRaba wrote:
    Between these 20s intervals you put everything to sleep that it can't check for itself if data has already come in?
    .
    It doesn't put itself to sleep. It can do other things during that time.
  • #50 18267641
    krisRaba
    Level 31  
    Ok, so it can be done right, just a matter of reaching for the right sources. I already thought they made it all so clunky and lame ;-) .
  • #51 18268198
    omnixcrs
    Level 11  
    no distance, they are next to each other on the board
  • #52 18268380
    krisRaba
    Level 31  
    omnixcrs wrote:
    I followed your advice and simplified the layout. I have one esp-12f handling one expander for buttons and lcd 2x16 over i2c and a NodeMcu V3 handling an expander for outputs. The connection between them is just 5v 0v tx and rx - nothing more. I2c are separate.
    .
    omnixcrs wrote:
    distance none, they are next to each other on the board
    .
    Then why didn't you simplify it even more and leave just one???? :) Then you have no communication over the UART and the problem is over :P .
    You're making life a bit more difficult for yourself...
    If I understand correctly, this output expander is on I2C? And the display too? And the expander for the buttons too? Jeez, it's just asking to put it all on one or two I2C buses in ONE proc and be done with it. Why multiply it so much? Separate programs, communication between controllers etc. I'm not even mentioning the fact that so far you had 3 procs for this 8-O :crazyeyes: .
  • #53 18268415
    khoam
    Level 42  
    omnixcrs wrote:
    They are placed on one board with the fact that it is a multiprocessor chip due to the implementation of different sentences in different modes.
    .
    I've read your assumptions again, and yes I wonder if you shouldn't connect the ESP-12 to the NodeMCU over SPI if you want to get a bit closer to the multiprocessor chip model. Of course then you need as many as 4 pins from each chip (MOSI, MISO, SS, SCK) and I don't know if this is possible.

    Yes, by the way the ESP-32 has two cores ;) .

Topic summary

The discussion revolves around sending float values between two Arduino devices over UART. The main challenge is how to transmit a float, such as 22.5, as Arduino's Serial.write() primarily handles byte data. Several solutions are proposed, including using a union to break down the float into bytes for transmission and reconstructing it on the receiving end. An alternative method involves using Serial.write() with a pointer to the float variable and the size of the float. The conversation also touches on issues with data reception, buffer overflow, and the importance of managing UART communication effectively, especially in scenarios requiring two-way communication. Suggestions include using Serial.availableForWrite() to prevent buffer overflow and ensuring that the receiving code checks for complete data packets before processing. Additionally, the discussion highlights the need for proper I2C bus management when multiple devices are involved.
Summary generated by the language model.
ADVERTISEMENT