modified: firmware/main/src/controller.c
modified: firmware/main/src/leds.c modified: firmware/main/src/main.c modified: firmware/main/src/wifi.c
This commit is contained in:
1 parent
19b7b5791f
commit
7db85d712e
4 files changed
+64
-30
No files matched your search
@@ -6,7 +6,7 @@
|
|||||||
#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 "controller.h"
|
#include "controller.h"
|
||||||
#include "functions.h"
|
#include "functions.h"
|
||||||
@@ -71,6 +71,7 @@ esp_err_t send_sensor_data(unsigned id, float humidity, float temperature) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Create cJSON object
|
// Create cJSON object
|
||||||
|
/*
|
||||||
cJSON *json_payload = cJSON_CreateObject();
|
cJSON *json_payload = cJSON_CreateObject();
|
||||||
if (json_payload == NULL) {
|
if (json_payload == NULL) {
|
||||||
ESP_LOGE(TAG, "Failed to create JSON object");
|
ESP_LOGE(TAG, "Failed to create JSON object");
|
||||||
@@ -78,22 +79,14 @@ esp_err_t send_sensor_data(unsigned id, float humidity, float temperature) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add data to the JSON object
|
// Add data to the JSON object
|
||||||
if (!cJSON_AddNumberToObject(json_payload, "temperature", temperature) ||
|
if (
|
||||||
|
!cJSON_AddNumberToObject(json_payload, "temperature", temperature) ||
|
||||||
!cJSON_AddNumberToObject(json_payload, "humidity", humidity) ||
|
!cJSON_AddNumberToObject(json_payload, "humidity", humidity) ||
|
||||||
!cJSON_AddNumberToObject(json_payload, "id", id)) {
|
!cJSON_AddNumberToObject(json_payload, "id", id)) {
|
||||||
ESP_LOGE(TAG, "Failed to add data to JSON object");
|
ESP_LOGE(TAG, "Failed to add data to JSON object");
|
||||||
cJSON_Delete(json_payload);
|
cJSON_Delete(json_payload);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
char tmp[100] = "{\"humidity\": ";
|
|
||||||
strcat(tmp, floatToString(humidity));
|
|
||||||
strcat(tmp, ",\"temperature\": ");
|
|
||||||
strcat(tmp, floatToString(temperature));
|
|
||||||
strcat(tmp, ",\"id\": ");
|
|
||||||
strcat(tmp, intToString(id));
|
|
||||||
strcat(tmp, "}");
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Print JSON object
|
// Print JSON object
|
||||||
char *json_string = cJSON_PrintUnformatted(json_payload);
|
char *json_string = cJSON_PrintUnformatted(json_payload);
|
||||||
@@ -102,15 +95,25 @@ esp_err_t send_sensor_data(unsigned id, float humidity, float temperature) {
|
|||||||
cJSON_Delete(json_payload);
|
cJSON_Delete(json_payload);
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Set up htttp client...");
|
char msg[60] = "{\"humidity\":";
|
||||||
|
strcat(msg, floatToString(humidity));
|
||||||
|
strcat(msg, ",\"temperature\":");
|
||||||
|
strcat(msg, floatToString(temperature));
|
||||||
|
strcat(msg, ",\"id\":");
|
||||||
|
strcat(msg, intToString(id));
|
||||||
|
strcat(msg, "}");
|
||||||
|
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Set up http client...");
|
||||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||||
|
|
||||||
// Set HTTP headers
|
// Set HTTP headers
|
||||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||||
// Set POST data
|
// Set POST data
|
||||||
esp_http_client_set_post_field(client, json_string, strlen(json_string));
|
//esp_http_client_set_post_field(client, json_string, strlen(json_string));
|
||||||
|
esp_http_client_set_post_field(client, msg, strlen(msg));
|
||||||
|
|
||||||
// Perform the HTTP POST request
|
// Perform the HTTP POST request
|
||||||
err = esp_http_client_perform(client);
|
err = esp_http_client_perform(client);
|
||||||
@@ -133,8 +136,8 @@ esp_err_t send_sensor_data(unsigned id, float humidity, float temperature) {
|
|||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
esp_http_client_cleanup(client);
|
esp_http_client_cleanup(client);
|
||||||
cJSON_Delete(json_payload);
|
//cJSON_Delete(json_payload);
|
||||||
free(json_string);
|
//free(json_string);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -38,6 +38,8 @@ esp_err_t led_init() {
|
|||||||
gpio_set_direction(LED2,GPIO_MODE_OUTPUT);
|
gpio_set_direction(LED2,GPIO_MODE_OUTPUT);
|
||||||
gpio_set_direction(LED_INT,GPIO_MODE_OUTPUT);
|
gpio_set_direction(LED_INT,GPIO_MODE_OUTPUT);
|
||||||
led0_on();
|
led0_on();
|
||||||
|
led1_off();
|
||||||
|
led2_off();
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ void mainTask(void *pvParameters) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
void app_main() {
|
void app_main() {
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
//ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
ESP_ERROR_CHECK(led_init());
|
ESP_ERROR_CHECK(led_init());
|
||||||
ESP_ERROR_CHECK(wifi_init());
|
ESP_ERROR_CHECK(wifi_init());
|
||||||
ESP_ERROR_CHECK(sht31_init());
|
ESP_ERROR_CHECK(sht31_init());
|
||||||
@@ -68,20 +68,23 @@ void app_main() {
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (sht31_read_temp_hum(&temperature, &humidity) == ESP_OK) {
|
if (sht31_read_temp_hum(&temperature, &humidity) == ESP_OK) {
|
||||||
split_float(temperature, &temp_int_part, &temp_dec_part);
|
//split_float(temperature, &temp_int_part, &temp_dec_part);
|
||||||
split_float(humidity, &hum_int_part, &hum_dec_part);
|
//split_float(humidity, &hum_int_part, &hum_dec_part);
|
||||||
ESP_LOGI(TAG, "Temperature: %i.%i°C, Humidity: %i.%i%%", temp_int_part, (int)(temp_dec_part*1000), hum_int_part, (int)(hum_dec_part*1000));
|
//ESP_LOGI(TAG, "Temperature: %i.%i°C, Humidity: %i.%i%%", temp_int_part, (int)(temp_dec_part*1000), hum_int_part, (int)(hum_dec_part*1000));
|
||||||
|
ESP_LOGI(TAG, "Temperature: %s°C, Humidity: %s%%", floatToString(temperature), floatToString(humidity));
|
||||||
|
|
||||||
if(wifi_on() == ESP_OK) {
|
if(wifi_on() == ESP_OK) {
|
||||||
// Send temperature...
|
// Send temperature...
|
||||||
send_sensor_data(0, humidity, temperature);
|
ESP_LOGI(TAG, "Sending temperature and humidity values to control node...");
|
||||||
|
send_sensor_data(1, humidity, temperature);
|
||||||
|
ESP_LOGI(TAG, "Done sending temperature and humidity values to control node.");
|
||||||
wifi_off();
|
wifi_off();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ESP_LOGE(TAG, "Failed to read from SHT31 sensor");
|
ESP_LOGE(TAG, "Failed to read from SHT31 sensor");
|
||||||
}
|
}
|
||||||
vTaskDelay(10000 / portTICK_RATE_MS);
|
vTaskDelay(60000 / portTICK_RATE_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,38 @@
|
|||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
//#include <esp_http_client.h>
|
//#include <esp_http_client.h>
|
||||||
|
//#include "esp_netif.h"
|
||||||
#include "functions.h"
|
#include "functions.h"
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
#include "leds.h"
|
#include "leds.h"
|
||||||
|
|
||||||
static const char * TAG = "wifi.c";
|
static const char * TAG = "wifi.c";
|
||||||
|
|
||||||
esp_err_t wifi_init(void) {
|
esp_err_t wifi_init() {
|
||||||
|
// Init IP Stack
|
||||||
tcpip_adapter_init();
|
tcpip_adapter_init();
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool wait_for_connected() {
|
||||||
|
tcpip_adapter_ip_info_t ip_info;
|
||||||
|
|
||||||
|
unsigned repeats = 10;
|
||||||
|
for(unsigned i = 0; i < repeats; ++i)
|
||||||
|
{
|
||||||
|
if ( tcpip_adapter_get_ip_info( TCPIP_ADAPTER_IF_STA, &ip_info ) == ESP_OK && ip_info.ip.addr != 0 ) {
|
||||||
|
ESP_LOGI(TAG, "IP Address: " IPSTR, IP2STR(&ip_info.ip));
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||||
|
}
|
||||||
|
ESP_LOGE(TAG, "Timed out waiting for ip address from dhcp server.");
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to turn on WiFi
|
||||||
|
esp_err_t wifi_on() {
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||||
@@ -34,18 +56,20 @@ esp_err_t wifi_init(void) {
|
|||||||
//ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM));
|
//ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MIN_MODEM));
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MAX_MODEM));
|
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MAX_MODEM));
|
||||||
|
|
||||||
return ESP_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to turn on WiFi
|
|
||||||
esp_err_t wifi_on() {
|
|
||||||
ESP_LOGI(TAG, "Turning WiFi ON...");
|
ESP_LOGI(TAG, "Turning WiFi ON...");
|
||||||
esp_wifi_start(); // Start WiFi
|
esp_wifi_start(); // Start WiFi
|
||||||
ESP_LOGI(TAG, "WiFi is ON and connecting...");
|
ESP_LOGI(TAG, "WiFi is ON and connecting...");
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
ESP_LOGI(TAG, "WiFi is ON and connected to AP.");
|
|
||||||
led1_on();
|
led1_on();
|
||||||
|
ESP_LOGI(TAG, "WiFi is ON and connected to AP.");
|
||||||
|
if (wait_for_connected() == ESP_OK) {
|
||||||
|
led2_on();
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
wifi_off();
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to turn off WiFi
|
// Function to turn off WiFi
|
||||||
@@ -54,7 +78,9 @@ esp_err_t wifi_off() {
|
|||||||
esp_wifi_disconnect();
|
esp_wifi_disconnect();
|
||||||
ESP_LOGI(TAG, "WiFi disconnected. Turning WiFi OFF...");
|
ESP_LOGI(TAG, "WiFi disconnected. Turning WiFi OFF...");
|
||||||
esp_wifi_stop(); // Stop WiFi
|
esp_wifi_stop(); // Stop WiFi
|
||||||
ESP_LOGI(TAG, "WiFi is OFF.");
|
esp_wifi_deinit();
|
||||||
led1_off();
|
led1_off();
|
||||||
|
led2_off();
|
||||||
|
ESP_LOGI(TAG, "WiFi is OFF.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user