Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into ultrasonic
Browse files Browse the repository at this point in the history
  • Loading branch information
thatliuser committed Feb 1, 2025
2 parents c93884f + 926ae11 commit 178f58a
Show file tree
Hide file tree
Showing 140 changed files with 24,251 additions and 4,215 deletions.
Binary file added .DS_Store
Binary file not shown.
12 changes: 11 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }
BasedOnStyle: LLVM
UseTab: Never
IndentWidth: 4
TabWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
FixNamespaceComments: false
18 changes: 18 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Compile check
on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Polyfill AWS certificates
run: |
cd components/ClientComponent/
touch ca.crt client.crt client.key aws.url
- name: Build ESP-IDF project
uses: espressif/esp-idf-ci-action@v1
with:
esp_idf_version: v5.3.1
target: esp32
16 changes: 16 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Format check
on: [push]

jobs:
format-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run clang-format
uses: DoozyX/[email protected]
with:
source: 'components main'
exclude: 'managed_components components/hx711'
clangFormatVersion: 18
style: file

14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/settings.json
build
config.hpp
.env
flake.nix
flake.lock
.cache/
.DS_Store
.vscode/settings.json
components/.DS_Store
components/ClientComponent/.DS_Store
dependencies.lock
**/.DS_Store
dependencies.lock
components/ClientComponent/aws.url
components/ClientComponent/ca.crt
components/ClientComponent/client.crt
components/ClientComponent/client.key
sdkconfig*
!sdkconfig.defaults
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"idf.flashType": "UART",
"idf.port": "/dev/COM4",
"idf.portWin": "COM7",
"files.associations": {
"system_error": "cpp"
},
"idf.espIdfPath": "/Users/anthonychen/esp/v5.3.1/esp-idf",
"idf.toolsPath": "/Users/anthonychen/.espressif"
}
Binary file added components/.DS_Store
Binary file not shown.
5 changes: 3 additions & 2 deletions components/ClientComponent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# target_compile_options(Client.cpp PRIVATE -fpermissive)

idf_component_register(SRCS "Client.cpp"
idf_component_register(SRCS "Client.cpp" "Serialize.cpp"
INCLUDE_DIRS "include"
PRIV_REQUIRES log esp_system esp_partition nvs_flash esp_event esp_netif protocol_examples_common mqtt app_update)
PRIV_REQUIRES log esp_system esp_partition nvs_flash esp_event esp_netif protocol_examples_common mqtt app_update json
EMBED_TXTFILES client.key client.crt ca.crt aws.url)
74 changes: 47 additions & 27 deletions components/ClientComponent/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@
CONDITIONS OF ANY KIND, either express or implied.
*/

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "esp_system.h"
#include "esp_partition.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_partition.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "esp_log.h"
#include "mqtt_client.h"
#include "esp_tls.h"
#include "esp_ota_ops.h"
#include "esp_tls.h"
#include "mqtt_client.h"
#include <sys/param.h>

#include "Credentials.hpp"
#include "Client.hpp"
#include "Credentials.hpp"

static const char *TAG = "mqtts_example";

static void publish(esp_mqtt_client_handle_t client, char data[])
static void publish(esp_mqtt_client_handle_t client, const void *data, size_t len)
{
int msg_id = esp_mqtt_client_publish(client, "zotbins/binid", data, 0, 0, 0);
int msg_id = esp_mqtt_client_publish(client, "binData", (char *)data, len, 1, 0);
ESP_LOGI(TAG, "message published with msg_id=%d", msg_id);
}

Expand All @@ -45,17 +45,21 @@ static void publish(esp_mqtt_client_handle_t client, char data[])
* @param event_id The id for the received event.
* @param event_data The data for the event, esp_mqtt_event_handle_t.
*/
esp_mqtt_client_handle_t test_client;
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id);
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
esp_mqtt_client_handle_t client = event->client;
test_client = client;
int msg_id;
switch ((esp_mqtt_event_id_t)event_id) {
switch ((esp_mqtt_event_id_t)event_id)
{
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
// publish(client, "Test");

// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
Expand All @@ -69,7 +73,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
// msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
// ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
// publish(client, "Hello world!");
ESP_LOGI(TAG, "sent publish successful");
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
Expand All @@ -88,14 +93,19 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT)
{
ESP_LOGI(TAG, "Last error code reported from esp-tls: 0x%x", event->error_handle->esp_tls_last_esp_err);
ESP_LOGI(TAG, "Last tls stack error number: 0x%x", event->error_handle->esp_tls_stack_err);
ESP_LOGI(TAG, "Last captured errno : %d (%s)", event->error_handle->esp_transport_sock_errno,
ESP_LOGI(TAG, "Last captured errno : %d (%s)", event->error_handle->esp_transport_sock_errno,
strerror(event->error_handle->esp_transport_sock_errno));
} else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) {
}
else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED)
{
ESP_LOGI(TAG, "Connection refused error: 0x%x", event->error_handle->connect_return_code);
} else {
}
else
{
ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type);
}
break;
Expand All @@ -110,19 +120,19 @@ static void mqtt_app_start(void)
const esp_mqtt_client_config_t mqtt_cfg = {
.broker = {
.address = {
.uri = "", // input manually
.uri = (const char *)AWS_URL,
},
.verification = {
.certificate = (const char *)AWS_CERT_CA,

.certificate = (const char *)AWS_CA_CRT,
},
},
.credentials = {
.authentication = {
.certificate = (const char *)AWS_CERT_CRT,
.key = (const char *)AWS_CERT_PRIVATE,
}
}
};
.credentials = {.client_id = "SensorBin", .authentication = {
.certificate = (const char *)AWS_CLIENT_CRT,
.key = (const char *)AWS_CLIENT_KEY,
}}};

