Skip to content
Snippets Groups Projects
Verified Commit 6ecb78b6 authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

add mock host that allows viewing of received espnow data

parent 81bd3361
No related branches found
No related tags found
4 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!14Merge gsm host into development
.pio
CMakeListsPrivate.txt
cmake-build-*/
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
; 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:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
lib_deps =
bblanchon/ArduinoJson@^6.19.4
fbiego/ESP32Time@^2.0.0
#include "ESP32Time.h"
#include <Arduino.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <esp_log.h>
#include <esp_now.h>
static const std::string TAG = "MAIN";
static const std::string TAG_ESPNOW = "ESPNOW";
static const std::string TAG_GSM = "GSM";
ESP32Time rtc;
TaskHandle_t ESPNOWTask;
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
// go to sleep
}
String getMacAddressAsString(const uint8_t *mac);
DynamicJsonDocument parseReceivedJsonData(char *data);
void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
esp_log_write(ESP_LOG_INFO, TAG_ESPNOW.c_str(), "Message recieved\n");
// copy received data to a char array
char data[len];
memcpy(data, incomingData, len);
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Raw received Data: %s\n", data);
DynamicJsonDocument doc = parseReceivedJsonData(data);
String macAddress = getMacAddressAsString(mac);
// add timestamp and mac address
doc["timestamp"] = rtc.getEpoch();
doc["clientMac"] = macAddress;
// serialize json document again
std::string dataString{};
serializeJson(doc, dataString);
}
DynamicJsonDocument parseReceivedJsonData(char *data)
{
DynamicJsonDocument doc(250);
auto error = deserializeJson(doc, data);
if (error) {
esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW.c_str(), "Error while parsing json: %s\n", error.f_str());
// TODO error handling
}
return doc;
}
String getMacAddressAsString(const uint8_t *mac)
{
String macAddress;
for (int i = 0; i < 6; i++) {
macAddress += String(mac[i], HEX);
}
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "MAC: %s\n", macAddress.c_str());
return macAddress;
}
[[noreturn]] void ESPNOWReceiveTask(void *parameter)
{
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "ESPNOWReceiveTask started on core %d\n", xPortGetCoreID());
WiFi.mode(WIFI_STA);
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Initialising ESPNow...\n");
if (esp_now_init() != ESP_OK) {
// initialization failed
esp_log_write(ESP_LOG_ERROR, TAG_ESPNOW.c_str(), "Initialising ESPNow FAILED\n");
exit(ESP_FAIL);
}
esp_log_write(ESP_LOG_DEBUG, TAG_ESPNOW.c_str(), "Initialising ESPNow SUCCESS\n");
esp_now_register_recv_cb(on_data_recv);
while (true) {
}
}
void setup()
{
// Set console baud rate
Serial.begin(115200);
delay(10);
// https://stackoverflow.com/questions/60442350/arduinos-esp-log-set-vprintf-does-not-work-on-esp32
esp_log_level_set("*", ESP_LOG_VERBOSE);
esp_log_write(ESP_LOG_DEBUG, TAG.c_str(), "%s", WiFi.macAddress().c_str());
delay(1000);
// create ESPNOWReceiveTask. TODO: Until the UTC time is not synced, this will not add the correct time. If we
// TODO: create the task after the time is synced, no messages will be received until synchronization is done
xTaskCreatePinnedToCore(ESPNOWReceiveTask, /* Function to implement the task */
"ESPNOWReceiveTask", /* Name of the task */
10000, /* Stack size in words */
nullptr, /* Task input parameter */
0, /* Priority of the task */
&ESPNOWTask, /* Task handle. */
0); /* Core where the task should run */
// Restart takes quite some time
// To skip it, call init() instead of restart()
esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Initializing modem...\n");
}
void loop() {}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment