The overview.
The plan is to have a hand held controller (client) with a few buttons to change settings like setpoint, on/off, height, etc, and store them in "radioTxArray". It then transmits this array via nrf24L01 radio to the server which the replies with the actual rpm of the motor. This is then displayed on the lcd.
At the other end, the server receives the array and stores it as "radioRxArray" and the sets the setpoint for the motor and controls its rpm via a PID algorithm. (Big thankyou to zhomeslice for his invaluable input in to the motor control). It then sends the actual rpm to the client in reply. The server will also have a ping sensor, current sensor and relay attached.
I have two nrf24L01 radios that talk to each other and send the arrays as I expect using a modified version of the RadioHead nrf24 reliable datagram example. I replaced the delay with a timer and inserted an array with fixed data.
These two sketches seem to work well.
Client modified example.
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#define CLIENT_ADDRESS 1001
#define SERVER_ADDRESS 1002
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Singleton instance of the radio driver
RH_NRF24 driver(9, 10);
// RH_NRF24 driver(8, 7); // For RFM73 on Anarduino Mini
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
uint8_t radioTxArray[6];//radioTxArray[6];
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
int rpm;
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
if (!manager.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
radioTxArray[0] = 0;
radioTxArray[1] = 30;
radioTxArray[2] = 1;
radioTxArray[3] = 0;
radioTxArray[4] = 137;
radioTxArray[5] = 0;
}
void loop()
{
static unsigned long RadioTimer;
if ((unsigned long)(millis() - RadioTimer) >= 500) {
RadioTimer = millis();
Serial.println("Sending to nrf24_reliable_datagram_server");
// Send a message to manager_server
if (manager.sendtoWait(radioTxArray, sizeof(radioTxArray), SERVER_ADDRESS))
{
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reply from: 0x");
lcd.print(from, HEX);
//Serial.print("rpm= ");
//Serial.print(rpm);
//Serial.print(" 1= ");
//Serial.print(buf[1]);
rpm = (buf[0]);
lcd.setCursor(0, 1);
lcd.print(" RPM = ");
lcd.print(rpm);
}
else
{
lcd.clear();
lcd.print("No reply");
Serial.println("No reply, is nrf24_reliable_datagram_server running?");
}
}
else
Serial.println("sendtoWait failed");
//delay(500);
}
}
Server modified example.
// nrf24_reliable_datagram_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, reliable messaging server
// with the RHReliableDatagram class, using the RH_NRF24 driver to control a NRF24 radio.
// It is designed to work with the other example nrf24_reliable_datagram_client
// Tested on Uno with Sparkfun WRL-00691 NRF24L01 module
// Tested on Teensy with Sparkfun WRL-00691 NRF24L01 module
// Tested on Anarduino Mini (http://www.anarduino.com/mini/) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
#define CLIENT_ADDRESS 1001
#define SERVER_ADDRESS 1002
// Singleton instance of the radio driver
RH_NRF24 driver(7, 8);
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, SERVER_ADDRESS);
int rpm = 35;
int radioRxArray[6];
//int radioTxArray[2];
uint8_t radioTxArray[2];
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
void setup()
{
Serial.begin(9600);
if (!manager.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
radioTxArray[0] = rpm;
radioTxArray[1] = 1;
}
uint8_t data = rpm;
// Dont put this on the stack:
//uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
void loop()
{
static unsigned long RadioTimer;
if ((unsigned long)(millis() - RadioTimer) >= 500) {
RadioTimer = millis();
if (manager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
radioRxArray[0] = buf[0];
radioRxArray[1] = buf[1];
radioRxArray[2] = buf[2];
radioRxArray[3] = buf[3];
radioRxArray[4] = buf[4];
radioRxArray[5] = buf[5];
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": 0= ");
Serial.print(radioRxArray[0]);
Serial.print(": 1= ");
Serial.print(radioRxArray[1]);
Serial.print(": 2= ");
Serial.print(radioRxArray[2]);
Serial.print(": 3= ");
Serial.print(radioRxArray[3]);
Serial.print(": 4= ");
Serial.print(radioRxArray[4]);
Serial.print(": 5= ");
Serial.print(radioRxArray[5]);
Serial.print(" millis ");
Serial.println(millis());
// Send a reply back to the originator client
if (!manager.sendtoWait(radioTxArray, sizeof(radioTxArray), from))
Serial.println("sendtoWait failed");
}
}
// }
}
Next post |
/