Skip to content

Instantly share code, notes, and snippets.

@crcastle
Created July 13, 2015 01:14
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crcastle/6bab21445c857993d1d8 to your computer and use it in GitHub Desktop.
Save crcastle/6bab21445c857993d1d8 to your computer and use it in GitHub Desktop.
Resolution of issue with ESP8266 described in https://gist.github.com/crcastle/93ebe3b42f3ab021639b (see comments for detailed changes)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define DEVICE_ID 2
#define CHANNEL 1 //MAX 127
// SPI pin configuration figured out from here:
// http://d.av.id.au/blog/esp8266-hardware-spi-hspi-general-info-and-pinout/
RF24 radio(2, 15); // Set up nRF24L01 radio on SPI bus plus pins 2 for CE and 15 for CSN
// Topology
const uint64_t pipes[2] = { 0xFFFFFFFFFFLL, 0xCCCCCCCCCCLL };
struct dataStruct{
unsigned long _salt;
float temp;
int volt;
}sensor_data;
void setup() {
yield();
Serial.begin(115200);
Serial.setDebugOutput(true);
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()) {
yield();
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(){
yield();
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��#�n�� � ��p�<����8��ǒ��p  �nn��;�nĒ�� � b�$�rrp�n��܀ ����l� � � b�n��n�Ď��� p ��nn�����l`���#�n�$��l`�`rn|��n������l`9~�� ��#�n�$� ��nn���l`nn��� �r��� � p p��<�����p ��nn����l`���#�n�rnr���;��  ��;p�n��� �r���l�� p p��<���� �8 ��nn��� r��#�n�$�l`�`rn|��n���8rr�ےn���8r���(�SQS�(RQ�)HT�)SHHHC�dr�starting......
restarting radio
Listening for sensor values...
scandone
add 0
aid 7
pm open phy_2,type:2 0 0
cnt
connected with Turtles, channel 11
dhcp client start...
ip:192.168.0.21,mask:255.255.255.0,gw:192.168.0.1
Got message:
_salt: 41640
volt: 300
temp: 78.01
-----------
Got message:
_salt: 41641
volt: 300
temp: 78.01
-----------
#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

Changes to resolve https://gist.github.com/crcastle/6bab21445c857993d1d8:

  • Wiring issue: I left CE from nRF24L01+ unconnected and instantiated the RF24 radio object with RF24 radio(15, 15);. This is incorrect. Connected CE pin to GPIO2 and updated instantiation as RF24 radio(2, 15);.

Unresolved issues/changes:

  • In RF24.cpp, I had to change every call to printf_P to printf.
  • radio.printDetails(); crashes the ESP8266. I don't know why. This might be related to the above point or some other issue with Program.
  • In RF24_config.h, I had to change #include <avr/pgmspace.h> to #include <pgmspace.h>

@rmhomecouk
Copy link

Thank you for this.

@nomanahmed3
Copy link

sir which esp module are you using?...i am trying to make this work with esp12e and nrf24 for days with no success...you used rf24(2,15) call fro ce and csn pins but i have seen that gpio15 should be pulled down and gpio2 to be pulled up fro normal operation of esp12e...can i change these pins? and what about CS0 pin on the HSPI header pin in given link?

http://www.electrodragon.com/product/esp-12e-esp8266-wifi-board/#

@gc61
Copy link

gc61 commented Feb 26, 2019

Hello, I apologize, but the tx module uses an Arduino or an esp8266 (NodeMcu)? from the use of the pins you have inserted it seems to be referred to an Ardunio.
I can not make nRF24L01 work together with NodeMcu

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