Teensy 3.6 + RF24L01
nRF24L01+PA+LNA Antenna Wireless Transceiver RF Transceiver Module. The 2.4GHz band is used.
Photo
Sketch
This file contains 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
// Teenst 3.6 and RF24 transmitter | |
// Copyright (C) 2021 https://www.roboticboat.uk | |
// da39af17-b65d-47e1-9c17-200fe6f049ed | |
// | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <https://www.gnu.org/licenses/>. | |
// These Terms shall be governed and construed in accordance with the laws of | |
// England and Wales, without regard to its conflict of law provisions. | |
#include <SPI.h> | |
#include "RF24.h" // Using RadioHead Library | |
#include <SD.h> | |
#include <ParallaxSIM33EAU.h> | |
#include <Navigation.h> | |
#define redLED 31 | |
#define greenLED 32 | |
// Create RF24 object | |
// Teensy 3.6 pin 6 = Reset pin (yellow) | |
// Teensy 3.6 pin 10 = CS pin (white) | |
// Teensy 3.6 pin 11 = MOSI pin (purple) | |
// Teensy 3.6 pin 12 = MISO pin (green) | |
// Teensy 3.6 pin 13 = SCK pin (blue) | |
RF24 radio(6, 10); | |
// WiFi messsge | |
char MessagePayload[40]; | |
char MessageSent[40]; | |
// nRF24L01+ | |
bool isnRF24L01plus = false; | |
bool signalLevel64dBm = false; | |
// Timer | |
unsigned long mytime; | |
// Let these addresses be used for the pair | |
// Addresses are 5 bytes = 40-bits | |
// Make sure only 1 transmitting on the pipe | |
// Make sure only 1 other listening on the pipe | |
uint8_t pipeName[6] = {"PipeG"}; | |
// GPS module | |
ParallaxSIM33EAU gps(&Serial1); | |
char startStatus = 'v'; | |
float dist = 0; | |
Navigation nav; | |
coordinate startPosition; | |
coordinate currentPosition; | |
int i = 0; | |
void setup() { | |
// Keep the User informed | |
Serial.begin(9600); | |
// GPS Module | |
Serial1.begin(9600); | |
// Give the Serial some time to setup | |
delay(100); | |
// LED pins | |
pinMode(redLED, OUTPUT); | |
pinMode(greenLED, OUTPUT); | |
// Initialise the transmitter | |
radio.begin(); | |
// Check if hardware is a nRF24L01+? | |
if (radio.isPVariant()){ | |
Serial.println("We have a nRF24L01+"); | |
isnRF24L01plus = true; | |
} | |
else{ | |
Serial.println("Not a nRF24L01+"); | |
} | |
// Transmission power level choices | |
// RF24_PA_HIGH | |
// RF24_PA_LOW | |
// RF24_PA_MIN | |
radio.setPALevel(RF24_PA_HIGH); | |
// Set data band rate | |
// RF24_2MBPS | |
// RF24_1MBPS | |
// RF24_250KBPS (slowest) | |
radio.setDataRate(RF24_250KBPS); | |
// Set communication channel (0-127) | |
radio.setChannel(108); | |
// The message length is variable length (max 32 bytes) | |
radio.enableDynamicPayloads(); | |
// Stop listening | |
radio.stopListening(); | |
// Set the TX address of the RX node into the TX pipe | |
// One pipe can be open at once only. | |
// Don't be listening whilst writing. | |
radio.openWritingPipe(pipeName); | |
// Select GPS sentences required | |
gps.SelectSentences(); | |
// Initialise the SD card | |
InitialiseSDcard(5); | |
} | |
void loop() { | |
// Do we have an initial GPS location | |
if (gps.gpsstatus == 'A') | |
{ | |
if (startStatus == 'v') | |
{ | |
// Store the first good reading | |
startStatus = 'A'; | |
startPosition.latitude = gps.latitude; | |
startPosition.longitude = gps.longitude; | |
} | |
else | |
{ | |
// Update current position | |
currentPosition.latitude = gps.latitude; | |
currentPosition.longitude = gps.longitude; | |
dist = nav.distance(currentPosition, startPosition); | |
} | |
} | |
// Timer | |
mytime = millis(); | |
// Create the text message | |
sprintf(MessagePayload,"MSG,%lu,%s",mytime,"Hello world"); | |
// Package the message | |
sprintf(MessageSent,"$%s*%02X",MessagePayload,MakeChecksum(MessagePayload)); | |
// Transmit message | |
// This blocks until the acknowledged by the Receiver | |
// Or max resends are reached | |
// Or max 60ms timeout | |
if (radio.write(&MessageSent, sizeof(MessageSent))) | |
{ | |
// Transmitted ok | |
digitalWrite(greenLED, HIGH); | |
Serial.print(dist); | |
Serial.print(",m,"); | |
Serial.print(MessageSent); | |
Serial.print(","); | |
Serial.print(gps.latitude,8); | |
Serial.print(","); | |
Serial.println(gps.longitude,8); | |
delay(100); | |
digitalWrite(greenLED, LOW); | |
File dataFile = SD.open("datalog.txt", FILE_WRITE); | |
if (dataFile) | |
{ | |
dataFile.print(mytime); | |
dataFile.print(","); | |
dataFile.print(dist); | |
dataFile.print(","); | |
dataFile.print(MessageSent); | |
dataFile.print(","); | |
dataFile.print(gps.latitude,8); | |
dataFile.print(","); | |
dataFile.print(gps.longitude,8); | |
dataFile.print(","); | |
dataFile.println("OK"); | |
dataFile.close(); | |
} | |
} | |
else | |
{ | |
// We had trouble transmitting | |
digitalWrite(redLED, HIGH); | |
Serial.print(dist); | |
Serial.print(",m,"); | |
Serial.print(F("Transmission ERROR")); | |
Serial.print(","); | |
Serial.print(gps.latitude,8); | |
Serial.print(","); | |
Serial.println(gps.longitude,8); | |
delay(100); | |
digitalWrite(redLED, LOW); | |
File dataFile = SD.open("datalog.txt", FILE_WRITE); | |
if (dataFile) | |
{ | |
dataFile.print(mytime); | |
dataFile.print(","); | |
dataFile.print(dist); | |
dataFile.print(","); | |
dataFile.print(MessageSent); | |
dataFile.print(","); | |
dataFile.print(gps.latitude,8); | |
dataFile.print(","); | |
dataFile.print(gps.longitude,8); | |
dataFile.print(","); | |
dataFile.println("ERROR"); | |
dataFile.close(); | |
} | |
} | |
// Wait 400ms | |
delay(400); | |
} | |
int MakeChecksum(char* msg){ | |
// Length of the message | |
int len = strlen(msg); | |
// Initialise the checksum | |
int cksum = 0; | |
// Loop over message characters | |
for (int i=0; i < len; i++) { | |
cksum ^= msg[i]; | |
} | |
return cksum % 0xffff; | |
} | |
void serialEvent1() | |
{ | |
//This event is called when Serial1 receives new bytes | |
gps.listen(); | |
} | |
void InitialiseSDcard(int testseconds) | |
{ | |
Serial.print("Initializing SD card..."); | |
if (!SD.begin(BUILTIN_SDCARD)) { | |
Serial.println("SD failed"); | |
for (i=1;i<=testseconds;i++) | |
{ | |
digitalWrite(13, HIGH); | |
delay(100); | |
digitalWrite(13, LOW); | |
delay(900); | |
} | |
return; | |
} | |
Serial.println("done."); | |
if (ReadWriteTest()){ | |
Serial.println("OK"); | |
digitalWrite(13, HIGH); | |
delay(testseconds * 1000); | |
digitalWrite(13, LOW); | |
} | |
else | |
{ | |
Serial.println("ERROR"); | |
for (i=1;i<=testseconds;i++) | |
{ | |
digitalWrite(13, HIGH); | |
delay(500); | |
digitalWrite(13, LOW); | |
delay(500); | |
} | |
} | |
} | |
bool ReadWriteTest() | |
{ | |
File myFile; | |
char filename[] = "testfile.txt"; | |
char writestring[] = "abcdefghijklmnopqrstuvwxyz1234567890"; | |
char readstring[40]; | |
// First remove the file is it already exists | |
if (SD.exists(filename)) { | |
SD.remove(filename); | |
} | |
// Open file to write | |
myFile = SD.open(filename, FILE_WRITE); | |
// If okay, write to the file | |
if (myFile) { | |
Serial.print("Writing to file... "); | |
myFile.print(writestring); | |
myFile.close(); | |
Serial.print('['); | |
Serial.print(writestring); | |
Serial.println("] done."); | |
} | |
else | |
{ | |
// Error writing to the file | |
Serial.println("error opening testfile.txt"); | |
} | |
// Open file to read. Which is the default option | |
myFile = SD.open(filename, FILE_READ); | |
if (myFile) { | |
Serial.print("Reading from file..."); | |
int n = 0; | |
while (myFile.available()) { | |
if (n<39) | |
{ | |
readstring[n] = myFile.read(); | |
readstring[n+1] = '\0'; | |
} | |
n=n+1; | |
} | |
myFile.close(); | |
Serial.print('['); | |
Serial.print(readstring); | |
Serial.println("] done."); | |
} | |
else | |
{ | |
// Error reading from the file | |
Serial.println("error opening testfile.txt"); | |
} | |
// Return true if the two char arrays are equal | |
if (strcmp(writestring, readstring) == 0){ | |
return true; | |
} | |
return false; | |
} |