I'm using two Arduinos with the rf24 modules.
I want to control a process with those, but for now I'm just checking the connection.
This is the code of the master:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
unsigned char msg[4];
unsigned char serial_msg[1];
String mensaje = "ii";
int CE = 9;
int CSN = 10;
RF24 radio(9,10);
//const uint64_t pipe = 0xE8E8F0F0E1LL;
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
bool done;
int incomingByte = 0;
String stringOne = "";
void setup(void){
Serial.begin(9600);
radio.begin();
radio.setRetries(15,15);
radio.openWritingPipe(pipes[1]);
}
void loop() {
radio.stopListening();
if (Serial.available() == 3) {
while(Serial.available() > 0)
{
incomingByte= Serial.read();
stringOne = String(stringOne + String((char)incomingByte));
}
Serial.println(mensaje);
incomingByte = Serial.read();
serial_msg[0] = incomingByte;
radio.write(stringOne.c_str() ,2);
}
stringOne = "";
radio.startListening(); // Volvemos a la escucha para recibir mas paquetes
delay(10); // necesario para activar el modo de esucha
if (radio.available())
done = false;
{
while (!done)
{
if (sizeof(msg)==4)
{
done = radio.read(msg,sizeof(msg) );
Serial.print("Estado: ");
Serial.println(msg[0]);
Serial.print("Voltaje: ");
Serial.print(msg[1]);
Serial.print(msg[2]);
Serial.println(msg[3]);
}
}
}
mensaje = "";
}
What t does is read data for the serial monitor, convert it to a string and them send the string.
The code of the receiver is this:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int CE = 6;
int CSN = 7;
RF24 radio(CE,CSN); /// ()
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
bool done = false;
int voltaje = 141;
String string_vol= "789";
enum states {
ESPERA,
ENCENDIDO,
PAUSA
};
enum states state = ESPERA;
unsigned char msg[2];
char incomingByte = '_';
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long interval = 1000;
void setup() {
Serial.begin(9600);
radio.begin();
radio.startListening();
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
void loop() {
string_vol= "789";
currentMillis = millis();
if ((currentMillis-previousMillis)>interval)
{
previousMillis = currentMillis;
msg[0] = char(state);
radio.stopListening(); // Dejamos d escuchar para poder hablar
string_vol = String(String((char)state) + string_vol );
radio.write(string_vol.c_str() ,4);
radio.startListening(); // Volvemos a la escucha para recibir mas paquetes
delay(10);
}
if (radio.available())
done = false;
{
while (!done)
{
done = radio.read(msg,sizeof(msg));
}
}
switch(state) {
case ESPERA:
if (msg[0] == 'i')
{
state = ENCENDIDO;
}
break;
case ENCENDIDO:
currentMillis = millis();
if (msg[0]=='r')
{
state = ESPERA;
}
else if (msg[0]=='d')
{
state = PAUSA;
}
break;
case PAUSA:
if (msg[0]=='r')
{
state = ESPERA;
}
else if (msg[0]=='c')
{
state = ENCENDIDO;
}
break;
}
}
This one acts as a state machine, and sends a string in this case:
string_vol= "789";
plus the actual state of the Arduino:
string_vol = String(String((char)state) + string_vol );
When this Arduino is in state one:
In the other side I got:
Estado: 1
Voltaje: 555657
When is in state II I got:
Estado: 2
Voltaje: 555657
But if it's in state zero, which is the initial one, I got:
Estado: 55
Voltaje: 56570
It change the position of the state for one of the chars of the string
EDIT.
The issue is when sending the '0' of the first state, I changed enum to:
enum states {
foo, // revisar, solo usada para comenzar en 1
ESPERA,
ENCENDIDO,
PAUSA
};
Now it works fine because the first state is '1', still don't now why it doesn't work if I use the '0'