Create a Private Chat Room using Arduino. How to?

Programming, Tutorial, Coding, New Design, and/or any project for your Arduino and Raspberry Pi can post your topic /suggestion here.
User avatar
Diy
Admin
Admin
Contact:
Location: Philippines
Posts: 185
Joined: Sat Apr 23, 2016 10:32 pm

Create a Private Chat Room using Arduino. How to?

Fri Jul 20, 2018 8:38 pm

Creating a Local Network to share personnel and confidential data's has become almost impossible for a common man in our modern world. This is mainly because all common chat methods like Whatsapp, Facebook, Hangout and almost everything involves an internet connection.
nrf24l01-with-arduino-nano-and-mega-for-chat-room.jpg
nrf24l01-with-arduino-nano-and-mega-for-chat-room.jpg (45.54 KiB) Viewed 1050 times
What if, we could share data without the medium of Internet?

How cool it would be if you could communicate with people inside your home or work place without a net pack or Internet Connection?

What if we could customize our chat-screen with our own imaginations?


All this is possible with a microcontroller and a Wireless transmission medium. This Arduino Chat Room using nRF24L01 Project will guide you on setting up a low cost Chat Room in your local area.
Arduino-Chatroom.jpg
Arduino-Chatroom.jpg (43.69 KiB) Viewed 1050 times
So let's jump in and see how it works.

Working Explanation:

Basically to make this thing work we will need a pair of Arduino boards and cheap wireless modules. The wireless modules that we will be using here are nRF24L01. The reason for choosing these modules is that these are the successors of Zigbee and is easy to work with a set up connection. Also these modules work on 2.4 GHz(ISM band) with frequency hopping spread spectrum and shock burst options which makes us feel relaxed of interference problems.

Our Arduino and NRF24L01 are connected together to establish a Serial communication so that they could talk to each other. The NRF24L01 are half duplex transceiver modules, hence they can send and receive data. The data is collected from the user and transmitted this data can be received by any (or one particular) modules and display it on their screen.

But!!!!! Are we going to chat using the Debug screen of Arduino? Of course not. We are going to built and customize our own chat screen with help of ‘Processing’. Processing is a software which is capable of communicating with the Arduino using UART. We will create a .exe file with Processing language, which could run on any computer with Java Runtime. In order to Chat we just have to plug in our Arduino and open this .exe file, and Booooom!! we are into our own Privatized totally free chat-Room.

This project is limited to just adding two members to the Chat room, But the nRF24L01 has 6 Pipelines, and so there could be a maximum of 6 members in our chat room. This char room can work within the 100 meter range depending upon the nRF24L01 Modules.

So let's go shopping!!!!

Components Required:
Arduino (any version or model) - 2Nos
nRF24L01+ Wireless Transceiver Module - 2Nos
3.3 Voltage Regulator - 2Nos. (Not mandatory)
Connecting wires
Interest (Lolz)

Circuit Diagram:

Arduino Mega with nRF24L01:
arduino-mega-and-nrf24l01-chat-room.png
arduino-mega-and-nrf24l01-chat-room.png (96.54 KiB) Viewed 1050 times
Arduino Nano with nRF24L01:​
arduino-nano-and-nrf24l01-chat-room.png
arduino-nano-and-nrf24l01-chat-room.png (50.46 KiB) Viewed 1050 times
Our project does not involve any complex connections. I have used an Arduino Mega and an Arduino Nano and their connections with nRF24L01 are shown above. You can use any Arduino Models.

Working with nRF24L01+ Wireless Transceiver Module:
However in order to make our nRF24L01 to work free from noise we might want to consider the following things. I have been working on this nRF24L01+ for a long time and learnt the following points can help you from getting hit on a wall.



1. Most of the nRF24L01+ modules in the market are fake. The cheap ones that we can find on Ebay and Amazon are the worst (Don’t worry, with few tweaks we can make them work)

2. The main problem is the power supply not your code. Most of the codes online will work properly, I myself have a working code which I personally tested, Let me know if you need them.

3. Pay attention because the modules which are printed as NRF24L01+ are actually Si24Ri (Yes a Chinese product).

4. The clone and fake modules will consume more power, hence do not develop your power circuit based on nRF24L01+ datasheet, because Si24Ri will have high current consumption about 250mA.

5. Beware of Voltage ripples and current surges, these modules are very sensitive and might easily burn up. (;-( fried up 2 modules so far)

6. Adding a couple capacitor ( 10uF and 0.1uF) across Vcc and Gnd of the module helps in making your supply pure and this works for most of the modules.

Still if you have problems report on comment section.

Programming the Arduinos:
The program for both Arduino Nano and Mega will be similar for the change in CE and CS pins. I will explain the program by splitting it into small segments.



Since the Arduino and nRF24L01 communicates through the SPI we have called for SPI library. We have also included our Maniacbug RF24 lib for our RF modules to work. Download the file from >>>> HERE, and add them to your IDE.
#include <SPI.h>
#include "RF24.h"
Our NRF modules are connected to pin 8 and 10 to CE and CS respectively.
RF24 myRadio (8, 10);
We create a structured data package called package. The variable text[20] will be use to transmit data on air.
struct package
{
char text[20]; //Text to transmit on air
};
typedef struct package Package;
Package data;
In the void setup() function, we initialize the baud rate to 9600 and setup our modules to 115 with MIN power consumption and 250KBPS speed. You can fiddle around with these values later.
void setup()
{
Serial.begin(9600);
delay(1000);
//Serial.print("Setup Initialized");
myRadio.begin();
myRadio.setChannel(115); //115 band above WIFI signals
myRadio.setPALevel(RF24_PA_MIN); //MIN power low rage
myRadio.setDataRate( RF24_250KBPS ) ; //Minimum speed
}
The module is made to work in transmit mode if Data is received through Serial buffer, else it will be in receiver mode looking for data on air. The data from user is stored in a char Array and sent to WriteData() to transmit them.
void loop()
{

while(Serial.available()>0) //Get values from user
{
val = Serial.peek();
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
if (val=='#')
{
strcpy( data.text, inData);
WriteData(); //Put module in Transmit mode
while (index!=0)
{
inData[index] = ' ';
index--;
}
}
}
ReadData(); //Put module Receive mode
}
void WriteData() function writes the data on 0xF0F0F0F0AA address, this address is used as writing pipe on other module.
void WriteData()
{
myRadio.stopListening(); //Stop Receiving and start transminitng
myRadio.openWritingPipe(0xF0F0F0F066);//Sends data on this 40-bit address
myRadio.write(&data, sizeof(data));
delay(300);
}
void ReadData() function writes the data on 0xF0F0F0F066 this address, this address is used as reading pipe on other module.
void ReadData()
{
myRadio.openReadingPipe(1, 0xF0F0F0F0AA); //Which pipe to read, 40 bit Address
myRadio.startListening(); //Stop Transminting and start Reveicing
if ( myRadio.available())
{
while (myRadio.available())
{
myRadio.read( &data, sizeof(data) );
}
Serial.println(data.text);
}
}
That’s it, our programming part is over. If you cannot understand few things here, check the two programs for both the Arduinos, given in the Code section below, I have added comment lines to explain things much better.

Processing Program:
‘Processing’ is open source software which is used by artists for Graphics designing. This software is used to develop software and Android applications. It is quite easy to develop and very much similar to the Android Development IDE. Hence I have shortened the explanation.

The Processing Code for both the Chat Screens is given here:

Chat Screen 1 Processing Code:

Code: Select all

import processing.serial.*; //Open serial Port

Serial port;

char[] inData = new char[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int val;

char[] getData = new char[100];
char KeyData;
int charPos=0;

String display;

void setup()
{
  size(510,500);
  port = new Serial(this,Serial.list()[4],9600); //Reads the 4th PORT at 9600 baudrate
  println(Serial.list());
   background(0);
}

void draw()
{
    
 if (port.available()>0)
  {
     if(index < 19) 
      {
          inChar = char(port.read()); 
          if (inChar == '#')
          inData[index] = ' ';
          else
          inData[index] = inChar; 
          index++; 
          inData[index] = ' ';  
      }
  }
  
  if (inChar == '#')
  {
 // println(str(inData));
printString();  
index=0;
port.clear();
}
}

void printString()
{
  fill(100);
  rect(0,height/2,width,height/2);
   int x=width/8, y= height -(height/4); 
   for(int j=0; j<20; j++)
   {
     fill(255);
   text(inData[j], x+j*10, y);
 }
}


void keyPressed() {
  int keyIndex = -1;
  if(key== ' ')
    {
      keyIndex=-33;
    }
    
  if (key >= 'A' && key <= 'Z') {
    keyIndex = key - 'A';
  } else if (key >= 'a' && key <= 'z') {
    keyIndex = key - 'a';
  }
  
  if (keyIndex == -1) 
  {
   // background(0);
    ToprintString();
    clearString();
    charPos=0;  
  } 
  
  else 
  { 
  //  background(0);
    fill(255);
    KeyData = char(keyIndex+65);
    if(keyIndex!=-1)
    {
    getData[charPos] = KeyData;
    charPos++;
    getData[charPos] = '#';
    }
  }
}

void ToprintString()
{
  
   fill(100,50,50);
  rect(0,0,width,height/2);
   int x=width/8, y= (height/4);  
   for(int j=0; j<=charPos; j++)
   {
     fill(255);
     if (j!=charPos)
   text(getData[j], x+j*10, y);
   print(getData[j]);
   port.write(getData[j]);
 }
}

void clearString()
{
  for (int reset=0; reset<=charPos; reset++)
  getData[reset] = ' ';

}
Chat Screen 2 Processing Code:

Code: Select all

import processing.serial.*;

Serial port;

char[] inData = new char[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int val;

char[] getData = new char[100];
char KeyData;
int charPos=0;

String display;

void setup()
{
  size(510,500);
  port = new Serial(this,Serial.list()[5],9600);
  println(Serial.list());
   background(0);
}

void draw()
{
    
 if (port.available()>0)
  {
     if(index < 19) 
      {
          inChar = char(port.read()); 
          if (inChar == '#')
          inData[index] = ' ';
          else
          inData[index] = inChar; 
          index++; 
          inData[index] = ' ';    
      }
  }
  
  if (inChar == '#')
  {
 // println(str(inData));
printString();  
index=0;
port.clear();
}
}

void printString()
{
  fill(100);
  rect(0,height/2,width,height/2);
   int x=width/8, y= height -(height/4);  
   for(int j=0; j<20; j++)
   {
     fill(255);
   text(inData[j], x+j*10, y);
 }
}


void keyPressed() {
  int keyIndex = -1;
  
  if(key== ' ')
    {
      keyIndex=-33;
    }
    
  if (key >= 'A' && key <= 'Z') {
    keyIndex = key - 'A';
  } else if (key >= 'a' && key <= 'z') {
    keyIndex = key - 'a';
  }
  
  if (keyIndex == -1) 
  {
    ToprintString();
    clearString();
    charPos=0;  
  } 
  
  else 
  { 
    fill(255);
    KeyData = char(keyIndex+65);
    if(keyIndex!=-1)
    {
    getData[charPos] = KeyData;
    charPos++;
    getData[charPos] = '#';
    }
  }
}

void ToprintString()
{
   fill(100,50,50);
  rect(0,0,width,height/2);
   int x=width/8, y= (height/4);  
   for(int j=0; j<=charPos; j++)
   {
     fill(255);
     if (j!=charPos)
   text(getData[j], x+j*10, y);
   print(getData[j]);
   port.write(getData[j]);
 }
}

void clearString()
{
  for (int reset=0; reset<=charPos; reset++)
  getData[reset] = ':';
}
Right click on them and click on ‘Save link as..’ to download them and open them in your computer after setting up the Arduinos. You need to install 'Processing' software to open these *.pde files and then 'Run' them to open the Chat Boxes. Comment section is open for queries. The Processing sketch for transmitter and Receiver module are identical.

In the below code section the "port = new Serial(this,Serial.list()[4],9600); //Reads the 4th PORT at 9600 baudrate" is important as it decides from which port to data from.
void setup()
{
size(510,500);
port = new Serial(this,Serial.list()[4],9600); //Reads the 4th PORT at 9600 baudrate
println(Serial.list());
background(0);
}
Here I have read data from the 4th port from my Arduino.

So for Example if you have COM[5] COM[2] COM[1] COM [7] COM[19]

Then the above code will read data from COM[7].

Testing:

Now since our Processing and Arduino sketch is ready, just upload the program to Arduino and leave it plugged into your Laptop. Open your Processing sketch and start typing and press "Enter" your message will be transmitted to the other Arduino which will display the received text on another Processing application connected to other computer.

So this how you can talk to your friends and family in your local area without having any Internet connection, using this inexpensive Arduino Chat Room.

Code:

Code: Select all

Code for Arduino Mega:

 

/**********
Arduino Mega with NRF24l01 for Chatroom project
Code by dohangout.com
on 7-2018
***********/

#include <SPI.h>  
#include "RF24.h"  // Manicbug LIB to be downloaded

RF24 myRadio (49, 53); // CE to 49 and 53 to CS
struct package
{
  char  text[20]; //Text to transmit on air
};

//byte addresses[][6] = {"0"}; 
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
 int val;

typedef struct package Package;
Package data;

void setup() 
{
  Serial.begin(9600);
  delay(1000);
  //Serial.print("Setup Initialized");
  myRadio.begin(); 
  myRadio.setChannel(115);  //115 band above WIFI signals
  myRadio.setPALevel(RF24_PA_MIN); //MIN power low rage
  myRadio.setDataRate( RF24_250KBPS ) ;  //Minimum speed
}

void loop()  
{
  
  while(Serial.available()>0) //Get values from user
  { 
    val = Serial.peek();
    if(index < 19) // One less than the size of the array
      {
          inChar = Serial.read(); // Read a character
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string
      }
     if (val=='#')
      { 
       strcpy( data.text, inData);
        WriteData(); //Put module in Transmit mode
    while (index!=0)
      {
      inData[index] = ' ';
      index--;
      }
      }
  }
ReadData(); //Put module Receive mode
}

void ReadData()
{
myRadio.openReadingPipe(1, 0xF0F0F0F0AA); //Which pipe to read, 40 bit Address
  myRadio.startListening(); //Sopt Transminting and start Reveicing 
  if ( myRadio.available()) 
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }
  // Serial.print("\nReceived:");
    Serial.println(data.text);
  }
}

void WriteData()
{
  myRadio.stopListening(); //Stop Receiving and start transminitng 
  myRadio.openWritingPipe(0xF0F0F0F066);//Sends data on this 40-bit address
  myRadio.write(&data, sizeof(data)); 
    //Serial.print("\nSent:");
    //Serial.println(data.msg);
  delay(300);
}

 

Code for Arduino Nano:

/**********
Arduino Nano with NRF24l01 for Chatroom project
Code by dohangout.com
on 7-2018
***********/

 

#include <SPI.h>  
#include "RF24.h"  // Manicbug LIB to be downloaded

RF24 myRadio (8, 10); // CE to 8 and 10 to CS

struct package
{
  char  text[20]; //Text to transmit on air
};

typedef struct package Package;
Package data;

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
 int val;

void setup()
{
  Serial.begin(9600);
  delay(1000);
  myRadio.begin();  
  myRadio.setChannel(115);  //115 band above WIFI signals
  myRadio.setPALevel(RF24_PA_MIN); //MIN power low rage
  myRadio.setDataRate( RF24_250KBPS ) ; //Minimum speed
  //Serial.print("Setup Initialized");
}

void loop()
{
  while(Serial.available()>0) //Get values from user
  { 
    val = Serial.peek();
    if(index < 19) // One less than the size of the array
      {
          inChar = Serial.read(); // Read a character
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          inData[index] = '\0'; // Null terminate the string
      }
     if (val=='#')
      { 
       strcpy( data.text, inData);
        WriteData(); //Put module in Transmit mode
   while (index!=0)
      {
      inData[index] = ' ';
      index--;
      }
      }
  }
 ReadData(); //Put module Receive mode
}

void WriteData()
{
  myRadio.stopListening(); //Stop Receiving and start transminitng 
  myRadio.openWritingPipe( 0xF0F0F0F0AA);  //Sends data on this 40-bit address
  myRadio.write(&data, sizeof(data)); 
//Serial.print("\nSent:");
//Serial.println(data.text);
  delay(300);
}

void ReadData()
{ 
myRadio.openReadingPipe(1, 0xF0F0F0F066); // Which pipe to read, 40 bit Address
  myRadio.startListening();  //Stop Transminting and start Reveicing 
  if ( myRadio.available()) 
  {
    while (myRadio.available())
    {
      myRadio.read( &data, sizeof(data) );
    }
    //Serial.print("\nReceived:");
    Serial.println(data.text);
  }
}


User avatar
Diy
Admin
Admin
Contact:
Location: Philippines
Posts: 185
Joined: Sat Apr 23, 2016 10:32 pm

Re: Create a Private Chat Room using Arduino. How to?

Sun Jul 22, 2018 3:25 pm

#repost from Facebook Group that will help to our builders:

"This chat room can work within the 100 meter range depending upon the nRF24L01 Modules."

No in reality because module produce high noise and power problems with stable 3.3V. You can connect a 3.3 uF to 10 uF capacitor directly on the module from +3.3V to Gnd (Some users say 10 uF in parallel with 0.1uF is best). If you are using the regular Arduino UNO which provides only 50 mA at 3.3V, you can fry regulator for 3.3V in time or you use stable 3.3V SPI adapter/regulator for nRF24L01.

Return to “ARDUINO AND RASPBERRY PI”

Links

In total there are 8 users online :: 0 registered, 0 hidden and 8 guests
Registered users: No registered users
Most users ever online was 156 on Sun Jun 17, 2018 7:42 am
Total posts 449
Total topics 328
Total members 62
Our newest member anonymous-kali
No birthdays today