Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TTGO Lora32 OLED PJON Example #345

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
6 changes: 6 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.pio
.pioenvs
.piolibdeps
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
67 changes: 67 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < https://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choose one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#


#
# Template #1: General project. Test it using existing `platformio.ini`.
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio run


#
# Template #2: The project is intended to be used as a library with examples.
#

# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"terminal.integrated.env.windows": {
"PATH": "C:\\Users\\MXB\\.platformio\\penv\\Scripts;C:\\Users\\MXB\\.platformio\\penv;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\PuTTY\\;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Users\\MXB\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\MXB\\AppData\\Local\\atom\\bin;C:\\Program Files\\LLVM\\bin;C:\\Users\\MXB\\AppData\\Local\\GitHubDesktop\\bin;C:\\Users\\MXB\\Programs\\VSCodium\\bin;C:\\Program Files (x86)\\Nmap;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\PuTTY\\;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Users\\MXB\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\MXB\\AppData\\Local\\atom\\bin;C:\\Program Files\\LLVM\\bin;C:\\Users\\MXB\\AppData\\Local\\GitHubDesktop\\bin;C:\\Users\\MXB\\Programs\\VSCodium\\bin;C:\\Program Files (x86)\\Nmap",
"PLATFORMIO_CALLER": "vscode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/include/README
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/lib/README
Original file line number Diff line number Diff line change
@@ -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 a 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
21 changes: 21 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; 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:esp32dev]
platform = espressif32
board = esp32dev ;heltec_wifi_lora_32_V2
framework = arduino
monitor_speed = 115200

lib_deps =
https://github.com/gioblu/PJON.git
https://github.com/ThingPulse/esp8266-oled-ssd1306
https://github.com/sandeepmistry/arduino-LoRa.git

108 changes: 108 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @file main.cpp
* @author Michael Branson
* @brief TTGO Lora32 Example of PJON.
* Upload the same code to two devices and they will PJON_BROADCAST to each other, displaying round trip time on the OLED.
* @version 0.1
* @date 2020-04-18
*
* @copyright Copyright (c) 2020
*
*/

#include <Arduino.h>
#include <SPI.h>

#define USE_OLED // load screen code
#include "oled.h"

#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 915000000UL // Match your boards Lora frequencies!

#define lowByte(w) ((uint8_t) ((w) & 0xff))
#define highByte(w) ((uint8_t) ((w) >> 8))

#define PJON_INCLUDE_TL
// #define PJON_PACKET_MAX_LENGTH 1024
#define PJON_INCLUDE_PACKET_ID true

#include <PJON.h>
const uint8_t bus_id[] = {0, 0, 0, 0};
const uint8_t dev_id = (int)random(1, 255);
PJON<ThroughLora> bus(bus_id,dev_id);

int32_t timer2 = millis();
int32_t dev_heartbeat_frequency = 5000;
uint32_t packet_id{};
uint32_t timer{};

void PrintHex8(uint8_t *data, uint16_t length) // prints 8-bit data in hex with leading zeroes
{
for (int i=0; i<length; i++) {
if (data[i]<0x10) {Serial.print("0");}
Serial.printf("0x%02x ",data[i]);
}
Serial.println("");
for (int i=0; i<length; i++) {

Serial.print((char)data[i]);
Serial.print(" ");
}
Serial.println();
}

void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {

if(payload[0]=='B'){
uint32_t t_time = millis() - timer;
String msg = "Pong: "+String(t_time)+"ms rtt"; // round trip time
oled(msg, 1, false);
}
if(payload[0]=='P'){
bus.send(0, "B",1,bus.config,packet_info.id);
}

Serial.print("Got packet from ");
Serial.print(packet_info.sender_id);
Serial.print(":");
Serial.println(packet_info.port);
PrintHex8(payload,length);


}



void setup() {
Serial.begin(115200);
oled_setup();
bus.set_receiver(receiver_function);
bus.set_packet_id(true);
bus.include_sender_info(true);
bus.include_port(true);
//bus.set_crc_32(true);
SPI.begin(SCK,MISO,MOSI,SS); // Important for TTGO boards
bus.strategy.setPins(SS,RST,DI0);
bus.strategy.setFrequency(BAND);
bus.strategy.setSignalBandwidth(250E3); //Optional
bus.begin();
Serial.println("\nPJON Ready.");
}

void loop() {
if(millis()-timer2 > dev_heartbeat_frequency){
bus.send(0, "P",1,bus.config,++packet_id);
timer = millis();
timer2 = timer;
oled("Ping "+String(packet_id), 0, true);

}
bus.update();
bus.receive();
}
46 changes: 46 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/src/oled.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef _OLED_
#define _OLED_

#include <Arduino.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 16 // Reset pin # (or -1 if sharing Arduino reset pin)

#ifdef USE_OLED
#include <SSD1306.h>
SSD1306 display(0x3c, 21, 22);

#endif

void oled_setup(){
#ifdef USE_OLED
pinMode(OLED_RESET,OUTPUT);
digitalWrite(OLED_RESET,LOW);
delay(50);
digitalWrite(OLED_RESET,HIGH);
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
#endif
}

void oled(String text, int8_t line, bool clear){
#ifdef USE_OLED
if(clear){
display.clear();
}
//display.setTextAlignment(TEXT_ALIGN_LEFT);
//display.setFont(ArialMT_Plain_10);
display.drawStringMaxWidth(0 , (line*20) , 128, " ");
display.drawStringMaxWidth(0 , (line*20) , 128, text);
// for(int8_t x=0;x<5;x++){
// //display.drawString(0 , (x*15) , lines[x]);
// }

display.display();
#endif
}

#endif
11 changes: 11 additions & 0 deletions examples/ESP32/LoRa/PlatformIO/TTGO V2 PJON TX-RX/test/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

This directory is intended for PIO Unit Testing 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 PIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html
23 changes: 23 additions & 0 deletions src/PJON.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,29 @@ class PJON {
strategy.back_off(packets[i].attempts)
)
) {

#ifdef PJON_INJECT_ATTEMPTS
//mxb added to replace last byte of payload with packet[i].attempts and recalculate checksum
uint16_t new_length = packets[i].length;
if(packets[i].content[1] & PJON_CRC_BIT) {
packets[i].content[packets[i].length-5] = packets[i].attempts;
uint32_t computed_crc =
PJON_crc32::compute((uint8_t *)packets[i].content, new_length - 4);
packets[i].content[new_length - 4] =
(uint8_t)((uint32_t)(computed_crc) >> 24);
packets[i].content[new_length - 3] =
(uint8_t)((uint32_t)(computed_crc) >> 16);
packets[i].content[new_length - 2] =
(uint8_t)((uint32_t)(computed_crc) >> 8);
packets[i].content[new_length - 1] =
(uint8_t)((uint32_t)computed_crc);
} else {
packets[i].content[packets[i].length-1] = packets[i].attempts;
packets[i].content[new_length - 1] =
PJON_crc8::compute((uint8_t *)packets[i].content, new_length - 1);
}
#endif

if(!(sync_ack && async_ack && packets[i].state == PJON_ACK))
packets[i].state = // Avoid resending sync-acked async ack packets
send_packet(packets[i].content, packets[i].length);
Expand Down