Skip to content

Instantly share code, notes, and snippets.

@crcastle
Last active December 15, 2022 23:36
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save crcastle/93ebe3b42f3ab021639b to your computer and use it in GitHub Desktop.
Save crcastle/93ebe3b42f3ab021639b to your computer and use it in GitHub Desktop.
Trying to use NRF24L01+ on ESP8266, using the ESP8266 as MCU (i.e. not an Arduino or ATmega328). See https://github.com/esp8266/Arduino for details on initial setup of ESP8266 as Arduino-compatible MCU. The serial output below does not show expected messages: a) the full output shows up all at once then stops dead and b) the values for _salt, vo…
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define DEVICE_ID 2
#define CHANNEL 1 //MAX 127
RF24 radio(15, 15); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
// Topology
const uint64_t pipes[2] = { 0xFFFFFFFFFFLL, 0xCCCCCCCCCCLL };
struct dataStruct{
unsigned long _salt;
float temp;
int volt;
}sensor_data;
void setup() {
ESP.wdtDisable();
// ESP.wdtFeed();
Serial.begin(115200);
Serial.println("starting......");
restartRadio(); // turn on and configure radio
Serial.println("restarting radio");
radio.startListening();
Serial.println("Listening for sensor values...");
}
void loop() {
if (radio.available()) {
while (radio.available()) {
radio.read(&sensor_data, sizeof(sensor_data));
Serial.println("Got message:");
Serial.print("_salt: ");
Serial.println(sensor_data._salt);
Serial.print("volt: ");
Serial.println(sensor_data.volt);
Serial.print("temp: ");
Serial.println(sensor_data.temp);
Serial.println("-----------");
}
}
}
void restartRadio(){
radio.begin(); // Start up the radio
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate(RF24_250KBPS);
//radio.openReadingPipe(1,pipes[1]);
//radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[0]);
radio.openWritingPipe(pipes[1]);
radio.stopListening();
}
rl��r��c�n��� � ��p�<����8��ǒ��p  �nn��;�n���� � b�$�rrp�n��܀ ����l� � � b�n��n����� p ��nn�����l`���#�n�$��l`�`rn|��n����;`�n� �n��Ì�<~�n���l`nn��܀ �r��� � p p��<�����p ��nn����l`���#�n�rnr���;�� ?�l�;p�n��܀ �r��l�� p p��<�� ���8 ��nn��� r��#�n��;nr���;�� ?��8rr�ےn��r��`����(�SQS�(RQ�)HT�)SHHHC�dr�starting......
restarting radio
Listening for sensor values...
Got message:
_salt: 234884608
volt: 234881038
temp: 0.00
-----------
Got message:
_salt: 3584
volt: 921088
temp: 0.00
-----------
Got message:
_salt: 917504
volt: 235798542
temp: 0.00
-----------
Got message:
_salt: 235798542
volt: 917504
temp: 0.00
-----------
Got message:
_salt: 234884608
volt: 234884608
temp: 0.00
-----------
Got message:
_salt: 3584
volt: 234884608
temp: 0.00
-----------
Got message:
_salt: 0
volt: 234881038
temp: 0.00
-----------
Got message:
_salt: 917518
volt: 3584
temp: 0.00
-----------
Got message:
_salt: 234881024
volt: 0
temp: 0.00
-----------
Got message:
_salt: 0
volt: 14
temp: 0.00
-----------
Got message:
_salt: 28
volt: 0
temp: 0.00
-----------
Got message:
_salt: 0
volt: 0
temp: 0.00
-----------
Got message:
_salt: 0
volt: 3584
temp: 0.00
-----------
Got message:
_salt: 0
volt: 0
temp: 0.00
-----------
Got message:
_salt: 0
volt: 0
temp: 0.00
-----------
Got message:
_salt: 28
volt: 0
temp: 0.00
-----------
Got message:
_salt: 0
volt: 0
temp: 0.00
-----------
Got message:
_salt: 0
volt: 0
temp: 0.00
-----------
Got message:
_salt: 0
volt: 14
temp: 0.00
-----------
Got message:
_salt: 234881052
volt: 0
temp: 0.00
-----------
Got message:
_salt: 234884608
volt: 14
temp: 0.00
-----------
Got message:
_salt: 0
volt: 0
temp: 0.00
-----------
Got message:
_salt: 234881038
volt: 917518
temp: 0.00
-----------
Got message:
_salt: 917504
volt: 234884608
temp: 0.00
-----------
Got message:
_salt: 235802126
volt: 235
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "LowPower.h"
#include "printf.h"
#define POWER 9 //power pin to reduce consumption while sleeping
// TODO: store this in EEPROM
#define DEVICE_ID 1
#define CHANNEL 1 //MAX 127
RF24 radio(7, 8); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
int tempVal = 0; // Variable to store the temp value coming from the sensor.
int voltVal = 0; // Variable to store the calculated current voltage value.
// Topology
// FIXME: can this be stored in EEPROM?
const uint64_t pipes[2] = { 0xFFFFFFFFFFLL, 0xCCCCCCCCCCLL };
// Adjust this value to your board's specific internal BG voltage
// This needs to be accurate to set per board to be able to report current battery voltage
// TODO: store this in EEPROM
const long InternalReferenceVoltage = 1078;
// Uncomment this line if you are using the updated dallas_temp_library that
// supports the busFail() method to diagnose bus problems
// #define BUSFAIL
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 6
#define TEMPERATURE_PRECISION 9
#define TEMP_SENSOR_ID 0
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
struct dataStruct {
unsigned long _salt;
float temp;
int volt;
} data;
void setup() {
pinMode(POWER, OUTPUT);
Serial.begin(115200);
printf_begin();
printf("Sending sensor values every 8 seconds...\n\r");
restartRadio();
radio.printDetails();
data._salt = 0;
}
void loop() {
// get all sensor values and increment the salt so we have at least one unique value in the payload
data.volt = getBandgap();
printf("Voltage: %d\r\n", data.volt);
data.temp = getTemp(TEMP_SENSOR_ID);
char tempTemp[10];
dtostrf(data.temp, 4, 2, tempTemp);
printf("Temperature: %s\r\n", tempTemp);
data._salt++;
printf("Salt: %d\r\n", data._salt);
printf("------------------\r\n");
// send sensor data to gateway
restartRadio(); // turn on and configure radio
printf("Now sending %d byte payload... ", sizeof(data));
if (!radio.write(&data , sizeof(data) )) { // Send via radio
printf("failed.\n\r");
} else {
printf("sent.\n\r");
}
stopRadio(); // stop the radio
// Enter power down state for 8 s with ADC and BOD module disabled
Serial.println("Sleeping...");
Serial.flush();
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
printf("waking up...\r\n");
}
/*
* Get temperature
*/
float getTemp(int tempSensorId) {
#ifdef BUSFAIL
testBus();
#endif
// int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
float tempF;
// Reset the bus every time so we can see if any devices appear or fall off
sensors.begin();
sensors.requestTemperatures(); // Send the command to get temperatures
// Search the wire for address
if (sensors.getAddress(tempDeviceAddress, tempSensorId)) {
// get the temp in F
tempF = DallasTemperature::toFahrenheit(sensors.getTempC(tempDeviceAddress));
}
return tempF;
}
// From: http://gammon.com.au/power
// Code courtesy of "Coding Badly" and "Retrolefty" from the Arduino forum
// results are Vcc * 100
// So for example, 5V would be 500.
int getBandgap () {
// REFS0 : Selects AVcc external reference
// MUX3 MUX2 MUX1 : Selects 1.1V (VBG)
ADMUX = bit (REFS0) | bit (MUX3) | bit (MUX2) | bit (MUX1);
ADCSRA |= bit( ADSC ); // start conversion
while (ADCSRA & bit (ADSC))
{ } // wait for conversion to complete
int results = (((InternalReferenceVoltage * 1024) / ADC) + 5) / 10;
return results;
}
void stopRadio() {
radio.powerDown();
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(POWER, LOW); // remove power from nrf24l01+ module
}
void restartRadio() {
digitalWrite(POWER, HIGH); // provide power to nrf24l01+ module
radio.begin(); // Start up the radio
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, pipes[1]);
radio.openWritingPipe(pipes[0]);
// radio.openReadingPipe(1,pipes[0]);
// radio.openWritingPipe(pipes[1]);
radio.stopListening();
}
/********
* This tests the Dallas One-Wire bus
* Only works with the updated dallas_temp_library that supports the
* budFail() method to diagnose bus problems
********/
void testBus() {
#ifdef BUSFAIL
Serial.print(" Test:");
if (sensors.reset()) {
Serial.print("good");
} else {
if (sensors.busFail()) {
Serial.print("fail");
} else {
Serial.print("empty");
}
}
#endif
}
@crcastle
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment