A better Chat program

In the previous post a predefined node could initiate the chat and also that a node could not send more than one message one after the other, after sending a message it has to receive a message and then only it can send a new message.

In the new code any of the nodes can initiate the chat and also one node can send messages more than once.This is achieved by the fact that in the loop function we constantly keep on looking for serial input and any payload. Suppose a payload is available then only the transmission function is activated otherwise we never enter the transmission function. This makes the same code to work for both the nodes.

You can get the code and edit/fork/clone to suit your needs at my Github repository

You can input a message larger than 31 bytes and see what happens. Even those messages are transmitted. I experimented with very large messages and found out that although the radio does not get stuck anywhere but some part of the messages especially towards the ends and beginning get lost.


About Shantam Raj

Currently I am a final year B.Tech undergraduate majoring in “Electronics and Communication Engineering” from IIT Guwahati. I am passionate about electronics and robotics. Apart from that I love writing, visit https://medium.com/@shantam_raj for some of my works. I am a die hard soccer fan. I also love to play badminton. I am a hardware hacking enthusiast and a tinkerer !!.
This entry was posted in Arduino, nRF24L01+, wireless communication and tagged , , . Bookmark the permalink.

36 Responses to A better Chat program

  1. Joseph chrzempiec says:

    hello i was wondering how can i put the text into 15 characters instead of 32 and in your sketch you have this line int len = Serial.readBytesUntil(‘\n’, input, 31); it haves 31 characters not 32 why is that?

    • Shantam Raj says:

      you don’t need to change anything to send 15 characters because of the fact that they are string inputs so the remaining space is just filled with null character. And about the other question ….well u can as well use 32, i don’t think anything wrong will happen…..i wrote 31 because i am sending string so by default the last element will be a null character which leaves me with 31 bytes of actual message…it is not a very serious issue though.

      • Joseph chrzempiec says:

        hello when i try to do 15 characters that worked on the lcd but if i do say like 25 it will not show the first 15 characters it will show the last 15 characters of the text
        so it i say ( Hello how are you? good i hope. ) thats 31 characters okay when it displays on the lcd it only shows ( u? good i hope. )

      • Shantam Raj says:

        i didnt know u were using lcd……for that just reduce the size of all the buffers and variables that are of size 32 bytes to the maximum size ur lcd display can show and let me know if that works for you.

  2. Joseph chrzempiec says:

    I’m using the Nokia 5110 currently it only shows 12 characters per line 6 lines only right now i have it displaying the text as i post them from my desktop they show on line 1 and 2 that is 24 characters 12 per line and when i go to my laptop and send a text it display on line 3 and 4 that is 24 characters also the last 2 lines I’m try to figure out how to as i write the text in Serial monitor it can show the same text as i write in the lcd here is my sketch i will show you what i have now http://www.codehunter.info/sketches/_2waytextmessenger_nokia5100lcd.ino

  3. hi i am getting error while compiling the code.

    Error line
    done = radio.read(payload,32);

    Error message :
    “void value not ignored as it ought to be”

    • Shantam Raj says:

      it will be difficult for me to tell u where the problem(s) lie because i myself and a few others who used my code didn’t find any problem….are u using arduino uno or any other board because for leonardo and mega i think u will have to add a few more lines ?
      it is even possible that the problem lies before what is pointed by the compiler because many a times thats the case and the compiler catches the problem in the later part because of the previous error…..make sure ur code is exact as mine…..take care of braces….do some tinkering and let me know if the problem still persists.

  4. David says:

    Hi Shantam, im from spain and my english is bad 😦 jajajaja
    i have a question.
    in this line, arduino say me ´print_begin’ was not declared in this scope printf_begin();
    which is the problem?
    Thank you and if you can respond fast i will you aprecciate, because is for a wok 🙂

    • Shantam Raj says:

      Hi David !! thanks for stopping by and greetings from india.
      The problem that u are facing is quite trivial actually. Thats happening because u are just copying the code that i have written. Apart from that u have to add one more tab. please refer the last paragraph of the blog post starting with “NOTE”.
      P.S i hope u have already added the RF24 library form maniacbug.
      If u still have problems do feel free to write !!.

  5. Divyesh says:

    Hello Shantam,

    In my project I am using nRF24L01 and Arduino uno boards. When I try your sketch, it stops compiling. I think I have some problems with libraries. I have tried many examples, but every time I get like “error: void value not ignored as it ought to be”. For example in your sketch I am getting this error indicating at “done = radio.read( payload , 32 );” line. I have tries many libraries available for this transceiver. But I am getting same error.

    Can you please tell me which library you used? It would be great help.

    Thank You

  6. Divyesh says:

    Hello Shantam,

    Can you help me with the error that I have mentioned in the previous post? I have tried many versions of this RF24 library but the error stays as it is.

    Thank you

    • Shantam Raj says:

      okay , can u please give me more details of the error…
      As of now try these-
      1. check that u are copying the code correctly.
      2. i m using an extra tab that is named as printf.h . have you tried the “getting started” example in the library “RF24master” ? just copy the contents of “printf.h” tab there and paste it here (of course after making a new tab with the same name).
      3. it is difficult to say why are u having error in the “done” part, i already checked if i am missing any initializations and i found that everything is okay and i have used this code so many times so i dont think there should be some errors .

      notify me if the problem still persists.

  7. Divyesh says:

    thank you very much Shantam for your help. I have solved those errors. I have one more question for you. Have you worked on RF24 Audio library. I want to make a network of 4 nrf24l01 transceivers, such that one can broadcast the audio message from one transceiver at the base station to all the other transceivers connected to it. After receiving the message, the transceiver should reply back to the base station in same manner.

    I checked the examples available in RF24 Audio library. Are they sufficient for the above scenario?
    Please give some guidance.

    • Shantam Raj says:

      Oh i just came to know about it right now….i knew of a separate networking library but had not tested it. i will have to look into the matter. i will get u asap.

    • Shantam Raj says:

      yeah u r right, i just found out the optimized as well as audio and new networking libraries. u just need a separate mic and speaker for the getting started example. i am testing it right now. will let u know if it works.

  8. Divyesh says:

    Ok. I was looking at that library examples, but I think their explanation is not sufficient for me to understand. In that library they have two three types of schematics for connecting mic and speaker. I am using Arduino Uno boards. What should be the pin connection for these boards? Also if you can help me to broadcast the voice from base station, it would be really helpful.


    • Shantam Raj says:

      Yeah i felt that too… its not really clear how exactly to do stuff using that library. I will let you know if i make some progress. And i will also request you to kindly notify me if u make some progress as i am also busy with some other work.
      thanks in advance !!.

  9. Divyesh says:

    yes definitely I will let you know if I get any progress.

  10. Divyesh says:

    Before broadcasting the voice over nrf24l01 network, I was trying broadcast text message over the network. In RF24 library there is “starping” example. In that example the transceiver at the base station receives messages from other radios and then broadcasts its acknowledgement to all the radios.
    Now how can I modify that code, such that the base station broadcasts the text message to all the radios and then the radios reply back in the serial chat manner?
    Any suggestions?

    Thank you

  11. Iman says:

    Hi Shantam, I’ve been used your example, but i have some problem. when I upload sketch and try to send some word like “hi”, but the serial monitor shows “could not write”. It shows continously. What happen with that? are you have any idea for solving that problem?

  12. grapes09 says:

    Hello Shantam! Thank you so much for this tutorial 🙂 However, I got a problem… It runs smoothly with me but when I write something. It tells It couldn’t write.

    • Shantam Raj says:

      Just comment the line of code that is responsible for could not write on the serial monitor . this problem is coming because ur radio is not able to transmit. U might have to do a few tweaks as it is possible that the library has been updated meanwhile. I would also recommend u to run the getting started code and other examples in the library. If they work then mine should too. Otherwise as I have told u before do some necessary tweaks.

  13. O Emmanuel says:

    hello shantam, in order to prevent collision avoidance of data. How is your program guaranteed.

    • Shantam Raj says:

      Actually its already implemented in the library itself. If you look at description of the function radio.setRetries(15,15); you will notice that it is responsible for tr-transmitting the packet after a fixed interval for a max of 15 times if it does not receive an acknowledgement packet. If the ack packet is not received even after that(due to collision or whatever) then that packet is lost. My code does nothing to re=transmit that lost packet. But sice all of this is based on ack packets its very easy to write your own code so that you take care of the collision. That code is not on my blog but i think i have written it during the period i was working with these transceivers.

  14. john says:

    i have problem with nRf24L01.h library, where can i download it?

  15. Nivedita Tigadi says:

    Hi Shanatam Raj,

    I am not able to do serial chat with your code. I have taken library from the link you specified, code is getting compiled but no serial chat happening.

    • Shantam Raj says:

      Hello Nivedita, are you successfully able to run the Getting Started program in the examples section of the library?
      If it works then i have no reason as to why my code wont work.
      However as u can see my code is pretty simple and i have explained it in details, so i think u might be able to write your own code from scratch because of the possibility that something might have changed during these 2 years of time.

    • Shantam Raj says:

      One more thing, NRF24L01+ is very primitive and can be easily hacked(a lot of people have done that and recently a research paper came out too regarding the security flaws) , so unless u are working with it for learning or just for a prototype its okay. However if u are planning to make a product, i would recommend h/w with encryption engines or very well defined protocol stack. So can look into Texas Instruments Sub GHz family or work with BLE 4.x or BLE 5.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s