communicating arduino to the other Arduino using RF24 getting the wrong result - arduino

I use these codes to transfer hello worlds but i just receive
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
in the receiver.
I don't understand what is the problem.
*******************Transmitter code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte rxAddr[6] = "00001";
void setup()
{
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(rxAddr);
radio.stopListening();
}
void loop()
{
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
delay(1000);
}
*****************receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8);
const byte rxAddr[6] = "00001";
void setup()
{
while (!Serial);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, rxAddr);
radio.startListening();
}
void loop()
{
if (radio.available())
{
char text[32] = {0};
radio.read(&text, sizeof(text));
Serial.println(text);
}
}

On the sending site you use:
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
On the receiver site:
char text[32] = {0};
radio.read(&text, sizeof(text));
Are the both sizeof(text) equals?
In the sending part text is 11 bytes
In the receiving part text is 32 bytes

Related

Can not get PN532 and mcp2515 to work together on arduino

I am trying to make the PN532 NFC reader module and mcp2515 to work together.
This is my code:
#include <SPI.h>
#include <mcp2515.h> //Library for using CAN Communication
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>
#define deviceAddress 0x42
struct can_frame canMsg;
MCP2515 mcp2515(10);
PN532_SPI pn532spi1(SPI, 5);
PN532_SPI pn532spi2(SPI, 6);
NfcAdapter nfc1 = NfcAdapter(pn532spi1);
NfcAdapter nfc2 = NfcAdapter(pn532spi2);
void setup(void) {
Serial.begin(9600);
Serial.println("Starting...");
mcp2515.reset();
mcp2515.setBitrate(CAN_10KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
delay(100);
nfc1.begin();
delay(100);
nfc2.begin();
}
void sendMessage(int data) {
canMsg.can_id = deviceAddress;
canMsg.can_dlc = 1;
canMsg.data[0] = data;
mcp2515.sendMessage(&canMsg);
}
void loop() {
if (nfc1.tagPresent())
{
Serial.println("Kattenhoofd");
NfcTag tag1 = nfc1.read();
if (tag1.getUidString() == "6C 1A D6 B9") {
Serial.println("YASSS!!!");
}
}
if (nfc2.tagPresent())
{
Serial.println("Konijnenvoet");
NfcTag tag2 = nfc2.read();
if (tag2.getUidString() == "59 07 CA 5C") {
Serial.println("YASSS!!!");
}
}
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
Serial.println("RECEIVED");
}
}
When I try this the two NFC readers only read the value once and then stop reading the tag.
Is there something wrong with my code? All the wiring is done correctly.
When I remove the mcp2515 and it initializations the nfc readers work as expected.

How to fix "aes128_enc_single' was not declared in this scope"

I am connecting a sensor to android device through arduino. I need to encrypt my sensor data and then send it to android device. But when I include aes128_enc_single(key, temp), It gives an error and said "aes128_enc_single' was not declared in this scope "
What should I do for this?
I have included AESLib.h library
My sample code is
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <AESLib.h>
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
SoftwareSerial blue(2,3);
int i = 0;
float temp = 0;
void setup() {
Serial.begin(9600);
blue.begin(9600);
}
void loop() {
i = analogRead(A0);
temp = (i/1024.0)*500;
Serial.println(temp);
aes128_enc_single(key, temp);
Serial.print("encrypted:");
Serial.println(temp);
blue.print("Encrypted Temperature: ");
blue.println(temp);
delay(1000);
}

Expected primary-expression before 'const'

