So this is the server code (the ESP32 that converts the LoRa to Wifi and back for the VB application)
#include <WiFi.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
const char* ssid = "TESTNET";
#define SCK 5 // GPIO5 -- SX1278's SCK
#define MISO 19 // GPIO19 -- SX1278's MISO
#define MOSI 27 // GPIO27 -- SX1278's MOSI
#define SS 18 // GPIO18 -- SX1278's CS
#define RST 14 // GPIO14 -- SX1278's RESET
#define DI0 26 // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND 868E6
String currentLine = "";
String rssi = "RSSI --";
String rssiD;
String packSize = "--";
String packet ;
int msize;
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
WiFiServer wifiServer(8000);
void setup() {
Serial.begin(115200);
delay(1000);
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Connecting to WiFi...");
display.setCursor(0, 20);
display.println("SSID:");
display.setCursor(0, 30);
display.println(ssid);
display.display();
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
SPI.begin(SCK, MISO, MOSI, SS);
LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(868E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
wifiServer.begin();
//display.flipScreenVertically();
//display.setFont(ArialMT_Plain_10);
LoRa.onReceive(cbk);
LoRa.receive();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Server IP:");
display.setCursor(0, 10);
display.println(WiFi.localIP());
display.display();
}
void loop() {
int packetSize = LoRa.parsePacket();
msize = LoRa.parsePacket();
if (packetSize) {
cbk(packetSize);
}
delay(10);
WiFiClient client = wifiServer.available();
if (client) {
while (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar;
while (client.available()) {
delay(3); //delay to allow buffer to fill
if (client.available() > 0) {
char inChar = client.read(); //gets one byte from serial buffer
currentLine += inChar; //makes the string readString
}
}
Serial.println("VB Command - " + currentLine);
if (inChar == '\n') {
currentLine = "";
}
if ( currentLine.startsWith("GET_STATUS")) {
delay(25);
client.println("DEVICE_OK");
LoRa.beginPacket();
LoRa.print("GET_STATUS");
LoRa.endPacket();
currentLine = "";
LoRa.receive();
} else if ( currentLine.startsWith("SET_RED=")) {
delay(25);
LoRa.beginPacket();
LoRa.print(currentLine);
LoRa.endPacket();
currentLine = "";
LoRa.receive();
} else if ( currentLine.startsWith("SET_GREEN=")) {
delay(25);
LoRa.beginPacket();
LoRa.print(currentLine);
LoRa.endPacket();
currentLine = "";
LoRa.receive();
} else if ( currentLine.startsWith("SET_BLUE=")) {
delay(25);
LoRa.beginPacket();
LoRa.print(currentLine);
LoRa.endPacket();
currentLine = "";
LoRa.receive();
} else if ( currentLine.startsWith("SET_BRIGHT=")) {
delay(25);
LoRa.beginPacket();
LoRa.print(currentLine);
LoRa.endPacket();
currentLine = "";
LoRa.receive();
}
Serial.println("LoRa command - " + packet);
if ( packet.startsWith("LORA_OK")) {
delay(25);
client.println("LORA_RSSI=" + rssiD);
packet = "";
} else if ( packet.startsWith("RED_OK")) {
Serial.println("RED_OK");
delay(25);
client.println("RED_OK");
packet = "";
} else if ( packet.startsWith("GREEN_OK")) {
Serial.println("GREEN_OK");
delay(25);
client.println("GREEN_OK");
packet = "";
} else if ( packet.startsWith("BLUE_OK")) {
Serial.println("BLUE_OK");
delay(25);
client.println("BLUE_OK");
packet = "";
} else if ( packet.startsWith("BRIGHT_OK")) {
Serial.println("BRIGHT_OK");
delay(25);
client.println("BRIGHT_OK");
packet = "";
} else {
Serial.println("Unknown LoRa Command - " + packet);
}
}
}
client.stop();
Serial.println("Client disconnected");
}
}
void cbk(int packetSize) {
packet = "";
packSize = String(packetSize, DEC);
for (int i = 0; i < packetSize; i++) {
packet += (char) LoRa.read();
}
rssi = "RSSI " + String(LoRa.packetRssi(), DEC) ;
rssiD = String(LoRa.packetRssi(), DEC);
//Serial.println("LoRa Data = " + packet);
}