A simple chat program

Let’s develop a simple chat program so that we can chat without the internet ! :p

The algorithm that i m going to use is as follows……

ON THE TX NODE –

The program waits for a serial input. As soon as the user enters the input it stores it in a variable until the user presses the enter. After that the flow is transferred to the function that transmits the message .Once the message has been transmitted properly then the control is transferred to the RX part that waits for the payload from other node.

ON THE RX NODE –

Exactly the same thing happens for the the other node, the only difference being that it first stays in RX mode then it goes to the serial input mode and finally to the TX mode.

So the TX part is (actually the node that writes first)…

#include<SPI.h>
#include"nRf24L01.h"
#include"RF24.h"
#include"printf.h"
RF24 radio(9, 10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
bool go_to_tx_mode = false;
bool go_to_rx_mode = false;
bool go_to_serial_mode = true;
char input[32] = "";
void setup() {
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.enableDynamicPayloads();
  radio.setRetries(15, 15);
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.startListening();
  radio.printDetails();
  // put your setup code here, to run once:

}

void loop() {
  if (go_to_tx_mode)
  {
    radio.stopListening();
    bool ok = radio.write(input, 32);
    if (ok)
    {
      printf("written successfully !!!\n");
      printf("ME : %s\n", input);
      go_to_rx_mode = true;
      go_to_tx_mode = false;
      radio.startListening();
    }
    else
      printf("could not write....\n");
  }
  if (go_to_rx_mode)
  {
    if (radio.available())
    {
      char payload[32] = "";
      bool done = false;
      while (!done)
      {
        done = radio.read( payload , 32 );
        printf("HIM : %s\n", payload);
        go_to_serial_mode = true;
        go_to_rx_mode = false;
      }
      radio.stopListening();
    }
  }
  if (go_to_serial_mode)
  {
    if (Serial.available())
    {
      int len = Serial.readBytesUntil('\n', input, 31);
      input[len] = '\0';
      go_to_tx_mode = true;
      go_to_serial_mode = false;
    }
  }
  // put your main code here, to run repeatedly:

}

The rx part is………

#include<SPI.h>
#include"nRf24L01.h"
#include"RF24.h"
#include"printf.h"
RF24 radio(9, 10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
bool go_to_tx_mode = false;
bool go_to_rx_mode = true;
bool go_to_serial_mode = false;
char input[32] = "";
void setup() {
  Serial.begin(57600);
  printf_begin();
  radio.begin();
  radio.enableDynamicPayloads();
  radio.setRetries(15, 15);
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
  radio.startListening();
  radio.printDetails();
  // put your setup code here, to run once:

}

void loop() {
  if (go_to_tx_mode)
  {
    radio.stopListening();
    bool ok = radio.write(input, 32);
    if (ok)
    {
      printf("written successfully !!!\n");
      printf("ME : %s\n", input);
      go_to_rx_mode = true;
      go_to_tx_mode = false;
      radio.startListening();
    }
    else
      printf("could not write....\n");
  }
  if (go_to_rx_mode)
  {
    if (radio.available())
    {
      char payload[32] = "";
      bool done = false;
      while (!done)
      {
        done = radio.read( payload , 32 );
        printf("HIM : %s\n", payload);
        go_to_serial_mode = true;
        go_to_rx_mode = false;
        delay(20);
      }
      radio.stopListening();
    }
  }
  if (go_to_serial_mode)
  {
    if (Serial.available())
    {
      int len = Serial.readBytesUntil('\n', input, 31);
      input[len] = '\0';
      go_to_tx_mode = true;
      go_to_serial_mode = false;
    }
  }
  // put your main code here, to run repeatedly:

}

This is a extremely simple program. You can experiment things like writing in the serial buffer when the node is in rx mode or what happens if the msg is not received if the radios are far apart or that the msg is larger than 31 bytes etc.

In the next post we will learn how to write a program where any of the nodes can initiate the chat and also it is resistant to mistimed inputs and large inputs.

Advertisements

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.

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