13. Networking and communications (Indonesia group)¶
This is group assignment page of Networking and communications (Indonesia Student) :
Group assignment:
- send a message between two projects
In this group assignment we will try to communicate between systems:
- The first system; ATTiny 3216
- second system: Arduino UNO
the two systems will be connected wirelessly using the 2.4G Wireless nRF24L01p module with PA and LNA
Test for Communication¶
For Transmiter system
/*
* Arduino Wireless Communication Tutorial
* Example 1 - Transmitter Code
* source code :
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 7); // CE, CSN
const byte address[6] = "00001";
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
delay(1000);
}
For Receiver :
/*
* Arduino Wireless Communication Tutorial
* Example 1 - Receiver Code
* source code :
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 7); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
}
Testing to controlled other system wireless¶
Source code for Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 7); // CE, CSN
const byte address[6] = "00001"; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
int button_pin = 5;
boolean button_state = 0;
void setup() {
pinMode(button_pin, INPUT);
radio.begin(); //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening(); //This sets the module as transmitter
}
void loop()
{
button_state = digitalRead(button_pin);
if(button_state == HIGH)
{
const char text[] = "Your Button State is HIGH";
radio.write(&text, sizeof(text)); //Sending the message to receiver
}
else
{
const char text[] = "Your Button State is LOW";
radio.write(&text, sizeof(text)); //Sending the message to receiver
}
radio.write(&button_state, sizeof(button_state)); //Sending the message to receiver
delay(1000);
}
Source Code for Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 7); // CE, CSN
const byte address[6] = "00001";
boolean button_state = 0;
int led_pin = 3;
void setup() {
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver
}
void loop()
{
if (radio.available()) //Looking for the data.
{
char text[32] = ""; //Saving the incoming data
radio.read(&text, sizeof(text)); //Reading the data
radio.read(&button_state, sizeof(button_state)); //Reading the data
if(button_state == HIGH)
{
digitalWrite(led_pin, HIGH);
Serial.println(text);
}
else
{
digitalWrite(led_pin, LOW);
Serial.println(text);}
}
delay(5);
}
Result:
Last update:
June 19, 2022