Hello,
I have a question to radio communication.
My plan is, that a main module (radio transmitter, radio reciver, Nano Every, rellay) asks two other modules alternately wheather a sensor-threshold is exceeded. Depending on the recived data the other Arduinos should send back different codes. And the main Arduino should switch a rellay depending on the recived data. My problem is, that it doesn't work like it should.
Code of the main module:
// Hauptmodul / Main module
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
int timenextrequest = 0;
int wartezeit = 5000;
int bearbeitetwindow = 0;
int bearbeitetstove = 0;
int fensterkontakt = 1; //0 open
int wohnzimmerofen = 1; //0 cold
int kuechenofen = 0;
int relaypin = 4;
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(digitalPinToInterrupt(2)); // defines interrupt pin
mySwitch.enableTransmit(10); // Transmitterpin 10
request_stove_Livingroom(); // gets "timenextrequest" started
pinMode(relaypin, OUTPUT); // defines the rellaypin
}
void loop() {
if (mySwitch.available()) // Asks wheather code is avaylable
{
int value = mySwitch.getReceivedValue(); // Recived data is saved in value
if (value == 0) // If recived data = 0 Code is unknown
{
Serial.println("unknown code");
}
else // If the recived code is usable
{
Serial.print("Empfangen: ");
Serial.println(mySwitch.getReceivedValue()); //prints recived code in the serial monitor
if (mySwitch.getReceivedValue() == 458923) {
wohnzimmerofen = 0; //the livingroom stove is cold
}
if (mySwitch.getReceivedValue() == 568923) {
wohnzimmerofen = 1; //the livingroom stove is hot
}
if (mySwitch.getReceivedValue() == 254968) {
fensterkontakt = 0; //the window is open
}
if (mySwitch.getReceivedValue() == 658325) {
fensterkontakt = 1; // the window is closed
}
}
mySwitch.resetAvailable(); // the reciver is resettet over here
}
if (wohnzimmerofen == 0 && kuechenofen == 0){ //If al stoves are cold or if the window is open a rellay is switched on
digitalWrite(relaypin, HIGH);
}else{
if(fensterkontakt == 0){
digitalWrite(relaypin, HIGH);
}else{
digitalWrite(relaypin, LOW); //if something isn't like it "should" be the rellay is switched off
}
}
if (bearbeitetwindow == 0 && millis() >= timenextrequest) { //controls, wheather it's time to ask the "window-arduino"
bearbeitetwindow = 1; //secures, that the Arduinos are "asked" alternating
bearbeitetstove = 0; //secures, that the Arduinos are "asked" alternating
request_contact_Window(); //asks the window Arduino wheather the Window is open or closed
}
if (bearbeitetstove == 0 && millis() >= timenextrequest) { //controls, wheather it's time to ask the "stove-arduino"
bearbeitetwindow = 0; //secures, that the Arduinos are "asked" alternating
bearbeitetstove = 1; //secures, that the Arduinos are "asked" alternating
request_stove_Livingroom(); //asks the window Arduino wheather the stove is hot or cold
}
}
void request_stove_Livingroom() {
mySwitch.enableReceive(digitalPinToInterrupt(12)); // the reciver is resettet over here
mySwitch.send(137548, 24); // sends the noumber which is recived by the stove Arduino
mySwitch.enableReceive(digitalPinToInterrupt(2)); // changes the interrupt pin back
timenextrequest = millis() + wartezeit; // calculates the time for the next data package to be send
Serial.println("request_stove_Livingroom"); // tells the serial monitor, that data was send to the stove-arduino
}
void request_contact_Window() {
mySwitch.enableReceive(digitalPinToInterrupt(12)); // changes the interrupt pin to permitt, that the arduino recives his own signal
mySwitch.send(369846, 24); // sends the noumber which is recived by the stove Arduino
mySwitch.enableReceive(digitalPinToInterrupt(2)); // changes the interrupt pin back
timenextrequest = millis() + wartezeit; // calculates the time for the next data package to be send
Serial.println("request_contact_Window"); // tells the serial monitor, that data was send to the stove-arduino
}
Code of the Window module (Nano Every, Radio transmitter, reciver, sensor(at the moment a switch)):
// Fenstersensor / window modul
#include <RCSwitch.h>
int time = 0;
int wartezeit = 2000;
int sende = 0; // will contain the code which should be send depending on sensor data (next step)
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(digitalPinToInterrupt(2)); // defines interrupt pin
mySwitch.enableTransmit(10); // Transmitterpin 10
pinMode(4, INPUT); // switch to simulate a sensor (is going to be changed)
}
void loop() {
if (digitalRead(4) == 1) { //sends a code to the main module depending on sensor data (at the moment a switch)
Serial.println("got 1");
sende = 658325;
}
if (digitalRead(4) == 0) {
Serial.println("got 0");
sende = 254968;
}
if (mySwitch.available()) // Asks wheather code is avaylable
{
int value = mySwitch.getReceivedValue(); // Recived data is saved in value
if (value == 0) // If recived data = 0 Code is unknown
{
Serial.println("Unbekannter Code");
}
else // If the recived code is usable
{
Serial.print("Empfangen: ");
Serial.println(mySwitch.getReceivedValue()); //prints recived code in the serial monitor
if (mySwitch.getReceivedValue() == 369846) { // answers if the "conversation" was for him
senden();
}
}
mySwitch.resetAvailable(); // the reciver is resettet over here
}
}
void senden() {
mySwitch.enableReceive(digitalPinToInterrupt(12)); // changes the interrupt pin to permitt, that the arduino recives his own signal
mySwitch.send(sende, 24); // sends the noumber which is recived by the main arduino Arduino
mySwitch.enableReceive(digitalPinToInterrupt(2)); // changes the interrupt pin back
}
Code of the stovemodule (Nano Every, Radio transmitter, reciver, sensor(at the moment a switch)):
// Wohnzimmerofen / stove modul
#include <RCSwitch.h>
int sende = 0; // will contain the code which should be send depending on sensor data (next step)
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(digitalPinToInterrupt(2)); // defines interrupt pin
mySwitch.enableTransmit(10); // Transmitterpin 10
pinMode(4, INPUT); // switch to simulate a sensor (is going to be changed)
}
void loop() {
if (digitalRead(4) == 1) { //sends a code to the main module depending on sensor data (at the moment a switch)
Serial.println("got 1");
sende = 568923;
}
if (digitalRead(4) == 0) {
Serial.println("got 0");
sende = 458923;
}
if (mySwitch.available()) // Asks wheather code is avaylable
{
int value = mySwitch.getReceivedValue(); // Recived data is saved in value
if (value == 0) // If recived data = 0 Code is unknown
{
Serial.println("Unbekannter Code");
}
else // If the recived code is usable
{
Serial.print("Empfangen: ");
Serial.println(mySwitch.getReceivedValue()); //prints recived code in the serial monitor
if (mySwitch.getReceivedValue() == 137548) { // answers if the "conversation" was for him
senden();
}
}
mySwitch.resetAvailable(); // the reciver is resettet over here
}
}
void senden() {
mySwitch.enableReceive(digitalPinToInterrupt(12)); // changes the interrupt pin to permitt, that the arduino recives his own signal
mySwitch.send(sende, 24); // sends the noumber which is recived by the main arduino Arduino
mySwitch.enableReceive(digitalPinToInterrupt(2)); // changes the interrupt pin back
}
Output Serial monitors:
Output serial monitor main module:
18:27:20.209 -> request_stove_Livingroom
18:27:25.653 -> request_contact_Window
18:27:31.117 -> request_stove_Livingroom
18:27:36.605 -> request_contact_Window
18:27:42.043 -> request_stove_Livingroom
18:27:47.509 -> request_contact_Window
Output serial monitor other module:
18:27:05.978 -> gesendet
18:27:06.459 -> gesendet
18:27:07.450 -> Empfangen: 137548
18:27:07.528 -> Empfangen: 137548
18:27:07.607 -> Empfangen: 137548
18:27:07.732 -> Empfangen: 137548
18:27:12.920 -> Empfangen: 369846
18:27:12.983 -> Empfangen: 369846
18:27:13.108 -> Empfangen: 369846
18:27:13.201 -> Empfangen: 369846
18:27:18.349 -> Empfangen: 137548
18:27:18.487 -> Empfangen: 137548
I hope that somebody can help me fix it.
Thankyou and best reguards
Lukas