Hi all,
I am trying to write a line of code the will make a cstring equal a word and another cstring’s value.
I have tried stuff like this:
loraSyncWord = ("0x", (receivedChars))
All of my code:
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
/*DEFINE THE PINS USED BY LORA TRANSCEIVER MODULE*/
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
/*SET THE CHANNEL AND FREQUENCY OF LORA TRANSCEIVER*/
#define BAND 915E6 //915MHz, E6 channel
/*DEFINE THE PINS USED BY THE OLED SCREEN*/
#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
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST); //WHAT DOES THIS DO?
char loraSyncWord[255];
char startLoRaSyncWord[] = "0x";
char myID[255];
const byte numChars = 255;
char receivedChars[numChars]; // an array to store the received data from the Serial Monitor
char loraDataChar[numChars]; //An Array to copy the loraData String contents to
String loraData; //Received LoRa data goes in this String
boolean newData = false;
void setup() {
/*INITIALIZE THE SERIAL MONITOR*/
Serial.begin(115200); //set the baud rate to 115200
/*RESET OLED DISPLAY VIA SOFTWARE*/
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
/*INIALIZE THE OLED DISPLAY*/
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32 OLED
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
/*SETUP THE OLED SCREEN AND DISPLAY SOME TEXT*/
display.clearDisplay(); //clear the OLED display
display.setTextColor(WHITE); //Set the text colour to white
display.setTextSize(1); //set the text size
display.setCursor(0, 0); //start at top left of OLED screen
display.print("LORA TRANSCEIVER "); //display title "LORA TRANSCEIVER"
display.display(); //WHAT DOES THIS DO?
/*DEFINE SOME PINS FOR OLED AND LORA MODULE*/
SPI.begin(SCK, MISO, MOSI, SS); //SPI LoRa pins
LoRa.setPins(SS, RST, DIO0); //setup LoRa transceiver module
LoRa.setSyncWord(0x45); //Set the "group" ID
/*START-UP LORA MODULE?*/
if (!LoRa.begin(BAND)) { //if LoRa does to begin 915E6?
Serial.println("Starting LoRa failed!"); //Serial.print
while (1);
}
/*SET THE LORA MODULES SPREADING FACTOR, SYNC WORD*/
LoRa.setSpreadingFactor(12); //LoRa spreading factor can range from 7 to 12 (Minimize bandwidth and maximize spreading factor to boost link budget. Maximize coding rate to boost reliability)
Serial.println("Please enter your ID");
while (Serial.available() < 1) {
}
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
strcpy(myID, receivedChars); //(char *)(strstr(receivedChars);
Serial.print ("ID received, your ID is: ");
Serial.println (myID);
}
}
Serial.println("Please enter your Group ID");
while (Serial.available() < 1) {
}
while (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
loraSyncWord = ("0x", (receivedChars))
LoRa.setSyncWord(0x87); // ranges from 0-0xFF, default 0x34, see API docs
Serial.print ("ID received, your ID is: ");
}
}
/*TELL USER THAT LORA SETUP WAS SUCCESSFUL VIA OLED SCREEN*/
display.setCursor(0, 10); //OLED screen second line far left
display.print("LoRa Initializing OK!"); //display on OLED screen
display.display(); //WHAT DOES THIS DO?
delay(2000);
}
void loop() {
display.clearDisplay(); //clear OLED screen
display.setCursor(0, 0); //start at top left of screen
display.println("LORA TRANSCEIVER"); //display text "LORA TRANSCEIVER"
display.display();
int packetSize = LoRa.parsePacket();
if (packetSize) {
//read packet
while (LoRa.available()) {
loraData = LoRa.readString();
}
//print RSSI of packet
int rssi = LoRa.packetRssi();
loraData.toCharArray(loraDataChar, numChars);
if (strchr(loraDataChar, '@') == NULL) {
Serial.println ("Message Received:");
Serial.println (loraDataChar);
/*DISPLAY ON OLED SCREEN THAT PACKET HAS BEEN RECEIVED*/
display.clearDisplay();
display.setCursor(0, 0);
display.print("MESSAGE RECEIVED");
display.setCursor(0, 55);
display.print("RSSI:");
display.print(rssi);
display.display();
delay (5000);
}
/*DISPLAY RECEIVED LORA PACKET TO YOU IF YOUR ID IS INCLUDED*/
if (strchr(loraDataChar, '@') != NULL) {
if (strstr(loraDataChar, myID)) { //if data received includes my ID
strcpy(loraDataChar, (char *)(strstr(loraDataChar, myID) + strlen(myID) + 1));
Serial.println ("Message Received:");
Serial.print ("*");
Serial.println (loraDataChar);
/*DISPLAY ON OLED SCREEN THAT PACKET HAS BEEN RECEIVED*/
display.clearDisplay();
display.setCursor(0, 0);
display.print("MESSAGE RECEIVED");
display.setCursor(0, 55);
display.print("RSSI:");
display.print(rssi);
display.display();
delay (5000);
}
}
}
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
LoRa.beginPacket(); //begin the LoRa packet
LoRa.print(receivedChars); //include the variiable "counter"
LoRa.endPacket(); //end and send the LoRa packet
newData = false;
Serial.println ("Message Sent:");
Serial.println (receivedChars);
display.clearDisplay(); //clear OLED screen
display.setCursor(0, 0); //start at top left of screen
display.println("MESSAGE SENT"); //display text "LORA SENDER"
display.display();
delay (5000);
}
}
How am I able to achieve this?
Thanks,
Zeb