-
-
Save seco/00f36db74471a8251159 to your computer and use it in GitHub Desktop.
Arduino to RaspberryPI communication test with nRF24 chipset and libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __VIRIDI_NEXU_PAYLOAD__ | |
#define __VIRIDI_NEXU_PAYLOAD__ | |
enum PayloadType { | |
HERBA, METEO | |
}; | |
typedef uint8_t vn_payload_type; | |
typedef uint8_t vn_payload_version; | |
typedef struct { | |
int16_t humidity; | |
int16_t temperature; | |
int16_t pressure; | |
int16_t altitude; | |
int16_t luminosity; | |
} vn_meteo_t; | |
typedef struct { | |
int16_t moisture; | |
int16_t temperature; | |
} vn_plant_t; | |
struct Payload { | |
vn_payload_type type; | |
vn_payload_version version; | |
union { | |
vn_meteo_t meteo; | |
vn_plant_t plant; | |
} data; | |
}; | |
#endif // __VIRIDI_NEXU_PAYLOAD__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include <iostream> | |
#include "librf24/RF24.h" | |
#include "payload.h" | |
using namespace std; | |
// | |
// Hardware configuration | |
// | |
RF24 radio("/dev/spidev0.0", 8000000 , 25); //spi device, speed and CSN,only CSN is NEEDED in RPI | |
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL }; | |
Payload payload = Payload(); | |
// | |
// Setup | |
// | |
void setup(void) { | |
radio.begin(); | |
radio.setRetries(15, 15); | |
radio.setDataRate(RF24_250KBPS); | |
radio.setPALevel(RF24_PA_MAX); | |
radio.setPayloadSize(sizeof(payload)); | |
radio.openWritingPipe(pipes[1]); | |
radio.openReadingPipe(1, pipes[0]); | |
radio.startListening(); | |
radio.printDetails(); | |
} | |
// | |
// Loop | |
// | |
void loop(void) { | |
if (radio.available()) { | |
radio.read(&payload, sizeof(payload)); | |
printf("packet %d %d %d %d %d \n", payload.data.meteo.temperature, payload.data.meteo.humidity, payload.data.meteo.luminosity, payload.data.meteo.altitude, payload.data.meteo.pressure); | |
} | |
} | |
int main(int argc, char** argv) { | |
setup(); | |
while(1) | |
loop(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "payload.h" | |
#define SERIAL_DEBUG true | |
#include <SerialDebug.h> | |
#define LED_DEBUG true | |
#include <LedDebug.h> | |
#define SENSE_DELAY 2000 | |
Payload payload = (Payload) { METEO }; | |
#include <RF24Network.h> | |
#include <RF24.h> | |
#include <SPI.h> | |
uint32_t lastSense; | |
inline bool sense() { | |
long now = millis(); | |
if (now < lastSense || now - lastSense > SENSE_DELAY) { | |
payload.data.meteo.humidity = millis(); | |
payload.data.meteo.temperature = millis() * 50; | |
payload.data.meteo.pressure = millis(); | |
payload.data.meteo.altitude = millis(); | |
payload.data.meteo.luminosity = map(analogRead(A0), 0, 1024, 100, 0); | |
lastSense = now; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
#define RADIO_CE_PIN 9 | |
#define RADIO_CS_PIN 10 | |
#define RADIO_RETRY_DELAY 15 | |
#define RADIO_RETRY_COUNT 15 | |
RF24 radio = RF24(RADIO_CE_PIN, RADIO_CS_PIN); | |
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL }; | |
void setup() { | |
SERIAL_DEBUG_SETUP(9600); | |
pinMode(A0, INPUT); | |
radio.begin(); | |
radio.setRetries(RADIO_RETRY_DELAY, RADIO_RETRY_COUNT); | |
radio.setDataRate(RF24_250KBPS); | |
radio.setPALevel(RF24_PA_MAX); | |
radio.setPayloadSize(sizeof(payload)); | |
radio.openWritingPipe(pipes[0]); | |
radio.openReadingPipe(1,pipes[1]); | |
} | |
void loop() { | |
if (sense()) { | |
radio.powerUp(); | |
if (!radio.write(&payload, sizeof(payload))) { | |
PULSE(3,75); | |
} else { | |
PULSE(1,225); | |
} | |
radio.powerDown(); | |
DEBUG("payload", sizeof(payload), "enum", sizeof(payload.type)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment