Added reciver and moved to sender

This commit is contained in:
Simon 2024-04-21 21:03:26 +02:00
parent cff059d952
commit 8df391e66f
14 changed files with 339 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,200 @@
// OLED
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
// LORA
#include <SPI.h>
#include <LoRa.h>
// MQTT
#include <WiFi.h>
#include <PubSubClient.h>
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 + ";");
}
}

5
sender/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
sender/.vscode/extensions.json vendored Normal file
View File

@ -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"
]
}

View File

@ -5,3 +5,8 @@ https://www.banggood.com/2Pcs-LILYGO-TTGO-433-or-470MHz-SX1278-ESP32-LoRa-0_96-I
#### PINS #### PINS
![alt text](doc/board_pins.webp) ![alt text](doc/board_pins.webp)
### Information
Example of lora and oled:
https://hackaday.io/project/27791-esp32-lora-oled-module#j-discussions-title

View File

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 56 KiB

39
sender/include/README Normal file
View File

@ -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

46
sender/lib/README Normal file
View File

@ -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 <Foo.h>
#include <Bar.h>
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

View File

@ -35,6 +35,10 @@ SSD1306 display(0x3c, 4, 15);
#define preambleLength 8 #define preambleLength 8
#define codingRateDenominator 8 #define codingRateDenominator 8
int counter = 0; int counter = 0;
void setup() { void setup() {

11
sender/test/README Normal file
View File

@ -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