Initial commit: pmme-cloud and pmme-device

This commit is contained in:
Joseph Ferano 2025-06-02 14:39:01 +07:00
commit ed27c7dc4b
9 changed files with 4184 additions and 0 deletions

1
pmme-cloud/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1813
pmme-cloud/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

11
pmme-cloud/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "pmme-cloud"
version = "0.1.0"
edition = "2024"
[dependencies]
rocket = { version = "0.5.1", features = ["json"] }
serde = "1.0.219"
[dev-dependencies]
mockito = "1.7.0"

64
pmme-cloud/src/main.rs Normal file
View File

@ -0,0 +1,64 @@
use std::sync::{Arc, Mutex};
use rocket::{http::Status, serde::json::Json, Build, Rocket};
use serde::{Deserialize, Serialize};
#[macro_use] extern crate rocket;
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
struct SensorData {
pm00_1: u32,
pm02_5: u32,
pm10_0: u32,
temp: u32,
hum: u32,
}
#[get("/sensor")]
async fn sensor_get(state: &rocket::State<Arc<Mutex<SensorData>>>) -> Json<SensorData> {
Json(state.lock().unwrap().clone())
}
#[put("/sensor", format = "json", data = "<data>")]
fn sensor_put(data: Json<SensorData>, state: &rocket::State<Arc<Mutex<SensorData>>>) -> Status {
*state.lock().unwrap() = data.0;
Status::Ok
}
#[launch]
fn rocket() -> Rocket<Build> {
rocket::build()
.manage(Arc::new(Mutex::new(SensorData::default())))
.mount("/", routes![sensor_get, sensor_put])
}
#[cfg(test)]
mod test {
use rocket::local::blocking::Client;
use rocket::http::Status;
use crate::SensorData;
#[test]
fn test_gets_and_puts_sensor_data() {
let client = Client::tracked(super::rocket()).expect("valid rocket instance");
let response = client.get(uri!(super::sensor_get)).dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_json::<SensorData>().unwrap(), SensorData::default());
let data = SensorData {
pm00_1: 1,
pm02_5: 2,
pm10_0: 3,
temp: 4,
hum: 5,
};
let response = client.put(uri!(super::sensor_put)).json(&data).dispatch();
assert_eq!(response.status(), Status::Ok);
assert!(response.body().is_none());
let response = client.get(uri!(super::sensor_get)).dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_json::<SensorData>().unwrap(), data);
}
}

2
pmme-device/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.cache
build

View File

@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(pmme)

View File

@ -0,0 +1,2 @@
idf_component_register(SRCS "pmme.c"
INCLUDE_DIRS ".")

View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "driver/i2c_master.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_log.h"
#include <string.h>
#include "driver/uart.h"
static const char *TAG = "PMME-Device";
#define UART_NUM UART_NUM_2
#define BUF_SIZE 32
#define UART_BUF_SIZE 128
#define TXD_PIN 17
#define RXD_PIN 16
void app_main(void)
{
ESP_LOGI(TAG, "Initialize UART, Wait 1 second");
vTaskDelay(pdMS_TO_TICKS(1000));
ESP_LOGI(TAG, "Initialize UART, ok now go...");
uart_config_t uart_config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
// .rx_flow_ctrl_thresh = 122,
};
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, UART_BUF_SIZE * 2, 0, 0, NULL, 0));
ESP_LOGI(TAG, "Create a simple pattern");
uint8_t uart_data[BUF_SIZE];
int frequency_check = 2000;
while (1) {
int len = uart_read_bytes(UART_NUM, uart_data, BUF_SIZE, frequency_check / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Received %d bytes from sensor", len);
if (len > 0) {
for(int i = 0; i < len && i < 16; i++) {
ESP_LOGI(TAG, "0x%02X", uart_data[i]);
}
}
vTaskDelay(pdMS_TO_TICKS(frequency_check));
}
}

File diff suppressed because it is too large Load Diff