radio.startListening(); // Switch to receive mode
A production-ready script must handle both alcance de arranque (initial connection) and alcance de GK (long-term stability). Below is a state machine script. Script RF24- alcance de arranque- alcance de GK...
// Compute RSSI (if using a module that reports it) uint8_t rssi = gateway.getRSSI(); // Requires custom function float distanceEstimate = estimateDistance(rssi); case CONNECTED: currentState = GK_KEEPALIVE
case CONNECTED: currentState = GK_KEEPALIVE; break; case GK_KEEPALIVE: if(millis() - lastGKPing > GK_INTERVAL) if(sendKeepAliveAndCheckRSSI()) lastGKPing = millis(); // Dynamically adjust data rate if RSSI is strong if(getRSSI() < 60) radio.setDataRate(RF24_1MBPS); // Faster else radio.setDataRate(RF24_250KBPS); // Longer range else currentState = STARTUP; // Lost connection break; case SLEEP: radio.powerDown(); delay(5000); radio.powerUp(); currentState = STARTUP; break; case GK_KEEPALIVE: if(millis() - lastGKPing >
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(7, 8); // CE, CSN pins
void loop() // Normal operation if(radio.available()) char rxBuf[33]; radio.read(&rxBuf, sizeof(rxBuf)); Serial.println(rxBuf);
void loop() switch(currentState) case STARTUP: // Send 5 beacon bursts, wait for ACK if(sendBeaconAndWaitAck()) currentState = CONNECTED; else delay(1000); // Retry startup