Compare commits

...
3 Commits
4 changed files with 1211 additions and 426 deletions

No files matched your search

+111 -6
View File
@@ -12,8 +12,15 @@
#include "driver/i2c_master.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "ssd1306/ssd1306.h"
#include "esp_wifi.h"
#include "esp_http_server.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "esp_event.h"
static const char *TAG = "PMME-Device";
#define I2C_HOST 0
@@ -29,6 +36,8 @@ static const char *TAG = "PMME-Device";
#define UART_BUF_SIZE 128
#define TXD_PIN 17
#define RXD_PIN 16
#define BUTTON_PIN GPIO_NUM_5
#define LED_PIN GPIO_NUM_19
#define SWAP_BYTES(x) ((x >> 8) | (x << 8))
@@ -69,7 +78,7 @@ void send_pms7003_command(uint8_t cmd, uint8_t datah, uint8_t datal) {
void read_pm_data(uint8_t *buf) {
uint16_t checksum = 0;
for (int i = 0; i <= 29; i++) {
for (int i = 0; i <= 28; i++) {
checksum += buf[i];
}
uint16_t c = (((uint16_t)buf[30]) << 8) | ((uint16_t)buf[31]);
@@ -121,13 +130,12 @@ void pm_sensor_task(void *pvParameters) {
int len = uart_read_bytes(UART_NUM, uart_data, BUF_SIZE, 100 / portTICK_PERIOD_MS);
// ESP_LOGI(TAG, "Received %d bytes from sensor", len);
if (len > 0) {
if (len == 32 && (uart_data[0] != 0x42 || uart_data[1] != 0x4D)) {
if (uart_data[0] != 0x42 || uart_data[1] != 0x4D) {
ESP_LOGW(TAG, "Frame Start does not match 0x42, 0x4D, instead got %x %x", uart_data[0], uart_data[1]);
} else {
} else if (len == 32) {
read_pm_data(uart_data);
// for(int i = 0; i < len && i < BUF_SIZE; i++) {
// ESP_LOGI(TAG, "0x%02X", uart_data[i]);
// }
} else {
ESP_LOGW(TAG, "Unexpected frame length: %d", len);
}
}
uart_flush(UART_NUM);
@@ -202,6 +210,10 @@ void oled_display_task_manual(void *pvParameters) {
void oled_display_task(void *pvParameters) {
init_ssd1306();
char pm25_str[128];
ssd1306_fill_screen();
vTaskDelay(pdMS_TO_TICKS(3000));
while (1) {
uint16_t pm25_value = 0;
if (xSemaphoreTake(sensor_data_mutex, pdMS_TO_TICKS(10))) {
@@ -219,6 +231,97 @@ void oled_display_task(void *pvParameters) {
}
}
void button_led_task(void *pvParameters) {
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << LED_PIN),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL << BUTTON_PIN);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
gpio_set_level(LED_PIN, 0);
int curr_state = 1; // HIGH
bool is_pressed = false;
while (1) {
int level = gpio_get_level(BUTTON_PIN);
is_pressed = false;
if (curr_state == 1 && level == 0) {
gpio_set_level(LED_PIN, 1);
curr_state = 0;
is_pressed = true;
} else if (curr_state == 0 && level == 1) {
gpio_set_level(LED_PIN, 0);
curr_state = 1;
is_pressed = true;
}
vTaskDelay(pdMS_TO_TICKS(is_pressed ? 100 : 10));
}
}
static esp_err_t provision_handler(httpd_req_t *req) {
const char *response = "<html><body>Hello world!</body></html>";
httpd_resp_send(req, response, strlen(response));
return ESP_OK;
}
void wifi_task(void *pvParameters) {
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = "PMME-Wifi",
.password = "88888888",
.ssid_len = strlen("PMME-Wifi"),
.channel = 1,
.authmode = WIFI_AUTH_WPA2_PSK,
.max_connection = 2,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "WiFi AP started: SSID=PMME-Wifi");
httpd_handle_t server = NULL;
httpd_config_t server_config = HTTPD_DEFAULT_CONFIG();
ESP_ERROR_CHECK(httpd_start(&server, &server_config));
httpd_uri_t provision_uri = {
.uri = "/api/provision",
.method = HTTP_GET,
.handler = provision_handler,
.user_ctx = NULL,
};
ESP_ERROR_CHECK(httpd_register_uri_handler(server, &provision_uri));
ESP_LOGI(TAG, "HTTP server started on port %d", server_config.server_port);
while (1) {
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void app_main(void) {
sensor_data_mutex = xSemaphoreCreateMutex();
if (sensor_data_mutex == NULL) {
@@ -227,4 +330,6 @@ void app_main(void) {
}
xTaskCreate(pm_sensor_task, "pm_sensor_task", 4096, NULL, 5, NULL);
xTaskCreate(oled_display_task, "oled_display_task", 4096, NULL, 5, NULL);
xTaskCreate(button_led_task, "button_led_task", 2048, NULL, 5, NULL);
xTaskCreate(wifi_task, "wifi_task", 8192, NULL, 5, NULL);
}
@@ -559,3 +559,19 @@ esp_err_t i2c_ssd1306_buffer_to_ram(i2c_ssd1306_handle_t *i2c_ssd1306)
return err;
}
// My Functions
esp_err_t ssd1306_fill_screen(void) {
i2c_ssd1306_buffer_fill(&i2c_ssd1306);
return i2c_ssd1306_buffer_to_ram(&i2c_ssd1306);
}
void init_ssd1306_no_splash(void)
{
i2c_new_master_bus(&i2c_master_bus_config, &i2c_master_bus);
i2c_ssd1306_init(i2c_master_bus, i2c_ssd1306_config, &i2c_ssd1306);
// i2c_ssd1306_buffer_image(&i2c_ssd1306, 32, 0, (const uint8_t *)ssd1306_logo, 64, 64, false);
// i2c_ssd1306_buffer_to_ram(&i2c_ssd1306);
vTaskDelay(1000 / portTICK_PERIOD_MS);
i2c_ssd1306_buffer_clear(&i2c_ssd1306);
}
@@ -282,3 +282,8 @@ esp_err_t i2c_ssd1306_pages_to_ram(i2c_ssd1306_handle_t *i2c_ssd1306, uint8_t in
* @return ESP_OK on success, or an error code otherwise.
*/
esp_err_t i2c_ssd1306_buffer_to_ram(i2c_ssd1306_handle_t *i2c_ssd1306);
// My Functions;
esp_err_t ssd1306_fill_screen(void);
void init_ssd1306_no_splash(void);
File diff suppressed because it is too large. Load diff