Transfer ASCII chars from one ESP32 to another with optical fiber

Hi,
I have to do a project, where I need to transfer data from one ESP32 to another with light (optical fiber). One has keyboard with led and other one display. I can choose from 0-9, and I want to send it to the other one digitally.

  1. I need to convert a char that I get from keyboard to binary array.
  2. change a state of led to indicate that I am about to send some data
  3. begin sending the data by blinking a led with the stored 1s and 0s in array
  4. on the other one save received bits into array
  5. transfer that binary array back to char so I can print it out on display

I know how to send and receive data, but I don't know how to transfer char like '4' to binary array and back. Can someone help with that ?

Thanks

Welcome to the forum

Do you actually need to convert the char to a binary array ? Why not read the bits of the char variable using the bitRead() function and use their values

Oh okay but how can I then send the bits one by one with the LED ? I was thinking to store them into array and then use for to cycle all stored 1s 0s. I am open to any suggestions even if you send some link or small code to represent how would you solve this problem.

for example I receive from keyboard '0'
in ASCII:
0, dec: 48, hex: 30, oct: 60, bin: 110000
now I want to store the binary value of that '0' so I can blink the 1s and 0s with LED to the another receiver EPS32

You don't need an array. Simply read each bit of the char as you need it

for (int bit = 0; bit < 8; bit++)  //bit order can be reversed if you want
{
  digitalWrite(ledPin, bitRead(myChar, bit));
//wait a bit if you need to
}
1 Like

Oh nice this looks good ! Thank you !
But how do I store bits on the other site ? There I probably need that array to store bits one by one and then how do I transfer them to a char ?

On the other side you can use the bitWrite() function

As a matter of interest, how are you going to keep the 2 ends in synch ?

Okay I will need to look up something more about those bitWrite,Read functions never used them before.
And I want to always send a 8bit long data. The LED is always on until I hit the "SEND" button, then it will change the status to LOW and the other one will indicate the change and will begin receiving function. I will send 1 bit every 100ms and the other one will read every 100ms but in the middle so reading will be shifted by 50ms
291596202_3137049726507986_1938238024350073271_n
here is little scheme, if you have any better ideas fill me in !

little curious here

why not use UART for this

you hook a resistor-LED(that gives out light) on TX end to the uart tx pin

and another resistor-LED ( that detects light) on the receiving end to the uart rx pin

no common ground

what you're trying to achieve there is what literally happens under the uart's hood

1 Like

Yes I was thinking about UART too but I have 0 experience with it. If you can help me out with that or you have any good explanation video hit me with it !
And I am using a photoresistor to receive data because I also have to me measure a strength of the signal and I don't know if I can use that with UART, any suggestions ?

Are there any other requirements that you have not mentioned ?

Using a serial connection, such as a UART, SPi, I2C etc will not allow you to measure the strength of the signal, whatever that means

The main problem with your proposed solution is in keeping the two ends in synch. What sort of distance is there between the two ends of the link ?

Very strange asking about how to connect two devices together with either optical fibre or wire when they are specifically designed to communicate by radio. :astonished:

Just a few meters, I have different types of length (1, 2, 5 meters) and it has to represent for children how digital data are transferred with optical cable.

I am using ESP32 bcs they are cheap and offer a lot of things, more then arduino if something flew in my mind to do with it, and they are faster.

Your full requirements are gradually emerging. If only you had given us the full picture at the start. Is there anything else that you want to tell us ?

So requirements are :

  1. Be able to set the brightness and color of the RGB LED, and monitor the signal strength (brightness) on the other one modul - this is working with RGB LED diode and photo resistor.
  2. Now I need to use this set color of the signal to transfer simple data from the 3x4 keyboard (numbers from 0-9) and display them on LCD - it has to represent how data are transferred via optical cable and if the bend it or damage in some way (loss of a signal strength) it corrupts the data it sending, in the future I would like to upgrade it so you could send a full message with a serial monitor on PC, but for now I just need to transfer simple char numbers from keyboard

And I was thinking to use UART, it will be faster and better, but idk how I would do it with the photoresistor I am using and I have never done anything with UART
Or develop my "own" data transfer protocol that will always send 8 bits of data and I will be able to set my own detection value for 1s and 0s and the speed of transfer so the kids can see it how it is transferring data and will have time to disturb it.
So I am open to any ideas, I never done a transfer of data between 2 Arduinos so no skill with UART but I know it is a useful thing that would solve my problem, but idk how and if it will make my requirements.

Again big thanks to you guys for trying to help me, sorry for not specifying my problem correctly this is my first post on any forum.

Another rabbit out of the hat

OK, I will agree to that.

Fur sure. :grin:

@kotvald
After 18 posts, I guess this is wasted, but I'd suggest you read the first three items here, anyway:

C

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.