Last active
July 8, 2022 01:54
-
-
Save jones2126/fd3a5d0ae8eac66f42afc491b2490573 to your computer and use it in GitHub Desktop.
Testing code for master/tractor/server to interact with slave/remote control/client using a TTGO board and LoRa communication. This code runs on the client which is meant to be a remote control module for the tractor/master.
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
/* Program: ttgo_adafruit_client_v2.ino | |
* a simple messaging client (receiver)with the RH_RF95 class. | |
*/ | |
#include <SPI.h> | |
#include <RH_RF95.h> | |
//Libraries for OLED Display | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
// LoRa pins for TTGO Semtech SX1276 ref: https://microcontrollerslab.com/ttgo-lora32-sx1276-oled-board-pinout-getting-started-with-arduino-ide/ | |
#define RFM95_MISO 19 | |
#define RFM95_MOSI 27 | |
#define RFM95_SCK 5 | |
#define RFM95_CS 18 // #define RFM95_CS 2 | |
#define RFM95_INT 26 // #define RFM95_INT 15 | |
#define RFM95_RST 14 // #define RFM95_RST 16 | |
#define LORA_BAND 915E6 //433E6 Asia; 866E6 Europe; 915E6 North America | |
//#define LORA_TxPower 20 // defaults to 17; 20 is highest | |
#define LORA_TxPower 5 // defaults to 17; 20 is highest | |
#define LORA_SyncWord 0xF1 // changes the sync word to match the receiver; ranges from 0-0xFF, default 0x34 | |
#define RF95_FREQ 915.0 // Change to 434.0 or other frequency, must match server's freq! | |
RH_RF95 rf95(RFM95_CS, RFM95_INT); // Instance of the radio driver | |
// change dataStruct -> RC_Struct and myData -> RC_Data | |
// add a line for struct RC_Struct RC_Data; see: https://techtutorialsx.com/2018/03/15/esp32-esp8266-arduino-using-structs/ | |
struct dataStruct{ | |
float pressure; | |
float humidity; | |
float TempF; | |
unsigned long counter; | |
}myData; | |
byte tx_buf[sizeof(myData)] = {0}; | |
// read the input on analog pin GIOP36: | |
#define POT_X 36 | |
#define POT_Y 37 | |
#define light_sensor 38 | |
int steering_val = analogRead(POT_X); | |
int throttle_val = analogRead(POT_Y); | |
//OLED pins | |
#define OLED_SDA 4 | |
#define OLED_SCL 15 | |
#define OLED_RST 16 | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 64 // OLED display height, in pixels | |
//BME280 Libraries and definitions | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#define BME280_SDA 21 | |
#define BME280_SCL 13 | |
bool bme280_status = false; | |
TwoWire I2Cone = TwoWire(1); | |
Adafruit_BME280 bme; | |
float bme_temperature = 0; | |
float bme_TempF = 0; | |
float bme_humidity = 0; | |
float bme_pressure = 0; | |
// Led definitions | |
#include <FastLED.h> | |
#define LED_NUM_LEDS 4 | |
#define LED_DATA_PIN 25 | |
#define LED_BRIGHTNESS 30 // 0 off, 255 highest | |
CRGB leds[LED_NUM_LEDS]; | |
#define LED 2 // On board LED to blink for feedback | |
void setup(){ | |
pinMode(LED, OUTPUT); | |
startSerial(); | |
startSignalLED(); | |
startOLED(); | |
startBME(); | |
startLoRA(); | |
// set initial values for tranmitting data | |
myData.pressure=1000.11; | |
myData.humidity=0.59; | |
myData.TempF=68.394; | |
myData.counter=1234; | |
} | |
void loop(){ | |
getWeatherReadings(); | |
getControlReadings(); | |
if (rf95.available()){ | |
// Should be a message for us now | |
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; | |
//uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; | |
//uint8_t len = sizeof(buf); | |
uint8_t buflen = sizeof(buf); | |
if (rf95.recv(buf, &buflen)){ | |
digitalWrite(LED, HIGH); | |
Serial.print("Got: "); | |
Serial.println((char*)buf); | |
Serial.print("RSSI: "); | |
Serial.println(rf95.lastRssi(), DEC); | |
//rf95.printBuffer("Got:", buf, buflen); | |
// Send a reply | |
memcpy(tx_buf, &myData, sizeof(myData) ); | |
byte zize=sizeof(myData); | |
rf95.send((uint8_t *)tx_buf, zize); | |
rf95.waitPacketSent(); | |
Serial.print("My reply, "); | |
Serial.print("Sent: "); | |
Serial.print(myData.pressure); | |
Serial.print(" ,"); | |
Serial.print(myData.humidity); | |
Serial.print(" ,"); | |
Serial.println(myData.TempF); | |
//Serial.println((char*)tx_buf); | |
digitalWrite(LED, LOW); | |
} | |
else{ | |
Serial.println("Receive failed"); | |
} | |
} | |
} | |
void getControlReadings(){ | |
steering_val = analogRead(POT_X); | |
throttle_val = analogRead(POT_Y); | |
} | |
void getWeatherReadings(){ | |
//light_val = analogRead(light_sensor); | |
bme_temperature = bme.readTemperature(); | |
bme_TempF = (bme_temperature*1.8)+32; | |
bme_humidity = bme.readHumidity(); | |
bme_pressure = bme.readPressure() / 100.0F; | |
} | |
void startSignalLED() { | |
FastLED.addLeds<NEOPIXEL, LED_DATA_PIN>(leds, LED_NUM_LEDS); | |
leds[0] = CRGB::Red; | |
leds[1] = CRGB::Green; | |
leds[2] = CRGB::Blue; | |
leds[3] = CRGB::Yellow; | |
FastLED.setBrightness(LED_BRIGHTNESS); | |
FastLED.show(); | |
} | |
void startSerial(){ | |
Serial.begin(115200); | |
if (!Serial) { | |
while(1) delay(1000); // loop forever and don't continue | |
} | |
Serial.println("running ttgo_adafruit_client_v2"); | |
} | |
void startOLED(){ | |
//reset OLED display via software | |
Serial.println("In startOLED"); | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST); | |
pinMode(OLED_RST, OUTPUT); | |
digitalWrite(OLED_RST, LOW); | |
delay(20); | |
digitalWrite(OLED_RST, HIGH); | |
Wire.begin(OLED_SDA, OLED_SCL); | |
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32 | |
Serial.println(F("SSD1306 allocation failed")); | |
while(1) delay(1000); // loop forever and don't continue | |
} | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setTextSize(1); | |
display.setCursor(0,0); | |
display.print("LORA SENDER"); | |
} | |
void startLoRA(){ | |
pinMode(RFM95_RST, OUTPUT); | |
digitalWrite(RFM95_RST, HIGH); | |
delay(100); | |
digitalWrite(RFM95_RST, LOW); | |
delay(10); | |
digitalWrite(RFM95_RST, HIGH); | |
delay(10); | |
SPI.begin(RFM95_SCK, RFM95_MISO, RFM95_MOSI, RFM95_CS); //SPI LoRa pins | |
while (!rf95.init()) { | |
Serial.println("LoRa radio init failed"); | |
Serial.println("Uncomment '#define SERIAL_DEBUG' in RH_RF95.cpp for detailed debug info"); | |
while (1); | |
} | |
Serial.println("LoRa radio init OK!"); | |
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM | |
if (!rf95.setFrequency(RF95_FREQ)) { | |
Serial.println("setFrequency failed"); | |
while (1); | |
} | |
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ); | |
// The default transmitter power is 13dBm, using PA_BOOST. | |
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then | |
// you can set transmitter powers from 5 to 23 dBm: | |
rf95.setTxPower(LORA_TxPower, false); | |
} | |
void startBME(){ | |
Serial.println("In startBME function"); | |
I2Cone.begin(BME280_SDA, BME280_SCL, 100000); | |
bme280_status = bme.begin(0x77, &I2Cone); //default 0x77; jump SDO to GND to change address to 0x76 | |
if (!bme280_status) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); | |
Serial.print("ID was: 0x"); Serial.println(bme.sensorID(),16); | |
while (1) delay(1000); // loop forever and don't continue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment