Browse Source

Implement sntp client and add display functionality

master
Christian Loch 9 months ago
parent
commit
f386f2783e
12 changed files with 170 additions and 126 deletions
  1. +31
    -14
      .cproject
  2. +3
    -0
      components/clock/CMakeLists.txt
  3. +58
    -0
      components/clock/clock.c
  4. +28
    -0
      components/clock/clock.h
  5. +1
    -1
      components/display/CMakeLists.txt
  6. +29
    -5
      components/display/display.c
  7. +5
    -7
      components/display/display.h
  8. +2
    -79
      components/server/server.c
  9. +1
    -17
      components/wifi/wifi.c
  10. +1
    -1
      components/wifi/wifi.h
  11. BIN
      doc/display_pins.png
  12. +11
    -2
      main/main.c

+ 31
- 14
.cproject View File

@@ -1,17 +1,34 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="org.eclipse.cdt.core.default.config.248586075">
<storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.248586075" moduleId="org.eclipse.cdt.core.settings" name="Configuration">
<externalSettings/>
<extensions/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.pathentry">
<pathentry kind="src" path=""/>
<pathentry excluding="**/CMakeFiles/**" kind="out" path="build"/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="org.eclipse.cdt.core.default.config.248586075">
<storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.248586075" moduleId="org.eclipse.cdt.core.settings" name="Configuration">
<externalSettings/>
<extensions/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.pathentry">
<pathentry kind="src" path=""/>
<pathentry excluding="**/CMakeFiles/**" kind="out" path="build"/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
</cproject>

+ 3
- 0
components/clock/CMakeLists.txt View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "clock.c"
INCLUDE_DIRS "."
REQUIRES )

+ 58
- 0
components/clock/clock.c View File

@@ -0,0 +1,58 @@
/*
* clock.c
*
* Created on: Apr 23, 2020
* Author: chris
*/

#include "clock.h"

const char* TAG = "clock";

void time_sync_notification_cb(struct timeval *tv)
{
ESP_LOGI(TAG, "Synchronized time to PTB!");
}

esp_err_t init_clock()
{
ESP_LOGI(TAG, "Initializing SNTP");
esp_event_loop_create_default();
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "ptbtime1.ptb.de");
sntp_set_time_sync_notification_cb(time_sync_notification_cb);
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
sntp_init();

// wait for time to be set
time_t now = 0;
struct tm timeinfo = { 0 };
int retry = 0;
const int retry_count = 10;
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
time(&now);
localtime_r(&now, &timeinfo);

setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 1);
tzset();

return ESP_OK;
}

void get_time(time_str_t* res)
{
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);

char date_str[64];
char time_str[64];
strftime(date_str, sizeof(date_str), "%d.%m.%Y", &timeinfo);
strftime(time_str, sizeof(time_str), "%H:%M:%S", &timeinfo);
strcpy(res->date_str, date_str);
strcpy(res->time_str, time_str);
}

+ 28
- 0
components/clock/clock.h View File

@@ -0,0 +1,28 @@
/*
* clock.h
*
* Created on: Apr 23, 2020
* Author: chris
*/

#ifndef COMPONENTS_CLOCK_CLOCK_H_
#define COMPONENTS_CLOCK_CLOCK_H_

#include <time.h>
#include <sys/time.h>
#include <esp_system.h>
#include <esp_log.h>
#include <string.h>
#include <esp_sntp.h>
#include "esp_event.h"

typedef struct {
char date_str[64];
char time_str[64];
} time_str_t;

esp_err_t init_clock();

void get_time(time_str_t* res);

#endif /* COMPONENTS_CLOCK_CLOCK_H_ */

+ 1
- 1
components/display/CMakeLists.txt View File

@@ -1,3 +1,3 @@
idf_component_register(SRCS "display.c"
INCLUDE_DIRS "."
REQUIRES tft)
REQUIRES tft clock)

+ 29
- 5
components/display/display.c View File

@@ -56,13 +56,14 @@ layout_t create_layout()
layout.margin = 5;
layout.borderVMargin = 10;

