diff --git a/host/esp32/lib/espnow/src/espnow.cpp b/host/esp32/lib/espnow/src/espnow.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d031dcc3ad62e6e0b73ccc3d1420fa8bc83ca62e --- /dev/null +++ b/host/esp32/lib/espnow/src/espnow.cpp @@ -0,0 +1,56 @@ +#include <esp_now.h> +#include <esp_wifi.h> +#include "espnow.hpp" +#include "WiFi.h" +#include <ESP32Time.h> + +uint8_t BROADCAST_MAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // leaving this in as possible backup +esp_now_peer_info_t hostInfo; +data_struct lastData; +ESP32Time rtc; + +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){ + memcpy(&lastData, incomingData, sizeof(lastData)); + for(int i=0; i<NUM_SENSORS; i++){ + Serial.println(lastData.identifiers[i] + lastData.values[i]); + } + Serial.println("at " + lastData.timestamp); +} + +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 + } + + hostInfo.channel = 0; + hostInfo.encrypt = true; + result = esp_now_add_peer(&hostInfo); + if(result != ESP_OK){ + //peer couldn't be added + return result; + } + + esp_now_register_recv_cb(on_data_recv); + esp_now_register_send_cb(on_data_sent); + + return ESP_OK; +} + +Message :: Message(){ + memcpy(BROADCAST_MAC, recipient, sizeof(BROADCAST_MAC)); + config = (config_struct*) malloc(sizeof(config_struct)); + esp_wifi_get_mac(WIFI_IF_STA, config->host); + config->time_millis = rtc.getMillis(); +} + +esp_err_t Message::send(){ + return esp_now_send(recipient, (uint8_t *) &config, sizeof(config)); +} \ No newline at end of file diff --git a/host/esp32/lib/espnow/src/espnow.hpp b/host/esp32/lib/espnow/src/espnow.hpp new file mode 100644 index 0000000000000000000000000000000000000000..0f588924e9f60203503c9cb152e30f149d1dc46c --- /dev/null +++ b/host/esp32/lib/espnow/src/espnow.hpp @@ -0,0 +1,30 @@ +#ifndef _ESPNOW +#define _ESPNOW + +#define NUM_SENSORS 10 + +typedef struct data_struct{ + int identifiers[NUM_SENSORS]; + float values[NUM_SENSORS]; + long timestamp; //maybe make this array +}data_struct; + +typedef struct config_struct{ + uint8_t host[6]; + long time_millis; +}config_struct; + +class Message{ + public: + Message(); + esp_err_t send(); + uint8_t recipient[6]; + + private: + config_struct *config; + +}; + +esp_err_t espnow_setup(); + +#endif \ No newline at end of file diff --git a/host/esp32/platformio.ini b/host/esp32/platformio.ini index e36a50f32857e3a1db663563eab844a827e6e7bf..e19f975aba8b5dc713dd2186b8280a4f35d8f082 100644 --- a/host/esp32/platformio.ini +++ b/host/esp32/platformio.ini @@ -12,3 +12,4 @@ platform = espressif32 board = esp32-c3-devkitm-1 framework = arduino +lib_deps = fbiego/ESP32Time@^2.0.0 diff --git a/host/esp32/src/main.cpp b/host/esp32/src/main.cpp index 58b344c5757478d990bb23dd41243135880fb2f5..48f83e3673723999fb1c52474352ac779eab2e56 100644 --- a/host/esp32/src/main.cpp +++ b/host/esp32/src/main.cpp @@ -1,9 +1,20 @@ #include <Arduino.h> +#include <esp_wifi.h> +#include "WiFi.h" +#include "espnow.hpp" void setup() { // put your setup code here, to run once: + Serial.begin(115200); + while(!Serial); + WiFi.mode(WIFI_STA); + espnow_setup(); + + Message* msg = new Message; + msg->send(); } void loop() { // put your main code here, to run repeatedly: + Serial.println("No message..."); } \ No newline at end of file