modified: firmware/main/include/leds.h

new file:   firmware/main/include/uart.h
new file:   firmware/main/include/wifi.h
modified:   firmware/main/src/functions.c
modified:   firmware/main/src/http_client.c
modified:   firmware/main/src/leds.c
modified:   firmware/main/src/main.c
modified:   firmware/main/src/sht31.c
new file:   firmware/main/src/uart.c
new file:   firmware/main/src/wifi.c
This commit is contained in:
Sven Luethi committed 2024-08-11 22:07:45 +02:00
1 parent f4436308dc
commit 5b58ad5e41
10 files changed
+167 -134

No files matched your search

+4 -6
View File
@@ -5,14 +5,12 @@
extern "C" { extern "C" {
#endif #endif
#define LED_INT GPIO_NUM_2
#define LED1 GPIO_NUM_4
#define LED2 GPIO_NUM_5
void led_blink_task(void *pvParameter); void led_blink_task(void *pvParameter);
unsigned initLEDs(); unsigned initLEDs();
void led_on(gpio_num_t ledNumber); void led1_on();
void led_off(gpio_num_t ledNumber); void led2_on();
void led1_off();
void led2_off();
#ifdef __cplusplus #ifdef __cplusplus
} }
+15
View File
@@ -0,0 +1,15 @@
#ifndef UART_H
#define UART_H
#include <esp_err.h>
#ifdef __cplusplus
extern "C" {
#endif
void uart_read_task(void *pvParameters);
#ifdef __cplusplus
}
#endif
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef WIFI_H
#define WIFI_H
#include <esp_err.h>
#ifdef __cplusplus
extern "C" {
#endif
void wifi_init(void);
#ifdef __cplusplus
}
#endif
#endif
+1 -1
View File
@@ -1,5 +1,5 @@
#include <freertos/FreeRTOS.h>
#include "functions.h" #include "functions.h"
#include "freertos/FreeRTOS.h"
static const char * TAG = "functions.c"; static const char * TAG = "functions.c";
+8 -10
View File
@@ -1,15 +1,13 @@
#include "freertos/FreeRTOS.h" #include <freertos/FreeRTOS.h>
#include "freertos/task.h" #include <freertos/task.h>
#include "esp_system.h" #include <esp_system.h>
#include "esp_log.h" #include <esp_log.h>
#include "esp_event_loop.h" #include <esp_event_loop.h>
#include "nvs_flash.h" #include <nvs_flash.h>
#include "esp_wifi.h" #include <esp_wifi.h>
#include "esp_http_client.h" #include <esp_http_client.h>
#include <cJSON.h> #include <cJSON.h>
#include <string.h> #include <string.h>
#include "http_client.h" #include "http_client.h"
#include "functions.h" #include "functions.h"
+34 -20
View File
@@ -1,7 +1,12 @@
#include "leds.h"
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include "esp_log.h" #include <esp_log.h>
#include "leds.h"
#define LED_INT GPIO_NUM_2
#define LED1 GPIO_NUM_4
#define LED2 GPIO_NUM_5
static const char * TAG = "leds.c"; static const char * TAG = "leds.c";
@@ -10,24 +15,21 @@ void led_blink_task(void *pvParameter)
initLEDs(); initLEDs();
while (1) { while (1) {
//gpio_set_level(LED_INT, 1);
ESP_LOGI(TAG, "Blink LEDs..."); ESP_LOGI(TAG, "Blink LEDs...");
// Blink the LEDs // Blink the LEDs
gpio_set_level(LED1, 0); gpio_set_level(LED1, 0);
gpio_set_level(LED2, 0); gpio_set_level(LED2, 0);
//gpio_set_level(LED_INT, 1); vTaskDelay(700 / portTICK_PERIOD_MS);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 0);
gpio_set_level(LED2, 1);
//gpio_set_level(LED_INT, 0);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 1); gpio_set_level(LED1, 1);
gpio_set_level(LED2, 0); gpio_set_level(LED2, 0);
//gpio_set_level(LED_INT, 1); vTaskDelay(100 / portTICK_PERIOD_MS);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 1); gpio_set_level(LED1, 1);
gpio_set_level(LED2, 1); gpio_set_level(LED2, 1);
//gpio_set_level(LED_INT, 0); vTaskDelay(100 / portTICK_PERIOD_MS);
vTaskDelay(500 / portTICK_PERIOD_MS); gpio_set_level(LED1, 0);
gpio_set_level(LED2, 1);
vTaskDelay(100 / portTICK_PERIOD_MS);
} }
} }
@@ -38,16 +40,28 @@ unsigned initLEDs() {
return 0; return 0;
} }
void led_on(gpio_num_t ledNumber) void led1_on() {
{ gpio_set_level(LED1, 1);
gpio_set_level(ledNumber, 1); ESP_LOGI(TAG, "LED 1 turned on");
ESP_LOGI(TAG, "LED turned on");
} }
void led_off(gpio_num_t ledNumber) void led2_on() {
{ gpio_set_level(LED2, 1);
gpio_set_level(ledNumber, 0); ESP_LOGI(TAG, "LED 2 turned on");
ESP_LOGI(TAG, "LED turned off");
} }
void led1_off()
{
gpio_set_level(LED1, 0);
ESP_LOGI(TAG, "LED 1 turned off");
}
void led2_off()
{
gpio_set_level(LED2, 0);
ESP_LOGI(TAG, "LED 2 turned off");
}
+12 -93
View File
@@ -1,103 +1,23 @@
#include "freertos/FreeRTOS.h" #include <freertos/FreeRTOS.h>
#include "freertos/task.h" #include <freertos/task.h>
#include "nvs_flash.h" #include <nvs_flash.h>
#include "esp_system.h" #include <esp_system.h>
#include "esp_log.h" //#include <esp_log.h>
#include "esp_event_loop.h" //#include <esp_event_loop.h>
#include "esp_wifi.h"
#include "esp_http_client.h"
#include <esp_err.h> #include <esp_err.h>
#include "driver/uart.h" //#include <EEPROM.h>
//#include <stdio.h>
#include <string.h>
#include "leds.h" #include "leds.h"
#include "sht31.h" #include "sht31.h"
#include "uart.h"
#include "wifi.h"
#include "functions.h"
#include "http_client.h" #include "http_client.h"
static const char * TAG = "main.c"; static const char * TAG = "main.c";
static void wifi_init(void) {
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
wifi_config_t sta_config = {
.sta = {
.ssid = CONFIG_ESP_WIFI_SSID,
.password = CONFIG_ESP_WIFI_PASSWORD,
},
};
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
}
static void split_float(float num, int *int_part, float *dec_part) {
// Extract the integer part
*int_part = (float)((int)num);
// Calculate the fractional part by subtracting the integer part from the original number
*dec_part = num - *int_part;
}
// Task to read data from UART
void uart_read_task(void *pvParameters) {
const unsigned bufferSize = 1024;
// Initialize UART
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(UART_NUM_0, &uart_config);
//uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_NUM_0, bufferSize * 2, 0, 0, NULL, 0);
// Capture input
uint8_t data[bufferSize];
while (1) {
// Read data from UART
int length = uart_read_bytes(UART_NUM_0, data, bufferSize, 20 / portTICK_RATE_MS);
if (length > 0) {
// Null-terminate the received string
data[length] = '\0';
// Print the received data
printf("Received data: %s\n", data);
if(strcmp((void*)data,"1") == 0) led_on(LED1);
else if(strcmp((void*)data,"2") == 0) led_on(LED2);
else if(strcmp((void*)data,"3") == 0) led_on(LED_INT);
else if(strcmp((void*)data,"4") == 0) {
led_off(LED1);
led_off(LED2);
led_off(LED_INT);
}
}
vTaskDelay(100 / portTICK_RATE_MS); // Delay to prevent task from consuming too much CPU time
}
}
/*
void main_task(void* args) {
float humidity = 0;
float temperature = 0;
vTaskDelay(2000 / portTICK_PERIOD_MS);
//Register device with Server
unsigned id;
hc_register(&id);
while (1)
{
hc_send_sensor_data(id,0,0); //test
}
}
*/
void app_main() void app_main()
{ {
//ESP_ERROR_CHECK(nvs_flash_init()); //ESP_ERROR_CHECK(nvs_flash_init());
@@ -106,7 +26,6 @@ void app_main()
initLEDs(); initLEDs();
xTaskCreate(&led_blink_task, "led_blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL); xTaskCreate(&led_blink_task, "led_blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
//xTaskCreate(uart_read_task, "uart_read_task", 2048, NULL, 10, NULL); xTaskCreate(uart_read_task, "uart_read_task", 2048, NULL, 10, NULL);
xTaskCreate(sht31_sensor_test_task, "sht31_sensor_test_task", 2048, NULL, 10, NULL); xTaskCreate(sht31_sensor_test_task, "sht31_sensor_test_task", 2048, NULL, 10, NULL);
//xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL);
} }
+4 -4
View File
@@ -1,10 +1,10 @@
#include "sht31.h" #include <freertos/FreeRTOS.h>
#include <stdio.h>
#include <driver/i2c.h>
//#include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <driver/i2c.h>
#include <stdio.h>
#include <esp_log.h> #include <esp_log.h>
#include "functions.h" #include "functions.h"
#include "sht31.h"
#define SDA_PIN GPIO_NUM_2 #define SDA_PIN GPIO_NUM_2
#define CLK_PIN GPIO_NUM_14 #define CLK_PIN GPIO_NUM_14
+48
View File
@@ -0,0 +1,48 @@
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/uart.h>
#include <string.h>
#include <esp_log.h>
#include "uart.h"
#include "functions.h"
#include "leds.h"
static const char * TAG = "uart.c";
// Task to read data from UART
void uart_read_task(void *pvParameters) {
const unsigned bufferSize = 1024;
// Initialize UART
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(UART_NUM_0, &uart_config);
//uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_NUM_0, bufferSize * 2, 0, 0, NULL, 0);
// Capture input
uint8_t data[bufferSize];
while (1) {
// Read data from UART
int length = uart_read_bytes(UART_NUM_0, data, bufferSize, 20 / portTICK_RATE_MS);
if (length > 0) {
// Null-terminate the received string
data[length] = '\0';
// Print the received data
printf("Received data: %s\n", data);
if(strcmp((void*)data,"1") == 0) led1_on();
else if(strcmp((void*)data,"2") == 0) led2_on();
else if(strcmp((void*)data,"3") == 0) {
led1_off();
led2_off();
}
}
vTaskDelay(100 / portTICK_RATE_MS); // Delay to prevent task from consuming too much CPU time
}
}
+26
View File
@@ -0,0 +1,26 @@
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_log.h>
#include <esp_wifi.h>
#include <esp_http_client.h>
#include "functions.h"
#include "wifi.h"
void wifi_init(void) {
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
wifi_config_t sta_config = {
.sta = {
.ssid = CONFIG_ESP_WIFI_SSID,
.password = CONFIG_ESP_WIFI_PASSWORD,
},
};
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
}