Skip to content

Instantly share code, notes, and snippets.

@crcastle
Created September 23, 2015 06:38
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save crcastle/9dd9dcad86eee7e5b4a2 to your computer and use it in GitHub Desktop.
Save crcastle/9dd9dcad86eee7e5b4a2 to your computer and use it in GitHub Desktop.
Using an Adafruit Huzzah ESP8266 with nRF24L01+ as a (one-way) internet gateway for several nRF24L01+ sensor nodes
#include <SPI.h>
#include <RF24Network.h>
#include <RF24.h>
#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "password";
const char* host = "coolapp.herokuapp.com";
// 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 & 15
RF24Network network(radio); // Network uses that radio
// TODO: store this in EEPROM
const uint16_t this_node = 0; // Address of this node (the gateway node)
struct dataStruct{
uint16_t _salt; // 2 bytes
uint16_t clientId; // 2 bytes
char location[4]; // 4 bytes
char room[4]; // 4 bytes
uint16_t sensorId; // 2 bytes
int16_t temp; // 2 bytes
uint16_t humidity; // 2 bytes
uint16_t volt; // 2 bytes
}sensor_data;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
connectToWifi();
yield();
Serial.println("starting......");
restartRadio(); // turn on and configure radio
// TODO: remove debugging used to fix payload being too large
Serial.print("Size of salt: ");Serial.println(sizeof(sensor_data._salt));
Serial.print("Size of clientId: ");Serial.println(sizeof(sensor_data.clientId));
Serial.print("Size of location: ");Serial.println(sizeof(sensor_data.location));
Serial.print("Size of room: ");Serial.println(sizeof(sensor_data.room));
Serial.print("Size of sensorId: ");Serial.println(sizeof(sensor_data.sensorId));
Serial.print("Size of temp: ");Serial.println(sizeof(sensor_data.temp));
Serial.print("Size of humidity: ");Serial.println(sizeof(sensor_data.humidity));
Serial.print("Size of volt: ");Serial.println(sizeof(sensor_data.volt));
Serial.print("Size of struct: ");Serial.println(sizeof(sensor_data));
Serial.println("Listening for sensor values...");
}
void loop() {
network.update();
while (network.available()) {
yield(); // give wifi functionality some time to do its operations
RF24NetworkHeader header;
//network.read(header, &sensor_data, radio.getDynamicPayloadSize());
// explicitly set payload size
network.read(header,&sensor_data,sizeof(sensor_data));
// TODO: remove debug
Serial.print("Got message: ");Serial.println(sizeof(sensor_data));
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.print("humidity: ");Serial.println(sensor_data.humidity);
Serial.print("clientId: ");Serial.println(sensor_data.clientId);
Serial.print("location: ");Serial.println(sensor_data.location);
Serial.print("room: ");Serial.println(sensor_data.room);
Serial.print("sensorId: ");Serial.println(sensor_data.sensorId);
Serial.println("-----------");
Serial.flush();
delay(3000);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
char url[6] = "/data";
char tempQueryString[500];
sprintf(tempQueryString, "?message=%i/%s/%s/%i/temp_%i,%i/%s/%s/%i/humidity_%i,%i/%s/%s/%i/voltage_%i", sensor_data.clientId,
sensor_data.location,
sensor_data.room,
sensor_data.sensorId,
sensor_data.temp,
sensor_data.clientId,
sensor_data.location,
sensor_data.room,
sensor_data.sensorId,
sensor_data.humidity,
sensor_data.clientId,
sensor_data.location,
sensor_data.room,
sensor_data.sensorId,
sensor_data.volt);
Serial.print("Requesting URL: ");
Serial.print(url);
Serial.println(tempQueryString);
// This will send the request to the server
client.print(String("GET ") + url + tempQueryString + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// delay until we are likely to have received a response from the server.
// if there's been no response after this delay, the following while
// loop will be skipped.
delay(500);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println("===========");
Serial.println("");
}
}
void connectToWifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void restartRadio(){
yield(); // give wifi functionality some time to do its operations
radio.begin(); // Start up the radio
network.begin(/*channel*/ 90, /*node address*/ this_node);
// 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();
network.update(); // always be pumping the network
}
@pooh25
Copy link

pooh25 commented Jul 31, 2016

the code shows error #include<avr/pgmspace.h> not available even after downloading the file and putting it up in library

@bolawole
Copy link

the code shows #include <avr/pgmspace.h> error. please help

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