From 8df391e66f870f8e6c6fc8d1e96104536617a786 Mon Sep 17 00:00:00 2001 From: Simon Milvert Date: Sun, 21 Apr 2024 21:03:26 +0200 Subject: [PATCH] Added reciver and moved to sender --- .../lora_reciver}/include/README | 0 {receiver => reciver/lora_reciver}/lib/README | 0 reciver/lora_reciver/platformio.ini | 19 ++ reciver/lora_reciver/src/main.cpp | 200 ++++++++++++++++++ .../lora_reciver}/test/README | 0 sender/.gitignore | 5 + sender/.vscode/extensions.json | 10 + {receiver => sender}/README.md | 5 + {receiver => sender}/doc/board_pins.webp | Bin sender/include/README | 39 ++++ sender/lib/README | 46 ++++ {receiver => sender}/platformio.ini | 0 {receiver => sender}/src/main.cpp | 4 + sender/test/README | 11 + 14 files changed, 339 insertions(+) rename {receiver => reciver/lora_reciver}/include/README (100%) rename {receiver => reciver/lora_reciver}/lib/README (100%) create mode 100644 reciver/lora_reciver/platformio.ini create mode 100644 reciver/lora_reciver/src/main.cpp rename {receiver => reciver/lora_reciver}/test/README (100%) create mode 100644 sender/.gitignore create mode 100644 sender/.vscode/extensions.json rename {receiver => sender}/README.md (79%) rename {receiver => sender}/doc/board_pins.webp (100%) create mode 100644 sender/include/README create mode 100644 sender/lib/README rename {receiver => sender}/platformio.ini (100%) rename {receiver => sender}/src/main.cpp (99%) create mode 100644 sender/test/README diff --git a/receiver/include/README b/reciver/lora_reciver/include/README similarity index 100% rename from receiver/include/README rename to reciver/lora_reciver/include/README diff --git a/receiver/lib/README b/reciver/lora_reciver/lib/README similarity index 100% rename from receiver/lib/README rename to reciver/lora_reciver/lib/README diff --git a/reciver/lora_reciver/platformio.ini b/reciver/lora_reciver/platformio.ini new file mode 100644 index 0000000..203dbd0 --- /dev/null +++ b/reciver/lora_reciver/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:reciver] +platform = espressif32 +board = ttgo-lora32-v1 +framework = arduino +monitor_speed = 115200 +lib_deps = + thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.5.0 + sandeepmistry/LoRa@^0.8.0 + knolleary/PubSubClient@^2.8 \ No newline at end of file diff --git a/reciver/lora_reciver/src/main.cpp b/reciver/lora_reciver/src/main.cpp new file mode 100644 index 0000000..25ce2b0 --- /dev/null +++ b/reciver/lora_reciver/src/main.cpp @@ -0,0 +1,200 @@ +// OLED +#include // Only needed for Arduino 1.6.5 and earlier +#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"` + +// LORA +#include +#include + +// MQTT +#include +#include + + +SSD1306 display(0x3c, 4, 15); + +//OLED pins to ESP32 GPIOs via this connection: +//OLED_SDA GPIO4 +//OLED_SCL GPIO15 +//OLED_RST GPIO16 + + + +// WIFI_LoRa_32 ports + +// GPIO5 SX1278 SCK +// GPIO19 SX1278 MISO +// GPIO27 SX1278 MOSI +// GPIO18 SX1278 CS +// GPIO14 SX1278 RESET +// GPIO26 SX1278 IRQ(Interrupt Request) + +#define SS 18 +#define RST 14 +#define DI0 26 +// #define BAND 429E6 + +// LoRa Settings +#define BAND 434500000.00 +#define spreadingFactor 9 +// #define SignalBandwidth 62.5E3 +#define SignalBandwidth 31.25E3 + +#define codingRateDenominator 8 + + + +// WiFi Network Credentials +const char *ssid = "SoS IOT"; +const char *password = "godisr8n"; + +// MQTTCredentials +const char *MQTT_USER = "simon"; +const char *MQTT_PASS = "bajsa123"; + +IPAddress broker(10,0,0,3); // IP address of your MQTT broker eg. 192.168.1.50 +const char *ID = "esp_1"; // Name of our device, must be unique +const char *TOPIC = "test/sensor"; // Topic to subcribe to +WiFiClient wclient; + +PubSubClient client(wclient); // Setup MQTT client + +String data = ""; + +// Connect to WiFi network +void setup_wifi() { + Serial.print("\nConnecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); // Connect to network + + while (WiFi.status() != WL_CONNECTED) { // Wait for connection + delay(500); + Serial.print("."); + } + + Serial.println(); + Serial.println("WiFi connected"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); +} + +// Reconnect to client +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect(ID,MQTT_USER,MQTT_PASS)) { + Serial.println("connected"); + Serial.print("Publishing to: "); + Serial.println(TOPIC); + Serial.println('\n'); + + } else { + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + + + +// ADC? Battery voltage +// const uint8_t vbatPin = 34; +// float VBAT; // battery voltage from ESP32 ADC read + +void setup() { + pinMode(16,OUTPUT); + digitalWrite(16, LOW); // set GPIO16 low to reset OLED + delay(50); + digitalWrite(16, HIGH); + + display.init(); + display.flipScreenVertically(); + display.setFont(ArialMT_Plain_10); + display.setTextAlignment(TEXT_ALIGN_LEFT); + + Serial.begin(115200); + while (!Serial); //if just the the basic function, must connect to a computer + delay(1000); + + Serial.println("LoRa Receiver"); + display.drawString(5,5,"LoRa Receiver"); + display.display(); + SPI.begin(5,19,27,18); + LoRa.setPins(SS,RST,DI0); + +/* + pinMode(vbatPin, INPUT); + VBAT = (120.0/20.0) * (float)(analogRead(vbatPin)) / 1024.0; // LiPo battery voltage in volts + Serial.println("Vbat = "); Serial.print(VBAT); Serial.println(" Volts"); +*/ + + if (!LoRa.begin(BAND)) { + display.drawString(5,25,"Starting LoRa failed!"); + while (1); + } + Serial.println("LoRa Initial OK!"); + + Serial.print("LoRa Frequency: "); + Serial.println(BAND); + + Serial.print("LoRa Spreading Factor: "); + Serial.println(spreadingFactor); + LoRa.setSpreadingFactor(spreadingFactor); + + Serial.print("LoRa Signal Bandwidth: "); + Serial.println(SignalBandwidth); + LoRa.setSignalBandwidth(SignalBandwidth); + + LoRa.setCodingRate4(codingRateDenominator); + + display.drawString(5,25,"LoRa Initializing OK!"); + display.display(); + + setup_wifi(); // Connect to network + client.setServer(broker, 1883); +} + +void loop() { + if (!client.connected()) // Reconnect if connection is lost + { + reconnect(); + } + client.loop(); + // try to parse packet + int packetSize = LoRa.parsePacket(); + if (packetSize) { + // received a packets + Serial.print("Received packet. "); + + display.clear(); + display.setFont(ArialMT_Plain_16); + display.drawString(3, 0, "Received packet "); + display.display(); + + // read packet + while (LoRa.available()) { + data = LoRa.readString(); + Serial.print(data); + display.drawString(20,22, data); + display.display(); + } + + // print RSSI of packet + Serial.print(" with RSSI "); + Serial.println(LoRa.packetRssi()); + Serial.print(" with SNR "); + Serial.println(LoRa.packetSnr()); + // display.drawString(0, 45, "RSSI: "); + // display.drawString(50, 45, (String)LoRa.packetRssi()); + + display.drawString(0, 45, (String)LoRa.packetRssi() + "dB (" + (String)LoRa.packetSnr() +"dB)"); + + display.display(); + client.publish(TOPIC, data.c_str() ); + Serial.println((String)TOPIC + " => " + data + ";"); + } +} diff --git a/receiver/test/README b/reciver/lora_reciver/test/README similarity index 100% rename from receiver/test/README rename to reciver/lora_reciver/test/README diff --git a/sender/.gitignore b/sender/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/sender/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/sender/.vscode/extensions.json b/sender/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/sender/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/receiver/README.md b/sender/README.md similarity index 79% rename from receiver/README.md rename to sender/README.md index 210dfd6..64764a1 100644 --- a/receiver/README.md +++ b/sender/README.md @@ -5,3 +5,8 @@ https://www.banggood.com/2Pcs-LILYGO-TTGO-433-or-470MHz-SX1278-ESP32-LoRa-0_96-I #### PINS ![alt text](doc/board_pins.webp) +### Information + +Example of lora and oled: +https://hackaday.io/project/27791-esp32-lora-oled-module#j-discussions-title + diff --git a/receiver/doc/board_pins.webp b/sender/doc/board_pins.webp similarity index 100% rename from receiver/doc/board_pins.webp rename to sender/doc/board_pins.webp diff --git a/sender/include/README b/sender/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/sender/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/sender/lib/README b/sender/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/sender/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/receiver/platformio.ini b/sender/platformio.ini similarity index 100% rename from receiver/platformio.ini rename to sender/platformio.ini diff --git a/receiver/src/main.cpp b/sender/src/main.cpp similarity index 99% rename from receiver/src/main.cpp rename to sender/src/main.cpp index c58f4c7..07cdc92 100644 --- a/receiver/src/main.cpp +++ b/sender/src/main.cpp @@ -35,6 +35,10 @@ SSD1306 display(0x3c, 4, 15); #define preambleLength 8 #define codingRateDenominator 8 + + + + int counter = 0; void setup() { diff --git a/sender/test/README b/sender/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/sender/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html