From 81e9d343f22d20453b5699b8d2507a336c2b1006 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Sun, 14 May 2023 21:02:21 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A1=20Adds=20broadcasting=20data=20by?= =?UTF-8?q?=20ws?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/spot.h | 4 ++++ src/spot.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/spot.h b/include/spot.h index 841f006..a0f72b8 100644 --- a/include/spot.h +++ b/include/spot.h @@ -46,6 +46,8 @@ class Spot { esp_err_t initialize_wifi(); uint8_t cpu_temperature(); + + esp_err_t broadcast_data(); private: esp_err_t _initialize_camera(); @@ -68,6 +70,8 @@ class Spot { MPU6050 _mpu; NewPing _leftUss; NewPing _rightUss; + + unsigned long _last_broadcast{0}; }; void display_ip_and_ssid(Adafruit_SSD1306* display, String ip, const char* ssid); diff --git a/src/spot.cpp b/src/spot.cpp index fdf7f7c..7ddb427 100644 --- a/src/spot.cpp +++ b/src/spot.cpp @@ -33,6 +33,8 @@ void Spot::handle(){ if(USE_CAPTIVE_PORTAL) _dnsServer.processNextRequest(); ArduinoOTA.handle(); _ws.cleanupClients(); + + broadcast_data(); } esp_err_t Spot::initialize_wifi(){ @@ -51,6 +53,34 @@ uint8_t Spot::cpu_temperature(){ return (temprature_sens_read() - 32) / 1.8; } +esp_err_t Spot::broadcast_data(){ + if(millis() - _last_broadcast < 50) return ESP_OK; + + size_t numContent = 13; + float content[numContent]; + content[0] = WiFi.RSSI(); + content[1] = _mpu.getTemp(); + content[2] = _mpu.getAccX(); + content[3] = _mpu.getAccY(); + content[4] = _mpu.getAccZ(); + content[5] = _mpu.getAngleX(); + content[6] = _mpu.getAngleY(); + content[7] = _mpu.getAngleZ(); + content[8] = cpu_temperature(); + content[9] = _leftUss.ping_cm(); + content[10] = _rightUss.ping_cm(); + content[11] = ESP.getFreeHeap(); + content[12] = ESP.getFreePsram(); + + uint8_t* buf = (uint8_t*) &content; + size_t buf_len = sizeof(buf); + + _ws.binaryAll(buf, buf_len * numContent); + _last_broadcast = millis(); + + return ESP_OK; +} + esp_err_t Spot::_initialize_camera(){ camera_config_t camera_config; camera_config.ledc_channel = LEDC_CHANNEL_0;