ESP_LOGI(TAG, "Connecting to AWS server %s", AWS_URL);

ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
Expand All @@ -131,6 +141,16 @@ static void mqtt_app_start(void)
esp_mqtt_client_start(client);
}

void Client::clientPublish(const void *message, size_t len)
{
publish(test_client, message, len);
}

void Client::clientPublishStr(const char *message)
{
Client::clientPublish(message, strlen(message));
}

void Client::clientStart(void)
{
ESP_LOGI(TAG, "[APP] Startup..");
Expand Down
31 changes: 31 additions & 0 deletions components/ClientComponent/Serialize.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "Serialize.hpp"
#include <esp_mac.h>

cJSON *Client::serialize(float fullness, uint32_t usage, bool overflow, int32_t weight)
{
// Use the ESP32's MAC address as a UUID (since it is a UUID)
uint8_t mac_addr_arr[6];
uint64_t mac_addr;
if (esp_base_mac_addr_get(mac_addr_arr) != ESP_OK)
{
// TODO: Handle mac address being unset properly
mac_addr = 0x112233445566;
}
else
{
mac_addr =
((uint64_t)mac_addr_arr[5] << 0) |
((uint64_t)mac_addr_arr[4] << 8) |
((uint64_t)mac_addr_arr[3] << 16) |
((uint64_t)mac_addr_arr[2] << 24) |
((uint64_t)mac_addr_arr[1] << 32) |
((uint64_t)mac_addr_arr[0] << 40);
}
cJSON *root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "bin_id", mac_addr);
cJSON_AddNumberToObject(root, "fullness", fullness);
cJSON_AddNumberToObject(root, "usage", usage);
cJSON_AddBoolToObject(root, "overflow", overflow);
cJSON_AddNumberToObject(root, "weight", weight);
return root;
}
10 changes: 8 additions & 2 deletions components/ClientComponent/include/Client.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#ifndef CLIENT_HPP
#define CLIENT_HPP

namespace Client {
#include <stddef.h>

namespace Client
{
void clientStart(void);
void clientPublish(const void *message, size_t len);
// ONLY pass null terminated strings to this function! It uses strlen!
void clientPublishStr(const char *message);
}

#endif
#endif
15 changes: 11 additions & 4 deletions components/ClientComponent/include/Credentials.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#ifndef CREDENTIALS_HPP
#define CREDENTIALS_HPP

const char* AWS_IOT_ENDPOINT;
const char* AWS_CERT_CA;
const char* AWS_CERT_CRT;
const char* AWS_CERT_PRIVATE;
// These are generated by CMakeLists.txt
// To make sure compilation succeeds, ensure ca.crt, client.crt, aws.url and client.key exist!
// https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#embedding-binary-data
// AWS root CA certificate
extern const uint8_t AWS_CA_CRT[] asm("_binary_ca_crt_start");
// Device-local certificate
extern const uint8_t AWS_CLIENT_CRT[] asm("_binary_client_crt_start");
// Device-local private key
extern const uint8_t AWS_CLIENT_KEY[] asm("_binary_client_key_start");
// URL of AWS endpoint
extern const uint8_t AWS_URL[] asm("_binary_aws_url_start");

#endif
26 changes: 26 additions & 0 deletions components/ClientComponent/include/Serialize.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef CLIENT_SERIALIZE_HPP
#define CLIENT_SERIALIZE_HPP

#include <cJSON.h>
#include <stdint.h>

namespace Client
{
/*
* @brief Serialize data from sensors for API interaction.
*
* The current shape of the JSON object returned is the following:
* {
* "bin_id": 18838586676582,
* "fullness": 0.5,
* "usage": 44,
* "overflow": false,
* "weight": 50
* }
*
* @return cJSON* root of a JSON object containing sensor data. Make sure to free this after you are done using it.
*/
cJSON *serialize(float fullness, uint32_t usage, bool overflow, int32_t weight);
}

#endif
2 changes: 1 addition & 1 deletion components/FullnessComponent/include/FullnessMetric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include "Distance.hpp"
#include <array>
#include <cstdint>
#include <cstddef>
#include <cstdint>

namespace Fullness
{
Expand Down
7 changes: 6 additions & 1 deletion components/TaskComponent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ idf_component_register(SRCS "fullnessTask.cpp"
"usageTask.cpp"
"weightTask.cpp"
"clientTask.cpp"
"servoTask.cpp"
"cameraTask.cpp"
INCLUDE_DIRS "include"
REQUIRES freertos FullnessComponent WeightComponent ClientComponent TaskComponent log driver nvs_flash)
REQUIRES freertos FullnessComponent WeightComponent ClientComponent TaskComponent log driver nvs_flash
json spiffs esp_http_server esp_wifi esp_netif esp_timer
PRIV_REQUIRES nvs_flash esp_psram driver esp32-camera unity mbedtls
)
Loading

0 comments on commit 178f58a

Please sign in to comment.