From 6703e8247f0670e3d697d9b269d4f15d4247d14a Mon Sep 17 00:00:00 2001 From: Zoe Pfister <zoe.pfister@student.uibk.ac.at> Date: Fri, 7 Oct 2022 11:58:30 +0200 Subject: [PATCH] fix message sending, add simple esp32 host --- client/client/include/MeasurementData.hpp | 1 - .../lib/espnow/src/ClientDataPackage.hpp | 1 + client/client/lib/espnow/src/ESPNow.cpp | 7 +++- client/client/lib/espnow/src/Message.cpp | 5 ++- client/client/platformio.ini | 3 ++ client/client/src/main.cpp | 3 +- host/esp32/src/main.cpp | 42 +++++++++++++++++++ 7 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 host/esp32/src/main.cpp diff --git a/client/client/include/MeasurementData.hpp b/client/client/include/MeasurementData.hpp index 9434e6a..36bd5db 100644 --- a/client/client/include/MeasurementData.hpp +++ b/client/client/include/MeasurementData.hpp @@ -34,7 +34,6 @@ class MeasurementData { double value; std::string measurementType; // TODO: consider using an enum std::optional<int> channel; - std::optional<int> i2cAddress; }; #endif // CLIENT_MEASUREMENTDATA_HPP diff --git a/client/client/lib/espnow/src/ClientDataPackage.hpp b/client/client/lib/espnow/src/ClientDataPackage.hpp index 2458ca6..7cdd66f 100644 --- a/client/client/lib/espnow/src/ClientDataPackage.hpp +++ b/client/client/lib/espnow/src/ClientDataPackage.hpp @@ -50,4 +50,5 @@ class ClientDataPackage { serializeJson(document, jsonString); return jsonString; } + }; diff --git a/client/client/lib/espnow/src/ESPNow.cpp b/client/client/lib/espnow/src/ESPNow.cpp index 8d7eb36..890392a 100644 --- a/client/client/lib/espnow/src/ESPNow.cpp +++ b/client/client/lib/espnow/src/ESPNow.cpp @@ -1,6 +1,6 @@ #include "ESPNow.hpp" -static const char* TAG = "ESPNOW"; +static const char *TAG = "ESPNOW"; uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; esp_now_peer_info_t hostInfo; @@ -53,7 +53,10 @@ esp_err_t espnow_setup() get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise hostInfo.channel = 0; - hostInfo.encrypt = 0; + + // TODO: PMK is used to encrypt LMK with the AES-128 algorithm. Call esp_now_set_pmk() to set PMK. If PMK is not set, a + // default PMK will be used. https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html + hostInfo.encrypt = false; esp_now_add_peer(&hostInfo); esp_now_register_recv_cb(on_data_recv); diff --git a/client/client/lib/espnow/src/Message.cpp b/client/client/lib/espnow/src/Message.cpp index 8c69a11..7de1fc9 100644 --- a/client/client/lib/espnow/src/Message.cpp +++ b/client/client/lib/espnow/src/Message.cpp @@ -9,8 +9,11 @@ esp_err_t Message::send() const ESP_LOGI(TAG, "Sending message"); esp_err_t success; auto messageData = getMessageAsMinifiedJsonString(); - success = esp_now_send(recipient, (uint8_t *)&messageData, sizeof(clientDataPackage)); + + // conversion from std::string to c_str adds null terminator, which is why we add 1 to message length + success = esp_now_send(recipient, (uint8_t *)messageData.c_str(), (messageData.length() + 1) * sizeof(char)); if (success != ESP_OK) { + ESP_LOGE(TAG, "Error sending the data"); // TODO REWRITE FOR JSON // if (!ram_cache_is_full()) { // // ram_cache_push(messageData); diff --git a/client/client/platformio.ini b/client/client/platformio.ini index 28cc58c..435ae69 100644 --- a/client/client/platformio.ini +++ b/client/client/platformio.ini @@ -20,6 +20,9 @@ build_flags = -DCORE_DEBUG_LEVEL=5 -std=gnu++17 build_unflags = -std=gnu++11 +; serial port +monitor_port = /dev/ttyUSB1 +upload_port = /dev/ttyUSB1 lib_deps = sparkfun/SparkFun SCD30 Arduino Library@^1.0.18 Wire diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index 77265a5..bf964c0 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -14,6 +14,8 @@ void setup() { Serial.begin(115200); drs26.setup(); + espnow_setup(); + // log_e("Setup complete."); } @@ -23,7 +25,6 @@ void loop() out_data_drs26 data{}; try { - espnow_setup(); // data = drs26.readData(); auto messages = drs26.buildMessages(); diff --git a/host/esp32/src/main.cpp b/host/esp32/src/main.cpp new file mode 100644 index 0000000..132224b --- /dev/null +++ b/host/esp32/src/main.cpp @@ -0,0 +1,42 @@ +#include <Arduino.h> +#include <WiFi.h> +#include <esp_now.h> + +void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) +{ + // go to sleep +} + +void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len) +{ + // print mac + Serial.println("Message recieved"); + for (int i = 0; i < 6; i++) { + Serial.print(mac[i], HEX); + Serial.print(":"); + } + Serial.println(); + + char data[len]; + memcpy(data, incomingData, len); + Serial.println(data); +} + +void setup() +{ + Serial.begin(115200); + WiFi.mode(WIFI_STA); + Serial.println("ESPNow init"); + if (esp_now_init() != ESP_OK) { + // initialization failed + Serial.println("ESPNow init failed"); + return; // not sure about this + } + Serial.println("ESPNow init success"); + + esp_now_register_recv_cb(on_data_recv); + // write your initialization code here +} + +void loop() { +} \ No newline at end of file -- GitLab