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.

36 thoughts on “A better Chat program

  1. 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?

    1. 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.

      1. 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. )

      2. 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. 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

    1. 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.

  3. 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 🙂

    1. 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 !!.

  4. 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

  5. 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

    1. 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.

  6. 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.

    1. 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.

    2. 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.

  7. 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.


    1. 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 !!.

  8. 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

    1. hi divyesh, sorry for replying so late…i have the beta codes for what u r trying to achieve so u will have to modify and debug it ur self. send me some contact so that i will send u the codes.

  9. 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?

    1. If you can get the Getting Started example to work then my code will also work otherwise it wont. First check if the hardware is proper or not. This code has been extensively tested my many people and all of them got it working. You will too.

  10. 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.

    1. 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.

    1. 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.

  11. 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.

    1. 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.

    2. 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s