TFT_setFont(UBUNTU16_FONT, NULL);
TFT_setFont(SMALL_FONT, NULL);
layout.datetimebarH = 20;
layout.datetimeW = TFT_getStringWidth("25.12.2031 08:31");
layout.datetimeW = TFT_getStringWidth("25.12.2031 08:31:11");
layout.datetimeA = TFT_getfontheight();
layout.dateTimeLeft = (screenW - layout.datetimeW) / 2;
layout.datetimeBaseline = (layout.datetimebarH - layout.datetimeA) / 2;

TFT_setFont(UBUNTU16_FONT, NULL);
layout.innenW = TFT_getStringWidth("Innen");
layout.aussenW = TFT_getStringWidth("Aussen");
layout.locA = TFT_getfontheight();
@@ -240,16 +241,24 @@ void print_humid2(uint32_t humidity_raw)
TFT_print(humid_str, layout.aussenLeft, layout.humBaseline);
}

void print_time(time_str_t time)
{
TFT_setFont(SMALL_FONT, NULL);
char datetime_str[129];
sprintf(datetime_str, "%s %s", time.date_str, time.time_str);
TFT_print(datetime_str, layout.dateTimeLeft, layout.datetimeBaseline);
}

void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw)
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw,
time_str_t time)
{
// HEADER
TFT_setFont(UBUNTU16_FONT, NULL);
tft_fg = TFT_WHITE;
TFT_print("25.12.2031 08:31", layout.dateTimeLeft, layout.datetimeBaseline);
TFT_drawFastHLine(0, 20, 160, TFT_WHITE);

// IN OUT LABEL
TFT_setFont(UBUNTU16_FONT, NULL);
TFT_print("Innen", layout.innenLeft, layout.innenBaseline);
TFT_print("Aussen", layout.aussenLeft, layout.aussenBaseline);

@@ -260,6 +269,7 @@ void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw
print_humid2(humidity2_raw);
print_press1(pressure_raw);
print_press2(pressure2_raw);
print_time(time);

// UNIT LABELS
TFT_setFont(DEJAVU18_FONT, NULL);
@@ -271,6 +281,20 @@ void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw
TFT_print("hPa", layout.unitLeft, layout.pressBaseline);
}

void update_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw,
time_str_t time)
{
// VALUES
print_temp1(temp_raw);
print_temp2(temp2_raw);
print_humid1(humidity_raw);
print_humid2(humidity2_raw);
print_press1(pressure_raw);
print_press2(pressure2_raw);
print_time(time);
}

esp_err_t init_display()
{
tft_max_rdclock = 4000000;


+ 5
- 7
components/display/display.h View File

@@ -12,16 +12,14 @@
#include <freertos/task.h>
#include <esp_log.h>
#include <esp_err.h>

//#include "esp32_ucglib_hal.h"
//#include "ucg.h"
#include "clock.h"

esp_err_t init_display();
void display_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw);
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw,
time_str_t time);
void update_data(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw);

void test_tft_lib();
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw,
time_str_t time);

#endif /* COMPONENTS_DISPLAY_DISPLAY_H_ */

+ 2
- 79
components/server/server.c View File

@@ -13,7 +13,8 @@ char cur_value_str[255] = "No measurements yet!";
void server_set_values(int32_t temp_raw, uint32_t pressure_raw, uint32_t humidity_raw,
int32_t temp2_raw, uint32_t pressure2_raw, uint32_t humidity2_raw)
{
sprintf(cur_value_str, "<html><body><p>%d %d %d</p><p>%d %d %d</p></body></html>", temp_raw, pressure_raw, humidity_raw, temp2_raw, pressure2_raw, humidity2_raw);
sprintf(cur_value_str, "<html><body><p>%d %d %d</p><p>%d %d %d</p></body></html>",
temp_raw, pressure_raw, humidity_raw, temp2_raw, pressure2_raw, humidity2_raw);
}

static esp_err_t get_handler(httpd_req_t *req)
@@ -33,60 +34,9 @@ static esp_err_t get_handler(httpd_req_t *req)
free(buf);
}

// buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
// if (buf_len > 1) {
// buf = malloc(buf_len);
// if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) {
// ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf);
// }
// free(buf);
// }
//
// buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1;
// if (buf_len > 1) {
// buf = malloc(buf_len);
// if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) {
// ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf);
// }
// free(buf);
// }

// /* Read URL query string length and allocate memory for length + 1,
// * extra byte for null termination */
// buf_len = httpd_req_get_url_query_len(req) + 1;
// if (buf_len > 1) {
// buf = malloc(buf_len);
// if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query => %s", buf);
// char param[32];
// /* Get value of expected key from query string */
// if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
// }
// if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
// }
// if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
// ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
// }
// }
// free(buf);
// }

/* Set some custom headers */
// httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1");
// httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2");

/* Send response with custom headers and body set as the
* string passed in user context*/
const char* resp_str = cur_value_str;
httpd_resp_send(req, resp_str, strlen(resp_str));

/* After sending the HTTP response the old HTTP request
* headers are lost. Check if HTTP request headers can be read now. */
// if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
// ESP_LOGI(TAG, "Request headers lost");
// }
return ESP_OK;
}

@@ -114,33 +64,6 @@ static httpd_handle_t start_webserver(void)
return NULL;
}

static void stop_webserver(httpd_handle_t server)
{
// Stop the httpd server
httpd_stop(server);
}

static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server == NULL) {
ESP_LOGI(TAG, "Starting webserver");
*server = start_webserver();
}
}

static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server) {
ESP_LOGI(TAG, "Stopping webserver");
stop_webserver(*server);
*server = NULL;
}
}

void init_server()
{
start_webserver();


+ 1
- 17
components/wifi/wifi.c View File

@@ -37,7 +37,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
}
}

void wifi_init_sta()
void init_wifi_sta()
{
//Initialize NVS
esp_err_t ret = nvs_flash_init();
@@ -56,19 +56,6 @@ void wifi_init_sta()
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

//tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
//tcpip_adapter_ip_info_t info;
//ip4_addr_t gw;
//gw.addr = ipaddr_addr("192.168.0.1");
//info.gw = gw;
//ip4_addr_t ip;
//ip.addr = ipaddr_addr("192.168.0.110");
//info.ip = ip;
//ip4_addr_t netmask;
//netmask.addr = ipaddr_addr("255.255.255.0");
//info.netmask = netmask;
//tcpip_adapter_sta_start(0, &info);

ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));

@@ -105,7 +92,4 @@ void wifi_init_sta()
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}

//ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
//ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
//vEventGroupDelete(s_wifi_event_group);
}

+ 1
- 1
components/wifi/wifi.h View File

@@ -15,6 +15,6 @@
#include <freertos/task.h>
#include "nvs_flash.h"

void wifi_init_sta();
void init_wifi_sta();

#endif /* COMPONENTS_WIFI_WIFI_H_ */

BIN
doc/display_pins.png View File

Before After
Width: 1018  |  Height: 526  |  Size: 278KB

+ 11
- 2
main/main.c View File

@@ -3,6 +3,7 @@
#include "sensors.h"
#include "wifi.h"
#include "server.h"
#include "clock.h"
#include <esp_log.h>

void app_main(void)
@@ -13,17 +14,25 @@ void app_main(void)
int32_t temp2 = 0;
uint32_t pressure2 = 0;
uint32_t humidity2 = 0;
time_str_t time;

init_sensors();
init_display();
wifi_init_sta();
init_wifi_sta();
init_server();
init_clock();

/* Draw the whole screen one time */
get_time(&time);
read_sensor(&temp, &pressure, &humidity);
read_sensor2(&temp2, &pressure2, &humidity2);
display_data(temp, pressure, humidity, temp2, pressure2, humidity2, time);
while (1) {
get_time(&time);
read_sensor(&temp, &pressure, &humidity);
read_sensor2(&temp2, &pressure2, &humidity2);
server_set_values(temp, pressure, humidity, temp2, pressure2, humidity2);
display_data(temp, pressure, humidity, temp2, pressure2, humidity2);
update_data(temp, pressure, humidity, temp2, pressure2, humidity2, time);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}


Loading…
Cancel
Save