Hi. I'm fairly new to Arduino IDE. I've encountered a roadblock with my project.
I'm making a proof-of-concept of a smart parking system, in which I'm using a total of 7 NRFs: 6 as Tx (5 w/ Nanos, 1 w/ an Uno R4 WiFi) and 1 as Rx (Raspi4). However, I'm doing Arduino to Arduino first to test the data transmission.
Every Tx Nano is configured with a switch. They're basically just dummy sensors. They only send "0" (vacant) or "1" (occupied) based on the state of the switch. I had no problem with this. They are working well together sending the dummy data to the Rx simultaneously consistently.
The problem is with the Tx attached to the Uno R4. It is configured with an ultrasonic sensor. With a basic code, the ultrasonic works and it sends data to a basic-coded Rx. However, since I have to send its data along with the 5 dummies', when I "combine" the codes, the transmission fails.
I've tried many solutions, incorporated fairly similar codes documented in the internet, YT tutorials, asked GPT for corrections. Nothing works. I don't know if the problem is with the Tx or Rx code or both.
For reference, below are the working codes of ultrasonic Tx and overall Rx.
//Ultrasonic Tx
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 10); // RX, TX
unsigned char data[4] = {};
float distance;
void setup() {
Serial.begin(57600);
mySerial.begin(9600);
}
void loop() {
int newValue;
do {
for (int i = 0; i < 4; i++) {
data[i] = mySerial.read();
}
} while (mySerial.read() == 0xff);
mySerial.flush();
if (data[0] == 0xff) {
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3]) {
distance = (data[1] << 8) + data[2];
if (distance > 400 || distance < 10) {
newValue = 0;
Serial.print("Occupancy: ");
Serial.println(newValue);
} else {
newValue = 1;
Serial.print("Occupancy: ");
Serial.println(newValue);
}
} else {
Serial.println("ERROR");
}
}
delay(500);
}
//Rx
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#define CE_PIN 7
#define CSN_PIN 8
// Instantiate an object for the nRF24L01 transceiver
RF24 radio(CE_PIN, CSN_PIN);
// Address and node number for the transmitters
uint64_t addresses[6] = { 0x7878787878LL, 0xB3B4B5B6F1LL, 0xB3B4B5B6CDLL, 0xB3B4B5B6A3LL, 0xB3B4B5B60FLL, 0xB3B4B5B605LL };
// Make a data structure to use as a payload.
struct PayloadStruct {
unsigned long nodeID;
bool switchStatus;
};
PayloadStruct payload;
void setup() {
Serial.begin(115200);
while (!Serial) {}
// Initialize the transceiver on the SPI bus
if (!radio.begin()) {
Serial.println(F("Radio hardware is not responding!!"));
while (1) {} // Hold in infinite loop
}
// Set the PA Level low to try preventing power supply related problems
radio.setPALevel(RF24_PA_LOW);
// Save on transmission time by setting the radio to only receive the
// number of bytes we need for the payload structure
radio.setPayloadSize(sizeof(payload));
// Set the addresses for the receiver pipes
for (uint8_t i = 0; i < 6; ++i)
radio.openReadingPipe(i, addresses[i]);
// Start listening for incoming transmissions
radio.startListening();
// Print introductory prompt
Serial.println(F("Receiver only"));
// Print details if needed
// printf_begin();
// radio.printDetails();
// radio.printPrettyDetails();
}
void loop() {
// Check if there is a payload available
if (radio.available()) {
// Read the payload from the transceiver
uint8_t bytes = radio.getPayloadSize();
radio.read(&payload, bytes);
// Print received payload information
Serial.print("Received from Node ");
Serial.print(payload.nodeID);
Serial.print(", switch status: ");
Serial.println(payload.switchStatus);
}
}
And then the failing overall Tx code.
//Overall Tx
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#include <SoftwareSerial.h>
#define CE_PIN 7
#define CSN_PIN 8
RF24 radio(CE_PIN, CSN_PIN);
SoftwareSerial mySerial(6, 10); // RX, TX
unsigned char data[4] = {};
float distance;
// Address and node number for the transmitter
uint64_t transmitterAddress = 0x7878787878LL; // Replace with desired transmitter address
// Make a data structure to use as a payload.
struct PayloadStruct {
unsigned long nodeID = 0; // Replace with desired transmitter node ID
bool switchStatus;
};
PayloadStruct payload;
void setup() {
Serial.begin(115200);
while (!Serial) {}
// Set up the sensor serial port
mySerial.begin(9600);
// Initialize the transceiver on the SPI bus
if (!radio.begin()) {
Serial.println(F("Radio hardware is not responding!!"));
while (1) {} // Hold in infinite loop
}
// Set the PA Level low to try preventing power supply related problems
radio.setPALevel(RF24_PA_LOW);
// Save on transmission time by setting the radio to only transmit the
// number of bytes we need to transmit a float
radio.setPayloadSize(sizeof(payload));
// Set the address for the transmitter
radio.openWritingPipe(transmitterAddress);
// Print introductory prompt
Serial.println(F("Transmitter for Channel 1"));
}
void loop() {
bool newValue;
do {
for (int i = 0; i < 4; i++) {
data[i] = mySerial.read();
}
} while (mySerial.read() == 0xff);
mySerial.flush();
if (data[0] == 0xff) {
int sum;
sum = (data[0] + data[1] + data[2]) & 0x00FF;
if (sum == data[3]) {
distance = (data[1] << 8) + data[2];
if (distance > 400 || distance < 10) {
newValue = 0;
} else {
newValue = 1;
}
} else {
Serial.println("ERROR");
}
}
// Update payload
payload.switchStatus = newValue; // Set switch status from ultrasonic sensor
Serial.print("Switch Status: ");
Serial.println(payload.switchStatus);
Serial.print("New Value: ");
Serial.println(newValue);
Serial.print("Distance: ");
Serial.println(distance);
// unsigned long start_timer = micros(); // Start the timer
bool report = radio.write(&payload, sizeof(payload)); // Transmit & save the report
// unsigned long end_timer = micros(); // End the timer
if (report) {
// Payload was delivered
Serial.print(F("Node "));
Serial.print(payload.nodeID); // Print nodeID
Serial.print(F(" successful!"));
Serial.print(F(" Switch status: "));
Serial.print(payload.switchStatus); // Print switch status
} else {
Serial.println(F("Transmission failed or timed out")); // Payload was not delivered
}
// Delay before next transmission */
delay(500); // 1 second delay
}
Any kind of help will be greatly appreciated. Thanks!