Hello World With NRF24L01 - arduino

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

Related

Arduino byte[] to string

Hello I am programming a bluetooth connection from Android Studio to Arduino. The connection works and it sends the String. I only get something like this tho on my Arduino (x?xx?xx??xxx?xxx?x) the questionmarks are the other way the string I send is on
This is my code:
#include <SoftwareSerial.h>
#define rxPort 11
#define txPort 10
char btData;
String string;
SoftwareSerial btSerial(rxPort, txPort);
void setup(){
Serial.begin(9600);
btSerial.begin(38400);
Serial.println("bluetooth available");
pinMode(LED_BUILTIN, OUTPUT);
}
void loop(){
if(btSerial.available()>0){
string = "";
}
while(btSerial.available()>0){
btData = (byte)btSerial.read();
if(btData==":"){
break;
}else{
string += btData;
}
delay(1);
Serial.println(string);
}
if(string == "on"){
digitalWrite(LED_BUILTIN,HIGH);
}
}
why do you cast the read to byte??
char it is :)
it should work with 9600 BAUD rate as well
example from my project (I didn't build String though - used single char as command)
SoftwareSerial blue(3, 5); // BlueTooth RX, TX;
void BluetoothSetup()
{
blue.begin(9600);
blue.print("AT+NAMEKuku"); // give it a name
delay(2000);
Serial.println("got BT");
}
void setup() {
....
BluetoothSetup();
....
}
void loop() {
if (blue.available()) {
char r = blue.read();
ProcessRemoteCommand(r);
}
}

Arduino interfacing with GPS gy Neo 6Mv2

I have written the following code. I want the string returned from the method
displayInfo( )
to be updated and printed just once, but the method sends strings repeatedly. If I copy the same code in void setup( ) function it is not printing any value.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS
SoftwareSerial ss(RXPin, TXPin);
String msg="";
String message="";
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop()
{
while (ss.available() > 0)
if (gps.encode(ss.read()))
message = displayInfo();
Serial.print(message);
}
String displayInfo()
{
if (gps.location.isValid())
{
String lati=String(gps.location.lat(), 3);
String logi=String(gps.location.lng(),3);
msg=lati+","+logi+"\n";
return(msg);
}
}
I have updated the code to recover some of the errors like function returning value with a global variable but it still does not provide me a single String value even after I had put the void loop( ) code in void setup( )
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
String msg="";
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
while (ss.available()>0)
if (gps.encode(ss.read()))
displayInfo();
Serial.print(msg);
}
void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
}
void displayInfo()
{
//Serial.print(F("Location: "));
if (gps.location.isValid())
{
String lati=String(gps.location.lat(), 3);
String longi=String(gps.location.lng(), 3);
msg="location: "+lati+","+longi+"\n";
}
else
{
msg=msg+"invalid";
}
}
EDIT:
The ss (SoftwareSerial) has a buffer wich contains the data that is ready to be send. ss.begin() will return 0 in your setup since the buffer is still empty, therefore the while loop will not be iterated even once.
The loop() function of the arduino works like a while so by placing the content of that while loop, and replacing the while with an if, you will be able to keep testing until you have a message in the buffer.
By adding a boolean to check if you have already sent a message, you can make sure only 1 message will be sent.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 12, TXPin = 13;
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
boolean sent = false;
void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop()
{
if (ss.available()>0 && sent == false){
if (gps.encode(ss.read())){
String msg = displayInfo();
if (msg != NULL){
Serial.print(msg);
sent = true;
}
}
}
}
String displayInfo()
{
if (gps.location.isValid())
{
String msg="";
String lati=String(gps.location.lat(), 3);
String logi=String(gps.location.lng(),3);
msg=lati+","+logi+"\n";
return(msg);
}
else{
return NULL;
}
}
By returning a NULL in the displayinfo()'s else statement, and testing for it within the loop(), you can ensure that you will only print a message when everything was working.

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");
}
}

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

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

Arduino: loop function runs only once

I'm trying to request temperatures from my DS18B20 sensor to post on plot.ly, but it seems my loop function is only running once; after connecting to plot.ly and creating the graph, the temperature is printed once in the serial monitor and does not seem to continue! Any help is greatly appreciated. Here is my code:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <plotly_streaming_cc3000.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define WLAN_SSID "wifi"
#define WLAN_PASS "********"
#define WLAN_SECURITY WLAN_SEC_WPA2
OneWire oneWire(10);
DallasTemperature sensors(&oneWire);
#define nTraces 1
char *tokens[nTraces] = {"token"};
plotly graph("username", "token", tokens, "filename", nTraces);
void wifi_connect(){
/* Initialise the module */
Serial.println(F("\n... Initializing..."));
if (!graph.cc3000.begin())
{
Serial.println(F("... Couldn't begin()! Check your wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
if (!graph.cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("... Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("... Request DHCP"));
while (!graph.cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
unsigned long aucDHCP = 14400;
unsigned long aucARP = 3600;
unsigned long aucKeepalive = 10;
unsigned long aucInactivity = 20;
if (netapp_timeout_values(&aucDHCP, &aucARP, &aucKeepalive, &aucInactivity) != 0) {
Serial.println("Error setting inactivity timeout!");
}
}
}
void setup() {
graph.maxpoints = 100;
Serial.begin(9600);
sensors.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
wifi_connect();
bool success;
success = graph.init();
if(!success){while(true){}}
graph.openStream();
}
void loop(void) {
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempFByIndex(0));
graph.plot(millis(), sensors.getTempFByIndex(0), tokens[0]);
delay(500);
}

Resources