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(blink)
+12
View File
@@ -0,0 +1,12 @@
# Blink
## The quintessential project
The 'hello world' of embedded projects is to get an LED to blink
## Links for more info:
github for esp-idf: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/index.html
docs: https://docs.espressif.com/
in depth video on esp-idf: https://www.youtube.com/watch?v=J8zc8mMNKtc
+2
View File
@@ -0,0 +1,2 @@
idf_component_register(SRCS "blink.c"
INCLUDE_DIRS ".")
+25
View File
@@ -0,0 +1,25 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"
#define LED 2
void app_main(void) {
const char *tag = "blink";
ESP_LOGI(tag, "Starting blink!\n");
const uint32_t hi = 1, lo = 0;
const TickType_t duration = 500 / portTICK_PERIOD_MS;
gpio_reset_pin(LED);
gpio_set_direction(LED, GPIO_MODE_OUTPUT);
while (1) {
gpio_set_level(LED, hi);
vTaskDelay(duration);
gpio_set_level(LED, lo);
vTaskDelay(duration);
}
}
+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(lcd-16x2)
+2
View File
@@ -0,0 +1,2 @@
idf_component_register(SRCS "lcd-16x2.c"
INCLUDE_DIRS ".")
+17
View File
@@ -0,0 +1,17 @@
## 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
panigrah/esp32-idf-hd44780: ^0.0.3
+44
View File
@@ -0,0 +1,44 @@
#include "HD44780.h"
#include <driver/i2c.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdio.h>
#define LCD_ADDR 0x27
#define SDA_PIN 21
#define SCL_PIN 22
#define LCD_COLS 16
#define LCD_ROWS 2
void app_main(void) {
char *tag = "LCD_16x2";
ESP_LOGI(tag, "starting app_main");
LCD_init(LCD_ADDR, SDA_PIN, SCL_PIN, LCD_COLS, LCD_ROWS);
// LCD_clearScreen();
// LCD_writeStr("Hello There");
// LCD_setCursor(0, 1);
// LCD_writeStr(" Second Row! ");
char num[8];
while (true) {
LCD_clearScreen();
LCD_writeStr("Counting Down");
for (int i = 10; i >= 0; i--) {
LCD_setCursor(8, 1);
sprintf(num, "%d", i);
LCD_writeStr(num);
vTaskDelay(1000 / portTICK_PERIOD_MS);
LCD_setCursor(0, 1);
LCD_writeStr(" ");
}
LCD_clearScreen();
LCD_writeStr("Liftoff!");
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
+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"
INCLUDE_DIRS ".")
+18
View File
@@ -0,0 +1,18 @@
## 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
+57
View File
@@ -0,0 +1,57 @@
#include "HD44780.h"
#include "dht.h"
#include <esp_err.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <stdint.h>
#include <stdio.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) {
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);
}
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; }
+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
+68
View File
@@ -0,0 +1,68 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define PORT 9000
#define BUFFER_SIZE 1024
int main(int argc, char **argv) {
int serverSocket;
struct sockaddr_in serverAddr;
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof serverAddr) <
0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(serverSocket, 5) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1) {
struct sockaddr_in clientAddr;
socklen_t clientAddr_len = sizeof clientAddr;
int *clientSocket = malloc(sizeof(int));
if ((*clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr,
&clientAddr_len)) < 0) {
perror("accept failed");
continue;
}
printf("Connected\n");
char buf[BUFFER_SIZE];
int bytes = recv(*clientSocket, buf, sizeof buf, 0);
if (bytes < 0) {
perror("error recv data from client");
continue;
}
// strip the header
char *data = strstr(buf, "\r\n\r\n");
if (data) {
printf("parsed data %s\n", data);
}
close(*clientSocket);
printf("disconnected client.\n");
}
close(serverSocket);
return 0;
}
+6
View File
@@ -0,0 +1,6 @@
create table Stats (
Id integer primary key not null,
Fahrenheit real not null,
Celsius real not null,
Humidity real not null
);
+161
View File
@@ -0,0 +1,161 @@
#include <arpa/inet.h>
#include <cjson/cJSON.h>
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define PORT 9000
#define BUFFER_SIZE 1024
typedef struct ESPData {
double celsius;
double fahrenheit;
double humidity;
} ESPData;
int parseJSON(char buffer[BUFFER_SIZE], ESPData *espData);
sqlite3 *initDB();
int storeData(sqlite3 *db, ESPData espData);
int main(int argc, char **argv) {
sqlite3 *db = initDB();
if (!db) {
exit(EXIT_FAILURE);
}
int serverSocket;
struct sockaddr_in serverAddr;
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof serverAddr) <
0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(serverSocket, 5) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1) {
struct sockaddr_in clientAddr;
socklen_t clientAddr_len = sizeof clientAddr;
int *clientSocket = malloc(sizeof(int));
if ((*clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr,
&clientAddr_len)) < 0) {
perror("accept failed");
continue;
}
printf("Connected\n");
// Receive from Client
char buf[BUFFER_SIZE];
int bytes = recv(*clientSocket, buf, sizeof buf, 0);
if (bytes <= 0) {
perror("failed to recv from client");
continue;
}
printf("raw data: %s\n", buf);
// Parse JSON
ESPData espData;
if (parseJSON(buf, &espData) == 0) {
printf("Fahrenheit: %.2f, Celsius: %.2f, Humidity: %.2f\n",
espData.fahrenheit, espData.celsius, espData.humidity);
storeData(db, espData);
}
close(*clientSocket);
printf("disconnected client.\n");
}
close(serverSocket);
return 0;
}
int parseJSON(char buffer[BUFFER_SIZE], ESPData *espData) {
// char *data = strstr(buffer, "\r\n\r\n");
// if (data) {
// data += 4;
// } else {
// return -1;
// }
const cJSON *fahr;
const cJSON *cel;
const cJSON *hum;
cJSON *jsonData = cJSON_Parse(buffer);
fahr = cJSON_GetObjectItem(jsonData, "fahrenheit");
if (!fahr || !cJSON_IsNumber(fahr)) {
printf("Error: failed to parse fahrenheit\n");
cJSON_Delete(jsonData);
return -1;
}
cel = cJSON_GetObjectItem(jsonData, "celsius");
if (!cel || !cJSON_IsNumber(cel)) {
printf("Error: failed to parse celsius\n");
cJSON_Delete(jsonData);
return -1;
}
hum = cJSON_GetObjectItem(jsonData, "humidity");
if (!hum || !cJSON_IsNumber(hum)) {
printf("Error: failed to parse humidity\n");
cJSON_Delete(jsonData);
return -1;
}
espData->fahrenheit = fahr->valuedouble;
espData->celsius = cel->valuedouble;
espData->humidity = hum->valuedouble;
cJSON_Delete(jsonData);
return 0;
}
sqlite3 *initDB() {
sqlite3 *db = NULL;
int res = sqlite3_open("stats.db", &db);
if (res != SQLITE_OK) {
fprintf(stderr, "Failed to open the db: %s", sqlite3_errmsg(db));
}
return db;
}
int storeData(sqlite3 *db, ESPData espData) {
char sql[256];
sprintf(sql,
"insert into Stats(Id, Fahrenheit, Celsius, Humidity) values(NULL, "
"'%f', '%f', '%f');",
espData.fahrenheit, espData.celsius, espData.humidity);
char *errMsg;
int res = sqlite3_exec(db, sql, 0, 0, &errMsg);
if (res != SQLITE_OK) {
fprintf(stderr, "Could not insert into DB: %s", errMsg);
sqlite3_free(errMsg);
return -1;
}
return 0;
}
+94
View File
@@ -0,0 +1,94 @@
#include <driver/i2c_master.h>
#include <esp_err.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <portmacro.h>
#include <ssd1306.h>
#include <stdio.h>
#include <ultrasonic.h>
// defines for sonar sensor
#define TRIGGER 19
#define ECHO 18
// defines for i2c display
#define I2C_HOST 0
#define SDA 22
#define SCL 21
#define RESET_PIN -1
#define I2C_HW_ADDR 0x3C
#define LCD_H 128
#define LCD_W 64
void displayData(ssd1306_config_t *cfg, ssd1306_handle_t *handle, float data) {
ESP_ERROR_CHECK(ssd1306_clear(*handle));
ssd1306_draw_rect(*handle, 0, 0, 128, 4, true);
char buf[64];
sprintf(buf, "Distance: %.2f", data);
ESP_ERROR_CHECK(ssd1306_draw_text(*handle, 0, 20, buf, true));
ESP_ERROR_CHECK(ssd1306_draw_rect(*handle, 0, 40, 128, 4, true));
ESP_ERROR_CHECK(ssd1306_display(*handle));
}
void app_main(void) {
i2c_master_bus_config_t bus_cfg = {
.i2c_port = I2C_HOST,
.sda_io_num = SDA,
.scl_io_num = SCL,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 0,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t bus = NULL;
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_cfg, &bus));
ssd1306_config_t cfg = {
.width = LCD_H,
.height = LCD_W,
.fb = NULL, // let driver allocate internally
.fb_len = 0,
.iface.i2c =
{
.port = I2C_HOST,
.addr = I2C_HW_ADDR, // typical SSD1306 I2C address
.rst_gpio = RESET_PIN, // no reset pin
},
};
ssd1306_handle_t handle = NULL;
ESP_ERROR_CHECK(ssd1306_new_i2c(&cfg, &handle));
const ultrasonic_sensor_t sensor = {.trigger_pin = TRIGGER, .echo_pin = ECHO};
const TickType_t duration = 500 / portTICK_PERIOD_MS;
ultrasonic_init(&sensor);
while (true) {
float distance = 0;
esp_err_t res = ultrasonic_measure(&sensor, 500, &distance);
if (res != ESP_OK) {
printf("Error %d: ", res);
switch (res) {
case ESP_ERR_ULTRASONIC_PING:
printf("Cannot ping (device is in invalid state)\n");
break;
case ESP_ERR_ULTRASONIC_PING_TIMEOUT:
printf("Ping timeout (no device found)\n");
break;
case ESP_ERR_ULTRASONIC_ECHO_TIMEOUT:
printf("Echo timeout (i.e. distance too big)\n");
break;
default:
printf("%s\n", esp_err_to_name(res));
}
} else {
displayData(&cfg, &handle, (distance * 100));
printf("Distance: %.2f cm\n", distance * 100);
}
vTaskDelay(duration);
}
}
+5
View File
@@ -0,0 +1,5 @@
# Embedded Tutorials
Tutorials covering the ESP32, STM32, Arduino, Raspberry PI, etc