Skip to content

Instantly share code, notes, and snippets.

@nickfox-taterli
Created April 6, 2021 09:44
Show Gist options
  • Save nickfox-taterli/632a1d145c220e34d25ecaaddd27f2cf to your computer and use it in GitHub Desktop.
Save nickfox-taterli/632a1d145c220e34d25ecaaddd27f2cf to your computer and use it in GitHub Desktop.
ESP-01 + DHT11 小板监测站工具
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "protocol_examples_common.h"
#include "nvs.h"
#include "nvs_flash.h"
#include <dht.h>
#include <netdb.h>
#include <sys/socket.h>
#include "mbedtls/platform.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/esp_debug.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/error.h"
#include "mbedtls/certs.h"
#define WEB_SERVER "test.thekoziolfoundation.com"
#define WEB_PORT "443"
#define WEB_URL "http://test.thekoziolfoundation.com/"
static const char *TAG = "example";
static const char *REQUEST = "POST " WEB_URL " HTTP/1.0\r\n"
"Host: " WEB_SERVER "\r\n"
"Content-Type: multipart/form-data; boundary=esp\r\n"
"User-Agent: ESP8266/TaterLi SmartHome\r\n"
"Content-Length: 136\r\n\r\n"
"--esp\r\nContent-Disposition: form-data; name=\"temperature\"\r\n\r\n"
"%d"
"\r\n--esp\r\n"
"Content-Disposition: form-data; name=\"humidity\"\r\n\r\n"
"%d"
"\r\n--esp--\r\n";
extern const uint8_t server_root_cert_pem_start[] asm("_binary_server_root_cert_pem_start");
extern const uint8_t server_root_cert_pem_end[] asm("_binary_server_root_cert_pem_end");
static void https_get_task(void *pvParameters)
{
char buf[512];
int ret, flags, len;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl;
mbedtls_x509_crt cacert;
mbedtls_ssl_config conf;
mbedtls_net_context server_fd;
int16_t temperature = 0;
int16_t humidity = 0;
mbedtls_ssl_init(&ssl);
mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_ssl_config_init(&conf);
mbedtls_entropy_init(&entropy);
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
NULL, 0)) != 0)
{
abort();
}
ret = mbedtls_x509_crt_parse(&cacert, server_root_cert_pem_start,
server_root_cert_pem_end - server_root_cert_pem_start);
if (ret < 0)
{
abort();
}
/* Hostname set here should match CN in server certificate */
if ((ret = mbedtls_ssl_set_hostname(&ssl, WEB_SERVER)) != 0)
{
abort();
}
if ((ret = mbedtls_ssl_config_defaults(&conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
{
goto exit;
}
mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_OPTIONAL);
mbedtls_ssl_conf_ca_chain(&conf, &cacert, NULL);
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
#ifdef CONFIG_MBEDTLS_DEBUG
mbedtls_esp_enable_debug_log(&conf, 4);
#endif
if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0)
{
goto exit;
}
while (1)
{
if (dht_read_data(DHT_TYPE_DHT11, 2, &humidity, &temperature) == ESP_OK)
{
printf("Humidity: %d%% Temp: %dC\n", humidity / 10, temperature / 10);
sprintf(buf, REQUEST, temperature / 10, humidity / 10);
mbedtls_net_init(&server_fd);
if ((ret = mbedtls_net_connect(&server_fd, WEB_SERVER,
WEB_PORT, MBEDTLS_NET_PROTO_TCP)) != 0)
{
goto exit;
}
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
while ((ret = mbedtls_ssl_handshake(&ssl)) != 0)
{
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
{
goto exit;
}
}
size_t written_bytes = 0;
do
{
ret = mbedtls_ssl_write(&ssl,
(const unsigned char *)buf + written_bytes,
strlen(buf) - written_bytes);
if (ret >= 0)
{
written_bytes += ret;
}
else if (ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret != MBEDTLS_ERR_SSL_WANT_READ)
{
goto exit;
}
} while (written_bytes < strlen(buf));
do
{
len = sizeof(buf) - 1;
bzero(buf, sizeof(buf));
ret = mbedtls_ssl_read(&ssl, (unsigned char *)buf, len);
if (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE)
continue;
else
break;
} while (1);
mbedtls_ssl_close_notify(&ssl);
exit:
mbedtls_ssl_session_reset(&ssl);
mbedtls_net_free(&server_fd);
}
}
}
void app_main(void)
{
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(example_connect());
xTaskCreate(&https_get_task, "https_get_task", 8192, NULL, 5, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment