Panel Cookies
Radio chat - code
Help me by sharing this post

Below you have the code for the radio chat module. The code is the same for both transmitter and receiver. So, just download it from below and upload it to both Arduinos and then we could make a test and see if it works and we could caht between the modules. But you will need some libraries as well. Below you will find those as well for the CC1101 radio module and the OLED display. So download those and install them to the Arduino IDE. Then, uplaod the code. You could dwonlaod the zip file with the code or just copy it from below.



schematic DIY radio caht with Arduino



/* Radio chat with full alphabet keyboard and the CC1101 radio mdoule
 *  Tutorial: https://electronoobs.com/eng_arduino_tut107.php
 *  Schematic: https://electronoobs.com/eng_arduino_tut107_sch1.php
 *  Code: https://electronoobs.com/eng_arduino_tut107_code1.php
 *  YouTube Channel: https://www.youtube.com/channel/UCjiVhIvGmRZixSzupD0sS9Q
 *  
// Arduino          CC1101
// GND              GND
// 3.3V             VCC
// 10               CSN/SS   **** Might need level shifted to 3.3V
// 11               SI/MOSI  **** Might need level shifted to 3.3V
// 12               SO/MISO
// 13               SCK      **** Might need level shifted to 3.3V
// 2                GD0

// Arduino          Keyboard
// GND              GND
// 5V               VCC
// Rx               Tx
// Tx               Rx

// Arduino          Oled Display
// GND              GND
// 5V               VCC
// A4               SDA
// A5               SCL

// 3                Buzzer
*/

//////////////////////////Libraries//////////////////////////
#include <ELECHOUSE_CC1101.h>     //Download it here: http://electronoobs.com/eng_arduino_ELECHOUSE_CC1101.php
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>         //download here: https://www.electronoobs.com/eng_arduino_Adafruit_GFX.php
#include <Adafruit_SSD1306.h>     //downlaod here: https://www.electronoobs.com/eng_arduino_Adafruit_SSD1306.php
#define OLED_RESET 5
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
int buzz = 3;



//////////////////////////Variables//////////////////////////
byte buffer[60] = "";
bool name_fixed = false;
int i = 0;
char inData_name[10];       // Allocate some space for the string
char inChar_name;           // Where to store the character read
byte index_name = 0;        // Index into array; where to store the character
int y = 0;
int name_length = 0;
bool sent = false;
char inData[70];            // 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
char received_data[60]; 
char text_to_send[70]; 
/////////////////////////////////////////////////////////////








void setup() {
  Serial.begin(115200);
  pinMode(buzz,OUTPUT);
  digitalWrite(buzz,LOW);
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32 or 64 from eBay)
  delay(100);

  //Set the size of the characters and print something...
  display.setTextSize(1); 
  display.setTextColor(WHITE);
  display.clearDisplay();
  display.setCursor(0,0);      
  display.print("Type name: "); //21 per line x 8 lines = 168        
  display.display();
  delay(100);

  //Start the radio module
  ELECHOUSE_cc1101.Init(F_433); // set frequency - F_433, F_868, F_965 MHz
  ELECHOUSE_cc1101.SetReceive();
  delay(100);

  //Stay in this while till the name is inserted
  while(!name_fixed)
  {
    if (Serial.available() > 0 ) 
    {
      inChar_name =  Serial.read();                
      name_length = name_length + 1;        //We will need the length of the name later


      if(inChar_name == '^')                //If the "{" is received, that means "SEND" button was pressed
      {
        inData_name[index_name - 1] = ' '; // Store it
        index_name--; // Increment where to write next   
        name_length--;
        display.clearDisplay();
        display.setCursor(0,0); 
        display.print("Type name: "); //21 per line x 8 lines = 168    
        display.print(inData_name); //21 per line x 8 lines = 168       
        display.display();
      }

      else if(inChar_name == '{')                //If the "{" is received, that means "SEND" button was pressed
      {
        name_fixed = true;

        inData_name[name_length-1] = ':';   //Add ": " after the name
        inData_name[name_length] = ' ';       
        
        //Print name was stored...
        display.clearDisplay();
        display.setCursor(0,0);      
        display.print("Name saved: "); //21 per line x 8 lines = 168    
        display.print(inData_name); //21 per line x 8 lines = 168    
        display.display();//Finally display the created image
        delay(1500);
        display.clearDisplay();
        display.setCursor(0,0);      
        display.print("Connected!"); //21 per line x 8 lines = 168           
        display.display();//Finally display the created image
        delay(1500);
        display.clearDisplay();
        display.setCursor(0,32);   
        display.print(received_data); //21 per line x 8 lines = 168 
        display.setCursor(0,0);   
        display.print("T: "); //21 per line x 8 lines = 168              
        display.display();
      }
      else
      {
        inData_name[index_name] = inChar_name; // Store it
        index_name++; // Increment where to write next   
        display.clearDisplay();
        display.setCursor(0,0); 
        display.print("Type name: "); //21 per line x 8 lines = 168    
        display.print(inData_name); //21 per line x 8 lines = 168       
        display.display();
      }      
    }//end serial available   
  }//end of !name fixed 
}//end of setup




void loop() {
  while(!sent)
  {
    if (Serial.available() > 0 ) 
    {
      inChar =  Serial.read();

      if(inChar == '^')                //If the "{" is received, that means "SEND" button was pressed
      {
        inData[index-1] = ' '; // Store it
        index--; // Increment where to write next
        display.clearDisplay();
        display.setCursor(0,0);  
        display.print("T: "); //21 per line x 8 lines = 168          
        display.print(inData); //21 per line x 8 lines = 168 
        display.setCursor(0,32);  
        display.print(received_data); //21 per line x 8 lines = 168     
        display.display();//Finally display the created image
      }
      
      else if(inChar == '{')
      {
        sent = true;
        y = 0;
        display.clearDisplay();
        display.setCursor(0,0); 
        display.print("T: "); //21 per line x 8 lines = 168   
        display.setCursor(0,32);  
        display.print(received_data); //21 per line x 8 lines = 168        
        display.display();
      }
      else
      {
        if(index < 60 - name_length - 1)
        {
          inData[index] = inChar; // Store it
          index++; // Increment where to write next
          display.setCursor(0,0);  
          display.print("T: "); //21 per line x 8 lines = 168          
          display.print(inData); //21 per line x 8 lines = 168    
          display.display();//Finally display the created image
        }
      }//end else      
    }//end serial available


    if (ELECHOUSE_cc1101.CheckReceiveFlag())
    {
      y=0;
      int len = ELECHOUSE_cc1101.ReceiveData(buffer);
      buffer[len] = '\0';    
      //Serial.print((char *) buffer); 

      while(y < 64)
      {
        received_data[y] = buffer[y];
        y = y + 1;
      }
      y=0;

      display.clearDisplay();    
      display.setCursor(0,0);  
      display.print("T: "); //21 per line x 8 lines = 168          
      display.print(inData); //21 per line x 8 lines = 168        
      display.setCursor(0,32);  
      display.print(received_data); //21 per line x 8 lines = 168        
      display.display();//Finally display the created image
      
      delay(1);
      
      ELECHOUSE_cc1101.SetReceive();

      digitalWrite(buzz,HIGH);
      delay(5);
      digitalWrite(buzz,LOW);
      delay(5);
      digitalWrite(buzz,HIGH);
      delay(5);
      digitalWrite(buzz,LOW);           
    }

    

    
  
  }//end sent

  if(sent)
  {
    y=0;
    //Join name and text to send together
    while(y < (64 + name_length))
    {
      if(y < name_length+1)
      {
        text_to_send[y] = inData_name[y];
      }      
      else
      {
        text_to_send[y] = inData[y-name_length-1];
      }     
      y = y + 1;
    }
    y = 0;
  
    
    //Now send the text
    ELECHOUSE_cc1101.SendData(text_to_send, 60); 
    delay(1);    
    ELECHOUSE_cc1101.SendData("\n", 2); 
    delay(1);       
    ELECHOUSE_cc1101.SetReceive();
    delay(5); 

    //Reset the inData and text_to_send by filling it wit '\0'
    while(y < 70)
    {
      inData[y] = '\0';
      text_to_send[y] = '\0';
      y = y + 1;
    }
    y = 0;
    sent = false;
    index = 0;    
  }//end of sent
}//end void loop








yt_link
insta_link
fb_link
twitter_link

Radio chat


Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel Intermedio