Skip to content
Snippets Groups Projects
Commit 7b8e9721 authored by Zoe Pfister's avatar Zoe Pfister :speech_balloon:
Browse files

Merge branch 'develop' into 'sensor_readout'

Merge Branch `develop` into `sensor_readout`

See merge request !3
parents 75fabc7e 859d306a
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,!3Merge Branch `develop` into `sensor_readout`
Showing
with 321 additions and 12 deletions
......@@ -14,6 +14,8 @@ dist/
downloads/
eggs/
.eggs/
lib/
!client/client/lib
lib64/
parts/
sdist/
......
{
"version": "0.1.0",
"tasks": [
{
"taskName": "clang-format-everything",
"command": "clang-format",
"args": ["-i", "*" ],
"isShellCommand": true,
"isBackground": true
}
]
}
\ No newline at end of file
"version": "0.1.0",
"tasks": [
{
"taskName": "clang-format-everything",
"command": "clang-format",
"args": ["-i", "*"],
"isShellCommand": true,
"isBackground": true
}
]
}
#include "ram_caching.hpp"
RTC_DATA_ATTR int cachedAmount = -1;
RTC_DATA_ATTR ClientDataPackage backup[NUM_SENSORS];
ClientDataPackage ram_cache_pop()
{
return backup[cachedAmount--];
}
void ram_cache_push(ClientDataPackage data)
{
backup[++cachedAmount] = data;
}
bool ram_cache_is_empty()
{
return cachedAmount == -1;
}
bool ram_cache_is_full()
{
return cachedAmount == 9;
}
\ No newline at end of file
#ifndef _RAM_CACHE
#define _RAM_CACHE
#include "ClientDataPackage.hpp"
#include <ESP32Time.h>
bool ram_cache_is_empty();
bool ram_cache_is_full();
void ram_cache_push(ClientDataPackage data);
ClientDataPackage ram_cache_pop();
#endif
\ No newline at end of file
# basic usage
To send data using espnow, create a new Message object,
then use the add_data(value, identifier) method for every value
to fill the message.
when every value is added, use the send() method to send the data
to the host (fipy). If the esp client has never recieved a config
message from the host, it will instead broadcast the message.
---
right now, it is not possible to add more than 10 values.
#pragma once
#define NUM_SENSORS 10
// packing the struct without padding, makes reading it on the fipy easier
#pragma pack(1)
// having the data be a struct of basic types makes sending easier,
// otherwise we would have to serialize the data before sending
struct ClientDataPackage {
int identifiers[NUM_SENSORS];
float values[NUM_SENSORS];
int amountData;
long timestamp; // maybe make this array
};
#include "ESPNow.hpp"
uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t hostInfo;
Preferences preferences;
void get_host_mac(uint8_t *destination)
{
preferences.begin("config", true);
if (preferences.isKey("host")) {
preferences.getBytes("host", destination, sizeof(uint8_t) * 6);
} else {
memcpy(destination, BROADCAST_MAC, sizeof(BROADCAST_MAC));
Serial.println("backup mac used");
}
preferences.end();
}
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)
{
Serial.println("message recieved");
config new_config;
memcpy(&new_config, incomingData, sizeof(new_config)); // TODO: check for valid mac
// put the host address in flash mem
preferences.begin("config", false);
if (!preferences.isKey("host")) {
preferences.putBytes("host", new_config.host, sizeof(new_config.host));
Serial.println("host mac saved to flash");
} // host change shouldn't be an issue
preferences.end();
// sync time
esptime::rtc.setTime(
new_config.time_millis); // see https://www.esp32.com/viewtopic.php?t=9965, maybe this needs an offset
}
esp_err_t espnow_setup()
{
esp_err_t result;
WiFi.mode(WIFI_STA);
result = esp_now_init();
if (result != ESP_OK) {
// initialization failed
return result; // not sure about this
}
get_host_mac(hostInfo.peer_addr); // check if there is a host saved in flash mem, broadcast otherwise
hostInfo.channel = 0;
hostInfo.encrypt = 0;
esp_now_add_peer(&hostInfo);
esp_now_register_recv_cb(on_data_recv);
esp_now_register_send_cb(on_data_sent);
return ESP_OK;
}
#ifndef _ESPNOW
#define _ESPNOW
#include "Message.hpp"
#include "Time.hpp"
#include "ram_caching.hpp"
#include <ClientDataPackage.hpp>
#include <ESP32Time.h>
#include <Preferences.h>
#include <WiFi.h>
#include <esp_now.h>
typedef struct config {
uint8_t host[6];
long time_millis;
} config;
esp_err_t espnow_setup();
bool is_host_defined();
void get_host_mac(uint8_t *destination);
void on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status);
void on_data_recv(const uint8_t *mac, const uint8_t *incomingData, int len);
#endif
\ No newline at end of file
#include "Message.hpp"
void Message::add_data(float value, int identifier)
{
if (data.amountData < NUM_SENSORS) {
data.values[data.amountData] = value;
data.identifiers[data.amountData] = identifier;
data.amountData++;
}
}
esp_err_t Message::send()
{
Serial.println("sending Message");
esp_err_t success;
success = esp_now_send(recipient, (uint8_t *)&data, sizeof(data));
// if(success != ESP_OK){
// if(!ram_cache_is_full()){
// ram_cache_push(*data);
// }
// }
for (int i = 0; i < data.amountData; i++) {
Serial.println(data.values[i]);
}
Serial.println((String) "time sent: " + data.timestamp);
Serial.println((String) "Send status: " + success);
Serial.println();
Serial.flush();
Serial.println("done");
return success;
}
Message ::Message()
{
// check for existing host mac address, use broadcast otherwise
get_host_mac(recipient);
data.amountData = 0;
data.timestamp = esptime::rtc.getMillis(); // I am assuming we are not sending data from Unix Epoch
}
Message ::Message(ClientDataPackage old_data)
{
data = old_data;
// memcpy(&data, &old_data, sizeof(data));
get_host_mac(recipient);
}
\ No newline at end of file
#pragma once
#include "ClientDataPackage.hpp"
#include "ESPNow.hpp"
#include "Time.hpp"
#include <Arduino.h>
#include <ESP32Time.h>
#include <esp_now.h>
// Format of the message sent from host to client
// if more things are sent from the host the name might not be accurate anymore
class Message {
public:
Message();
Message(ClientDataPackage old_data);
void add_data(float value, int identifier);
esp_err_t send();
private:
ClientDataPackage data;
uint8_t recipient[6];
};
\ No newline at end of file
#pragma once
#include <ESP32Time.h>
namespace esptime {
// internal linkage only
static ESP32Time rtc; // global variable for the RTC (i don't think this is good practice)
} // namespace esptime
// namespace esptime
......@@ -29,4 +29,3 @@ void loop() {
// }
delay(5000);
}
#include "TestESPNow.hpp"
void test_on_data_recv_valid_config()
{
uint8_t mac_addr[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int len = 0;
config conf = {host : {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, time_millis : 0};
// preferences / hostinfo would need to be global for this to work
TEST_FAIL();
}
#pragma once
#include <Arduino.h>
#include <ESPNow.hpp>
#include <unity.h>
void test_on_data_recv_valid_config();
\ No newline at end of file
#include "TestMessage.hpp"
void test_on_data_recv()
{
Message message = Message{};
TEST_ASSERT_EQUAL(0, message.getData().amountData);
}
void test_new_message_filled()
{
Message message = Message{};
message.add_data(1.1, 0);
message.add_data(1.2, 1);
message.add_data(1.3, 2);
float expectedValuesArray[] = {1.1, 1.2, 1.3};
int expectedIdentifiersArray[] = {0, 1, 2};
TEST_ASSERT_EQUAL(3, message.getData().amountData);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedValuesArray, message.getData().values, 3);
TEST_ASSERT_EQUAL_INT_ARRAY(expectedIdentifiersArray, message.getData().identifiers, 3);
}
\ No newline at end of file
#pragma once
#include <Arduino.h>
#include <ESPNow.hpp>
#include <unity.h>
void test_on_data_recv();
void test_new_message_filled();
\ No newline at end of file
#include "TestESPNow.hpp"
#include "TestMessage.hpp"
#include <Arduino.h>
#include <unity.h>
void setup()
{
delay(2000); // service delay
UNITY_BEGIN();
RUN_TEST(test_on_data_recv_valid_config);
RUN_TEST(test_on_data_recv);
RUN_TEST(test_new_message_filled);
UNITY_END();
}
void loop() {}
\ No newline at end of file
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
{
"cmake.configureOnOpen": true
}
\ 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