Skip to content
Snippets Groups Projects
Unverified Commit 6703e824 authored by Zoe Pfister's avatar Zoe Pfister :speech_balloon:
Browse files

fix message sending, add simple esp32 host

parent f05e1dd5
No related branches found
No related tags found
6 merge requests!39Merge Develop into Main,!19development into master,!17Inital Host, initial Client,!10merge serial comm and sd write into espnow,!8merge sensor_readout into develop,!7move to c++17, change message building, refactored SensorInformation and MeasurementData
...@@ -34,7 +34,6 @@ class MeasurementData { ...@@ -34,7 +34,6 @@ class MeasurementData {
double value; double value;
std::string measurementType; // TODO: consider using an enum std::string measurementType; // TODO: consider using an enum
std::optional<int> channel; std::optional<int> channel;
std::optional<int> i2cAddress; std::optional<int> i2cAddress;
}; };
#endif // CLIENT_MEASUREMENTDATA_HPP #endif // CLIENT_MEASUREMENTDATA_HPP
...@@ -50,4 +50,5 @@ class ClientDataPackage { ...@@ -50,4 +50,5 @@ class ClientDataPackage {
serializeJson(document, jsonString); serializeJson(document, jsonString);
return jsonString; return jsonString;
} }
}; };
#include "ESPNow.hpp" #include "ESPNow.hpp"
static const char* TAG = "ESPNOW"; static const char *TAG = "ESPNOW";
uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t hostInfo; esp_now_peer_info_t hostInfo;
...@@ -53,7 +53,10 @@ esp_err_t espnow_setup() ...@@ -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 get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise
hostInfo.channel = 0; 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_add_peer(&hostInfo);
esp_now_register_recv_cb(on_data_recv); esp_now_register_recv_cb(on_data_recv);
......
...@@ -9,8 +9,11 @@ esp_err_t Message::send() const ...@@ -9,8 +9,11 @@ esp_err_t Message::send() const
ESP_LOGI(TAG, "Sending message"); ESP_LOGI(TAG, "Sending message");
esp_err_t success; esp_err_t success;
auto messageData = getMessageAsMinifiedJsonString(); 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) { if (success != ESP_OK) {
ESP_LOGE(TAG, "Error sending the data");
// TODO REWRITE FOR JSON // TODO REWRITE FOR JSON
// if (!ram_cache_is_full()) { // if (!ram_cache_is_full()) {
// // ram_cache_push(messageData); // // ram_cache_push(messageData);
......
...@@ -20,6 +20,9 @@ build_flags = ...@@ -20,6 +20,9 @@ build_flags =
-DCORE_DEBUG_LEVEL=5 -DCORE_DEBUG_LEVEL=5
-std=gnu++17 -std=gnu++17
build_unflags = -std=gnu++11 build_unflags = -std=gnu++11
; serial port
monitor_port = /dev/ttyUSB1
upload_port = /dev/ttyUSB1
lib_deps = lib_deps =
sparkfun/SparkFun SCD30 Arduino Library@^1.0.18 sparkfun/SparkFun SCD30 Arduino Library@^1.0.18
Wire Wire
......
...@@ -14,6 +14,8 @@ void setup() ...@@ -14,6 +14,8 @@ void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
drs26.setup(); drs26.setup();
espnow_setup();
// log_e("Setup complete."); // log_e("Setup complete.");
} }
...@@ -23,7 +25,6 @@ void loop() ...@@ -23,7 +25,6 @@ void loop()
out_data_drs26 data{}; out_data_drs26 data{};
try { try {
espnow_setup();
// data = drs26.readData(); // data = drs26.readData();
auto messages = drs26.buildMessages(); auto messages = drs26.buildMessages();
......
#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
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