source code migrating from gitlab

This commit is contained in:
2026-05-15 15:51:41 -04:00
commit 6cf9a21574
28 changed files with 2960 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(temp-humidity)
+2
View File
@@ -0,0 +1,2 @@
idf_component_register(SRCS "temp-humidity.c" "wifi/wifi_con.c" "json/json_convert.c" "client/client.c"
INCLUDE_DIRS ".")
+57
View File
@@ -0,0 +1,57 @@
#include "client/client.h"
#include <lwip/sockets.h>
#define PORT 9000
const char *address = "192.168.40.88";
int sendJSON(char *json, int len) {
int fd = 0, res = 0;
struct sockaddr_in server;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
res = inet_pton(AF_INET, address, &server.sin_addr);
if (res < 0) {
close(fd);
return -2;
}
res = connect(fd, (struct sockaddr *)&server, sizeof(server));
if (res < 0) {
close(fd);
return -3;
}
res = sendAll(fd, json, len);
if (res < 0) {
close(fd);
return -4;
}
close(fd);
return 0;
}
int sendAll(int sock, char *buf, int len) {
int total = 0, bytesleft = len, n = 0;
while (total < len) {
n = send(sock, buf + total, bytesleft, 0);
if (n < 0) {
break;
}
total += n;
bytesleft -= n;
}
return n == -1 ? -1 : 0;
}
+7
View File
@@ -0,0 +1,7 @@
#ifndef CLIENT_H
#define CLIENT_H
int sendJSON(char *json, int len);
int sendAll(int sock, char *buf, int len);
#endif // !CLIENT_H
@@ -0,0 +1,19 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf:
version: '>=4.1.0'
# # Put list of dependencies here
# # For components maintained by Espressif:
# component: "~1.0.0"
# # For 3rd party components:
# username/component: ">=1.0.0,<2.0.0"
# username2/component2:
# version: "~1.0.0"
# # For transient dependencies `public` flag can be set.
# # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default.
# public: true
zorxx/dht: ^1.0.1
panigrah/esp32-idf-hd44780: ^0.0.3
espressif/cjson: ^1.7.19
@@ -0,0 +1,45 @@
#include "json/json_convert.h"
#include <cJSON.h>
#include <esp_log.h>
char *createJSON(float fahr, float cel, float hum) {
char *json = NULL;
cJSON *fahrenheit = NULL;
cJSON *celsius = NULL;
cJSON *humidity = NULL;
cJSON *data = cJSON_CreateObject();
if (!data) {
return NULL;
}
fahrenheit = cJSON_CreateNumber(fahr);
if (!fahrenheit) {
cJSON_Delete(data);
return NULL;
}
cJSON_AddItemToObject(data, "fahrenheit", fahrenheit);
celsius = cJSON_CreateNumber(cel);
if (!celsius) {
ESP_LOGE("JSON", "Failed to create celsius JSON");
cJSON_Delete(data);
return NULL;
}
cJSON_AddItemToObject(data, "celsius", celsius);
humidity = cJSON_CreateNumber(hum);
if (!humidity) {
cJSON_Delete(data);
return NULL;
}
cJSON_AddItemToObject(data, "humidity", humidity);
json = cJSON_Print(data);
return json;
}
@@ -0,0 +1,6 @@
#ifndef JSON_CONVERT_H
#define JSON_CONVERT_H
char *createJSON(float fahr, float cel, float hum);
#endif // !JSON_CONVERT_H
+92
View File
@@ -0,0 +1,92 @@
#include "HD44780.h"
#include "dht.h"
#include "freertos/idf_additions.h"
#include "nvs.h"
#include <esp_err.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "client/client.h"
#include "wifi/wifi_con.h"
#include "json/json_convert.h"
#define DHT_PIN 25
#define LCD_ADDR 0x27
#define SDA_PIN 21
#define SCL_PIN 22
#define LCD_ROWS 16
#define LCD_COLS 2
float convertValue(int16_t val);
float converTemp(int16_t celsius);
void app_main(void) {
esp_err_t status = nvs_flash_init();
if (status == ESP_ERR_NVS_NO_FREE_PAGES ||
status == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
status = nvs_flash_init();
}
ESP_ERROR_CHECK(status);
status = initWifi();
if (status == WIFI_FAILURE) {
ESP_LOGE(WIFI_TAG, "Failed to connect to Wifi. Exiting");
return;
} else {
ESP_LOGI(WIFI_TAG, "Connected to Wifi Successfully");
}
const char *tag = "TEMP_HUMID";
int16_t temp, humid;
char tempStr[32], humidStr[32];
ESP_LOGI(tag, "Starting our temp/humidity readings");
LCD_init(LCD_ADDR, SDA_PIN, SCL_PIN, LCD_COLS, LCD_ROWS);
while (1) {
LCD_clearScreen();
esp_err_t err = dht_read_data(DHT_TYPE_AM2301, DHT_PIN, &humid, &temp);
if (err != ESP_OK) {
ESP_LOGE(tag, "Error: could not read DHT22 Sensor");
} else {
// printf("Temp: %d.%d C\n", temp / 10, temp % 10);
// printf("Humidity: %d.%d%%\n", humid / 10, humid % 10);
//
float celsius = convertValue(temp);
float humidity = convertValue(humid);
float fahrenheit = converTemp(celsius);
sprintf(tempStr, "T: %.1fC / %.1fF", celsius, fahrenheit);
sprintf(humidStr, "Humidity: %.1f%%", humidity);
LCD_writeStr(tempStr);
LCD_setCursor(0, 1);
LCD_writeStr(humidStr);
// store in JSON object
char *json = createJSON(fahrenheit, celsius, humidity);
if (!json) {
ESP_LOGE("JSON", "Error creating JSON string");
} else {
ESP_LOGI("JSON", "JSON String %s", json);
int len = strlen(json);
int res = sendJSON(json, len);
if (res < 0) {
ESP_LOGE("CLIENT", "Failed to send JSON: %d", res);
}
}
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
float convertValue(int16_t val) { return val / 10.0f; }
float converTemp(int16_t celsius) { return celsius * (9.0f / 5.0f) + 32.0f; }
+79
View File
@@ -0,0 +1,79 @@
#include "wifi_con.h"
#include "creds.h"
#include "esp_netif_types.h"
#include "esp_wifi_types_generic.h"
static void wifiEventHandler(void *arg, esp_event_base_t eventBase,
int32_t eventId, void *eventData) {
static int retryNum = 0;
if (eventBase == WIFI_EVENT && eventId == WIFI_EVENT_STA_START) {
ESP_LOGI(WIFI_TAG, "Connecting to WiFi...");
esp_wifi_connect();
} else if (eventBase == WIFI_EVENT &&
eventId == WIFI_EVENT_STA_DISCONNECTED) {
if (retryNum < MAX_FAILURES) {
ESP_LOGI(WIFI_TAG, "retrying to connect.. %d", retryNum);
esp_wifi_connect();
retryNum++;
} else {
xEventGroupSetBits(wifiEventGroup, WIFI_FAILURE);
}
}
}
static void ipEventHandler(void *arg, esp_event_base_t eventBase,
int32_t eventId, void *eventData) {
if (eventBase == IP_EVENT && eventId == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)eventData;
ESP_LOGI(WIFI_TAG, "IP Address Assigned: " IPSTR,
IP2STR(&event->ip_info.ip));
xEventGroupSetBits(wifiEventGroup, WIFI_SUCCESS);
}
}
esp_err_t initWifi() {
wifiEventGroup = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&config));
esp_event_handler_instance_t instanceWIFI;
esp_event_handler_instance_t instanceGotIP;
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT, ESP_EVENT_ANY_ID, &wifiEventHandler, NULL, &instanceWIFI));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &ipEventHandler, NULL, &instanceGotIP));
wifi_config_t wifiConfig = {.sta = {
.ssid = SSID,
.password = WIFI_PASS,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
}};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifiConfig));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(WIFI_TAG, "WiFi init station complete");
EventBits_t bits =
xEventGroupWaitBits(wifiEventGroup, WIFI_SUCCESS | WIFI_FAILURE, pdFALSE,
pdFALSE, portMAX_DELAY);
if (bits & WIFI_SUCCESS) {
return WIFI_SUCCESS;
} else if (bits & WIFI_FAILURE) {
return WIFI_FAILURE;
} else {
return WIFI_FAILURE;
}
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef WIFI_CON_H
#define WIFI_CON_H
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <esp_wifi.h>
#include <freertos/event_groups.h>
#include <nvs_flash.h>
#define MAX_FAILURES 10
#define WIFI_TAG "WIFI"
static EventGroupHandle_t wifiEventGroup;
#define WIFI_SUCCESS BIT0
#define WIFI_FAILURE BIT1
// NEW FUNC DEFS
esp_err_t initWifi();
#endif // !WIFI_CON_H
File diff suppressed because it is too large Load Diff