OK, I figured I sould get off the 'project guidance' thread since my problem seems more of a programming/interface issue...
I have been trying to make a simple sketch that reads data over the NRF24L01 and then display it in a web browser. For the purposes of sorting out how to do this, I created the sketch below, starting with SurferTim's WebServerST and adding the RF24 library to it.
Both seem to work perfect by themselves, but when run together it's not stable at all. The code runs for a while, for some random number of cycles, and then it quits.
By 'quits' I mean that the NRF24L01 quits returning data -- I get FFFF for everything I try to read from it. At leas 3 times I thought I had it licked and got it to work, but I kept finding it would not provided data from the remote NRF24L01 (which is just sitting there sending 32 bytes of data every 10-20 seconds or so. It's actually hooked up to a PIR sensor so I can wave my hand at it to get something sent to me. That code is essentially the RF24 library's 'ping pair' example with the PIR initiating the pings so I could fire them off on demand. (Pushbuttons are so 20th century, you know)
I've tried switching the NRF24L01 modules, changing the pins it uses for ce/cs, and a different UNO board. Still the problem persists.
So I'm hoping someone might have some recommendations on coding or configuration to truly resolve this. By all reckoning, this should 'just work'. My W5100 boards are of recent vintage and seem to have the inverter-fix incorporated for SS that handles the longstanding 'SPI bug'.
/*
Web server sketch for IDE v1.0.1 and w5100/w5200
Posted October 2012 by SurferTim
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8, 7);
uint64_t pipe = 0xF0F0F0F0E1LL;
uint16_t i = 0;
int data_rate = 0;
int payload_size;
bool tx_ok;
bool tx_fail;
bool rx_ready;
bool good = true;
// this must be unique
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
char payload[32];
// change to your network settings
IPAddress ip( 192,168,0,11 );
IPAddress gateway( 192,168,0,1 );
IPAddress subnet( 255,255,255,0 );
EthernetServer server(80);
void setup()
{
Serial.begin(9600);
// disable w5100 while setting up SD
// uncomment next 5 lines if using a microSD card
// pinMode(10,OUTPUT);
// digitalWrite(10,HIGH);
// Serial.print("Starting SD..");
// if(!SD.begin(4)) Serial.println("failed");
// else Serial.println("ok");
Ethernet.begin(mac, ip, gateway, gateway, subnet);
digitalWrite(10,HIGH);
delay(2000);
server.begin();
Serial.println("Ready");
radioOn();
// radio.printDetails();
}
void loop()
{
radio.whatHappened(tx_ok, tx_fail, rx_ready);
payload_size = radio.getDynamicPayloadSize();
EthernetClient client = server.available();
if(client && good) {
// radioOff();
boolean currentLineIsBlank = true;
boolean currentLineIsGet = true;
int tCount = 0;
char tBuf[64];
int r,t;
char *pch;
Serial.print("Client request: ");
while (client.connected()) {
while(client.available()) {
char c = client.read();
if(currentLineIsGet && tCount < 63)
{
tBuf[tCount] = c;
tCount++;
tBuf[tCount] = 0;
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response
Serial.println(tBuf);
Serial.print("POST data: ");
while(client.available()) Serial.write(client.read());
Serial.println();
pch = strtok(tBuf,"?");
while(pch != NULL)
{
if(strncmp(pch,"t=",2) == 0)
{
t = atoi(pch+2);
Serial.print("t=");
Serial.println(t,DEC);
}
if(strncmp(pch,"r=",2) == 0)
{
r = atoi(pch+2);
Serial.print("r=");
Serial.println(r,DEC);
}
pch = strtok(NULL,"& ");
}
Serial.println("Sending response");
client.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>");
client.print("<head><script type=\"text/javascript\">");
client.print("var timer = setInterval(function(){if (document.forms[0].t.value != '32') clearInterval(timer); else document.forms[0].submit()},3000);");
client.print("function show_alert() {return;alert(\"This is an alert\");}");
client.print("</script></head");
client.print("<body><H1>TEST</H1>");
// Send back the payload_size and millis(), just so we can see that the page has reloaded.
client.print("<form method=GET onSubmit=\"show_alert()\">T: <input type=text name=t value=\""); client.print(payload_size); client.print("\">
");
client.print("R: <input type=text name=r value=\""); client.print(millis()); client.print("\">
<input type=submit></form>");
data_rate = 0; // Clear it so we know if radio contines to set it
client.print("</body></html>\r\n\r\n");
client.stop();
}
else if (c == '\n') {
currentLineIsBlank = true;
currentLineIsGet = false;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
Serial.println("done");
// radioOn();
if (payload_size > 32) good = false;
}
if (good && millis() % 5000 == 0) {
// radio.printDetails();
radio.whatHappened(tx_ok, tx_fail, rx_ready);
payload_size = 0;
payload_size = radio.getDynamicPayloadSize();
Serial.println("Data Values Read ============================== "); // Try to read something to see if we get it or the radio has stopped talking
Serial.print(tx_ok);
Serial.print(", ");
Serial.print(tx_fail);
Serial.print(", ");
Serial.println(rx_ready);
Serial.print("Payload Size: ");
Serial.println(payload_size);
Serial.println(++i);
}
}
void radioOff() {
radio.stopListening();
// radio.powerDown();
Serial.println("Radio is off");
}
void radioOn() {
radio.begin();
radio.setRetries(1,1);
radio.setPayloadSize(32);
radio.openReadingPipe(1,pipe);
radio.startListening();
Serial.println("Radio ready");
}