I am trying to send one UDP packet but get stuck on concatenate between different data type and don't know how to solve it.
The code:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
const char* UdpIPaddress = "192.168.0.240";
unsigned int UdpPort = 4210;
const char* Udpmessage = "Message";
int a=0;
float Temp=29.25;
void setup() {
Serial.begin (115200);
}
void loop() {
if(a==0){
a = a + 1;
const char* sendmessage = const char*(a) + "=" + const char*(Temp) + "=" + Udpmessage;
UdpSend(sendmessage, UdpIPaddress, UdpPort);
}
}
void UdpSend(const char* message, const char * ipaddress, int port){
Serial.println("Send message: ");
Udp.beginPacket(ipaddress, port);
Udp.write(message);
Udp.endPacket();
}
Arduino IDE compiler error: expected primary-expression before 'const'
Please help...
Found the answer of my own question:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
const char* UdpIPaddress = "192.168.0.240";
unsigned int UdpPort = 4210;
char buf[40];
const char* Udpmessage = "Message";
int a=0;
float Temp=29.25;
void setup() {
Serial.begin (115200);
}
void loop() {
if(a==0){
a = a + 1;
sprintf(buf, "%d=%f=%s", a,Temp, Udpmessage);
UdpSend(buf, UdpIPaddress, UdpPort);
}
}
void UdpSend(const char* message, const char * ipaddress, int port){
Serial.println("Send message: ");
Udp.beginPacket(ipaddress, port);
Udp.write(message);
Udp.endPacket();
}
Works perfectly :) :) :)

Hello World With NRF24L01

I have Arduino and a Duinotech NRF24L01, I am trying to send the string "Hello world" with maniacs bug RF24 library however, I think it cannot detect the incoming RF signal.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
const uint64_t pipe = 0xF0F0F0F0E1L;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, pipe);
radio.startListening();
}
void loop() {
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
else {
Serial.println("Data was not found");
}
In the read code, it would always execute data was not found. This makes me think that maybe it does not find the RF signal at all.
Here is the code that writes the data.
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(7, 8); // CE, CSN
int text = 1;
const uint64_t pipe = 0xF0F0F0F0E1LL;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
radio.stopListening();
}
void loop() {
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
Try this
For the transmitter Instead of const uint64_t pipe = 0xF0F0F0F0E1LL; use const byte address[6] = "00001"; as the address and then have you void setup like the code below
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
Then ensure that you have a value set for the test variable to be transmitted as below
void loop() {
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
Serial.println("Sending Data");
delay(1000);
}
At the receiver end have this code running
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // 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); //This will print out the received value
}
}
PS: Ensure that all the connections are done to the right pins
AND you can test if the NRF24L01 chip is connected correctly by adding the code below
bool result = radio.isChipConnected ();
Serial.println (result);
it should print out a 1 to the serial monitor if the NRF24L01 chip is connected correctly

Simulating RSSI with Cheap RF Modules

My goal is to essentially spoof RSSI (Received Signal Strength Indicator) using a system of counting received packets. The idea is to have something where:
A specific number of packets is sent in a specific time from the transmitter.
Then are received at another unit and the number of packets received is counted.
The number in the counter of the receiver indicates the number of packets received at that time specific in the transmitter.
The fewer packages (counter value) that are received, the farther the sender will be.
I'm having a little trouble implementing the logic in my code however so I'd really appreciate the help. I am using Arduino Pro Mini 5V with NRF24L01+ radios and the RF24 Network library. My code is as follows:
Transmitter:
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <Wire.h>
RF24 radio(8,9);
RF24Network network(radio);
const uint16_t home_node = 00;
const uint16_t distant_node = 01;
struct payload_t { // Structure of our payload
byte ID;
};
void setup(void) {
Serial.begin(115200);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 92, /*node address*/ distant_node);
}
void loop(void) {
byte ID = 1;
for (int i = 0; i < 50; i++)
{
payload_t payload = {ID};
RF24NetworkHeader header(/*to node*/ home_node);
bool ok = network.write(header,&payload,sizeof(payload));
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
delay (300);
}
delay(15000);
}
Receiver:
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <Wire.h>
RF24 radio(8,9);
RF24Network network(radio);
const uint16_t home_node = 00;
const uint16_t distant_node = 01;
struct payload_t {
byte ID;
};
//const unsigned long interval = 3000;
//unsigned long last_sent;
int count = 0;
void setup(void)
{
Serial.begin(115200);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 92, /*node address*/ home_node);
}
void loop(void)
{
RF24NetworkHeader header;
payload_t payload;
network.update();
while ( network.available() ) { // Is there anything ready for us?
bool ok = network.read(header, &payload, sizeof(payload));
if (ok) // Non-blocking
{
count++;
Serial.println ("count=");
Serial.println (count);
}
else
Serial.println ("Failed");
}
}

Resources