Skip to content

Commit 1a07443

Browse files
committed
Initial import
0 parents  commit 1a07443

File tree

11 files changed

+363
-0
lines changed

11 files changed

+363
-0
lines changed

.github/workflows/main.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# documentation at https://docs.platformio.org/en/latest/integration/ci/github-actions.html
2+
3+
name: PlatformIO CI
4+
5+
on: [push, pull_request]
6+
7+
jobs:
8+
build:
9+
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/cache@v3
15+
with:
16+
path: |
17+
~/.cache/pip
18+
~/.platformio/.cache
19+
key: ${{ runner.os }}-pio
20+
- uses: actions/setup-python@v4
21+
with:
22+
python-version: '3.9'
23+
- name: Install PlatformIO Core
24+
run: pip install --upgrade platformio
25+
26+
- name: Build PlatformIO Project
27+
run: pio run

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
6+
.DS_Store

.vscode/extensions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}

include/README

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

partition_table.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Name, Type, SubType, Offset, Size, Flags
2+
nvs, data, nvs, 0x9000, 0x6000,
3+
otadata, data, ota, 0xf000, 0x2000,
4+
app0, app, ota_0, 0x10000, 0x700000,
5+
app1, app, ota_1, 0x710000,0x700000,
6+
spiffs, data, spiffs, 0xe10000,0x1F0000,

platformio.ini

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env]
12+
platform = platformio/espressif32@^6.11.0
13+
14+
framework = arduino
15+
board = esp-wrover-kit
16+
; disable the next two lines for automatic port assignment
17+
monitor_port = /dev/cu.wchusbserial58900018531
18+
upload_port = /dev/cu.wchusbserial58900018531
19+
upload_speed = 921600
20+
monitor_speed = 115200
21+
board_build.flash_size = 8MB
22+
board_build.partitions = huge_app.csv
23+
;board_build.f_cpu = 240000000L
24+
monitor_filters = esp32_exception_decoder, time
25+
lib_deps = h2zero/NimBLE-Arduino@^2.3.2
26+
suculent/AESLib@^2.3.6
27+
bblanchon/ArduinoJson@^7.4.2
28+
fastled/FastLED @ ^3.3.3
29+
30+
[env:production]
31+
build_flags = -DCORE_DEBUG_LEVEL=1
32+
33+
34+
[env:local-dev]
35+
build_flags =
36+
-DCORE_DEBUG_LEVEL=5
37+

src/led.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <FastLED.h>
4+
#include "settings.h"
5+
6+
CRGB leds[LED_COUNT];
7+
8+
// Initialize
9+
void initStatusLeds() {
10+
FastLED.addLeds<WS2812B, ESP_LED_PIN, GRB>(leds, LED_COUNT);
11+
FastLED.setBrightness(50);
12+
}
13+
14+
void clearLeds() {
15+
for (int i = 0; i < LED_COUNT; i++) {
16+
leds[i] = CRGB::Black;
17+
}
18+
}
19+
20+
// Set the color of LED at position <index>
21+
void updateLed(uint8_t index, CRGB color) {
22+
leds[index] = color;
23+
FastLED.show();
24+
}

src/main.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include <esp_now.h>
2+
#include <WiFi.h>
3+
#include <esp_wifi.h>
4+
5+
// REPLACE WITH YOUR RECEIVER MAC Address
6+
uint8_t broadcastAddress[] = {0xC8, 0xF0, 0x9E, 0x19, 0x88, 0x80};
7+
8+
// Structure example to send data
9+
// Must match the receiver structure
10+
typedef struct {
11+
float accMag;
12+
float meanhpfvm;
13+
float peakhpfvm;
14+
float gyrMag;
15+
float tempC;
16+
float batteryLevelVoltage;
17+
int batteryLevelPercentage;
18+
char timeStamp[25];
19+
char id[32];
20+
} SensorPacket;
21+
22+
uint8_t mac[6]; //Mac address
23+
char macStr[18]; //Mac address in string format
24+
25+
// Create a struct_message called myData
26+
SensorPacket myData;
27+
28+
esp_now_peer_info_t peerInfo;
29+
30+
uint8_t channel = 0;
31+
bool isHubChannelFound = false;
32+
uint8_t failureCount = 0;
33+
34+
// callback when data is sent
35+
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
36+
Serial.print("\r\nLast Packet Send Status:\t");
37+
log_i("Sending status: %s", status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
38+
// Did sending fail?
39+
if (status == ESP_NOW_SEND_FAIL) {
40+
if (!isHubChannelFound) {
41+
log_i("Failed with channel %d.", channel);
42+
// Increase channel. If hub is not online this will search forever!
43+
// Consider implementing an increasing waiting time to avoid channel congestion
44+
// whenn all nodes start searching a channel
45+
channel = (channel + 1) % 13;
46+
esp_wifi_set_promiscuous(true);
47+
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
48+
esp_wifi_set_promiscuous(false);
49+
} else {
50+
// If the hub channel has been found before but we have failures now
51+
// we give the hub some time before we start searching the channel again
52+
// This might avoid all nodes starting the channel search immediatelly
53+
failureCount++;
54+
if (failureCount > 10) {
55+
isHubChannelFound = false;
56+
}
57+
}
58+
59+
} else {
60+
failureCount = 0;
61+
isHubChannelFound = true;
62+
}
63+
}
64+
65+
void setup() {
66+
// Init Serial Monitor
67+
Serial.begin(115200);
68+
69+
// Set device as a Wi-Fi Station
70+
WiFi.mode(WIFI_STA);
71+
72+
// Init ESP-NOW
73+
if (esp_now_init() != ESP_OK) {
74+
log_i("Error initializing ESP-NOW");
75+
return;
76+
}
77+
78+
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, mac); //Read MAC address (station mode)
79+
80+
if (ret == ESP_OK) {
81+
snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X",
82+
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); //Convert MAC (unique ID of the device)
83+
} else {
84+
85+
log_i("Failed to get the MAC!");
86+
}
87+
88+
log_i("%s", macStr);
89+
90+
// Once ESPNow is successfully Init, we will register for Send CB to
91+
// get the status of Trasnmitted packet
92+
esp_now_register_send_cb(OnDataSent);
93+
94+
esp_wifi_set_promiscuous(true);
95+
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
96+
esp_wifi_set_promiscuous(false);
97+
98+
// Register peer
99+
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
100+
peerInfo.channel = 0;
101+
peerInfo.encrypt = false;
102+
103+
// Add peer
104+
if (esp_now_add_peer(&peerInfo) != ESP_OK){
105+
log_i("Failed to add peer");
106+
return;
107+
}
108+
}
109+
110+
void loop() {
111+
112+
myData.accMag = random(1,20) / 10.0f;
113+
myData.meanhpfvm = random(1,20) / 10.0f;
114+
myData.peakhpfvm = random(1,20) / 10.0f;
115+
myData.gyrMag = random(1,20) / 10.0f;
116+
myData.tempC = random(0,450) / 10.0f;
117+
myData.batteryLevelVoltage = random(1,20) / 10.0f;
118+
myData.batteryLevelPercentage = random(1,20);
119+
// Timestamp will be set on hub
120+
strcpy(myData.timeStamp, "1970-01-01T00:00:00Z");
121+
// could also be set on receiver side
122+
strcpy(myData.id,macStr);
123+
124+
// Send message via ESP-NOW
125+
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
126+
127+
if (result == ESP_OK) {
128+
log_i("Sent with success");
129+
} else {
130+
log_i("Error sending the data");
131+
}
132+
133+
if (isHubChannelFound) {
134+
delay(2000);
135+
} else {
136+
delay(100);
137+
}
138+
139+
}

src/settings.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <ETH.h>
4+
5+
// Ethernet settings
6+
#define ETH_CLK_MODE ETH_CLOCK_GPIO16_OUT
7+
#define ETH_POWER_PIN 5
8+
#define ETH_TYPE ETH_PHY_LAN8720
9+
#define ETH_ADDR 0
10+
#define ETH_MDC_PIN 23
11+
#define ETH_MDIO_PIN 18
12+
13+
#define ETH_TIMEOUT_MILLIS 10000
14+
15+
// LED Settings
16+
#define ESP_LED_PIN 32
17+
#define LED_COUNT 4
18+
#define CHANNEL 0

0 commit comments

Comments
 (0)