From d288176fba341f8ac67ee6b4d866a65bacb4e0ed Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Sat, 12 Aug 2023 21:38:42 +0000 Subject: [PATCH] Apparently I was doing stuff --- rp2040/sht30/sht30.c | 8 +-- rp2040/sht30wifi/.gitignore | 1 + rp2040/sht30wifi/CMakeLists.txt | 15 ++++ rp2040/sht30wifi/lwipopts.h | 36 ++++++++++ rp2040/sht30wifi/main.c | 111 ++++++++++++++++++++++++++++++ rp2040/sht30wifi/secret.h.example | 6 ++ 6 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 rp2040/sht30wifi/.gitignore create mode 100644 rp2040/sht30wifi/CMakeLists.txt create mode 100644 rp2040/sht30wifi/lwipopts.h create mode 100644 rp2040/sht30wifi/main.c create mode 100644 rp2040/sht30wifi/secret.h.example diff --git a/rp2040/sht30/sht30.c b/rp2040/sht30/sht30.c index 101f8fe..d6bf84b 100644 --- a/rp2040/sht30/sht30.c +++ b/rp2040/sht30/sht30.c @@ -37,7 +37,7 @@ static uint8_t crc8(uint8_t msb, uint8_t lsb) { return crc8_table[lsb ^ crc8_table[msb ^ 0xff]]; } -static void sht30_reset_all() { +void sht30_reset_all() { // Generall call recet uint8_t cmd[] = {0x06}; i2c_write_blocking(i2c, 0x00, cmd, sizeof(cmd), false); @@ -45,7 +45,7 @@ static void sht30_reset_all() { sleep_ms(2); } -static uint16_t sht30_get_firmware_version() { +uint16_t sht30_get_firmware_version() { uint8_t cmd[] = {0xd1, 0x00}; i2c_write_blocking(i2c, addr, cmd, sizeof(cmd), true); uint8_t resp[3]; @@ -55,13 +55,13 @@ static uint16_t sht30_get_firmware_version() { return (uint16_t)resp[0] << 8 | resp[1]; } -static void sht30_start_capture() { +void sht30_start_capture() { // High repeatability, 1 sample per second uint8_t cmd[] = {0x21, 0x30}; i2c_write_blocking(i2c, addr, cmd, sizeof(cmd), false); }; -static struct sht30_result sht30_fetch_data() { +struct sht30_result sht30_fetch_data() { uint8_t cmd[] = {0xe0, 0x00}; i2c_write_blocking(i2c, addr, cmd, sizeof(cmd), true); uint8_t resp[6]; diff --git a/rp2040/sht30wifi/.gitignore b/rp2040/sht30wifi/.gitignore new file mode 100644 index 0000000..a7fbec4 --- /dev/null +++ b/rp2040/sht30wifi/.gitignore @@ -0,0 +1 @@ +secret.h diff --git a/rp2040/sht30wifi/CMakeLists.txt b/rp2040/sht30wifi/CMakeLists.txt new file mode 100644 index 0000000..982bae6 --- /dev/null +++ b/rp2040/sht30wifi/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.12) + +include($ENV{PICO_SDK_PATH}/pico_sdk_init.cmake) +set(CMAKE_C_STANDARD 11) +set(PICO_BOARD "pico_w") + +project(sht30wifi) +pico_sdk_init() + +add_executable(sht30wifi main.c) +target_include_directories(sht30wifi PRIVATE ${CMAKE_CURRENT_LIST_DIR}) +target_link_libraries(sht30wifi pico_stdlib hardware_i2c pico_cyw43_arch_lwip_threadsafe_background) + +pico_enable_stdio_usb(sht30wifi 1) +pico_add_extra_outputs(sht30wifi) diff --git a/rp2040/sht30wifi/lwipopts.h b/rp2040/sht30wifi/lwipopts.h new file mode 100644 index 0000000..f4ffe37 --- /dev/null +++ b/rp2040/sht30wifi/lwipopts.h @@ -0,0 +1,36 @@ +#pragma once + +#define NO_SYS 1 +#define LWIP_SOCKET 0 +#define MEM_LIBC_MALLOC 0 +#define MEM_ALIGNMENT 4 +#define MEM_SIZE 4000 +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 diff --git a/rp2040/sht30wifi/main.c b/rp2040/sht30wifi/main.c new file mode 100644 index 0000000..90e954d --- /dev/null +++ b/rp2040/sht30wifi/main.c @@ -0,0 +1,111 @@ +#include +#include + +#include "hardware/i2c.h" +#include "pico/stdlib.h" +#include "pico/cyw43_arch.h" +#include "lwip/pbuf.h" +#include "lwip/tcp.h" + +struct sht30_result { + float temp_c; + float relative_humidity; +}; + +static struct i2c_inst *i2c = i2c1; +const static uint32_t sda_pin = 2; +const static uint32_t scl_pin = 3; +const static uint8_t addr = 0x44; +const static uint8_t crc8_table[] = { + 0, 49, 98, 83, 196, 245, 166, 151, 185, 136, 219, 234, 125, 76, 31, + 46, 67, 114, 33, 16, 135, 182, 229, 212, 250, 203, 152, 169, 62, 15, + 92, 109, 134, 183, 228, 213, 66, 115, 32, 17, 63, 14, 93, 108, 251, + 202, 153, 168, 197, 244, 167, 150, 1, 48, 99, 82, 124, 77, 30, 47, + 184, 137, 218, 235, 61, 12, 95, 110, 249, 200, 155, 170, 132, 181, 230, + 215, 64, 113, 34, 19, 126, 79, 28, 45, 186, 139, 216, 233, 199, 246, + 165, 148, 3, 50, 97, 80, 187, 138, 217, 232, 127, 78, 29, 44, 2, + 51, 96, 81, 198, 247, 164, 149, 248, 201, 154, 171, 60, 13, 94, 111, + 65, 112, 35, 18, 133, 180, 231, 214, 122, 75, 24, 41, 190, 143, 220, + 237, 195, 242, 161, 144, 7, 54, 101, 84, 57, 8, 91, 106, 253, 204, + 159, 174, 128, 177, 226, 211, 68, 117, 38, 23, 252, 205, 158, 175, 56, + 9, 90, 107, 69, 116, 39, 22, 129, 176, 227, 210, 191, 142, 221, 236, + 123, 74, 25, 40, 6, 55, 100, 85, 194, 243, 160, 145, 71, 118, 37, + 20, 131, 178, 225, 208, 254, 207, 156, 173, 58, 11, 88, 105, 4, 53, + 102, 87, 192, 241, 162, 147, 189, 140, 223, 238, 121, 72, 27, 42, 193, + 240, 163, 146, 5, 52, 103, 86, 120, 73, 26, 43, 188, 141, 222, 239, + 130, 179, 224, 209, 70, 119, 36, 21, 59, 10, 89, 104, 255, 206, 157, + 172}; + +static uint8_t crc8(uint8_t msb, uint8_t lsb) { + return crc8_table[lsb ^ crc8_table[msb ^ 0xff]]; +} + +static void sht30_reset_all() { + // Generall call recet + uint8_t cmd[] = {0x06}; + i2c_write_blocking(i2c, 0x00, cmd, sizeof(cmd), false); + // Datasheet says max hard reset time is 1ms, soft reset is 1.5ms + sleep_ms(2); +} + +static uint16_t sht30_get_firmware_version() { + uint8_t cmd[] = {0xd1, 0x00}; + i2c_write_blocking(i2c, addr, cmd, sizeof(cmd), true); + uint8_t resp[3]; + i2c_read_blocking(i2c, addr, resp, sizeof(resp), false); + if (crc8(resp[0], resp[1]) != resp[2]) + return 0xffff; + return (uint16_t)resp[0] << 8 | resp[1]; +} + +static void sht30_start_capture() { + // High repeatability, 1 sample per second + uint8_t cmd[] = {0x21, 0x30}; + i2c_write_blocking(i2c, addr, cmd, sizeof(cmd), false); +}; + +static struct sht30_result sht30_fetch_data() { + uint8_t cmd[] = {0xe0, 0x00}; + i2c_write_blocking(i2c, addr, cmd, sizeof(cmd), true); + uint8_t resp[6]; + i2c_read_blocking(i2c, addr, resp, sizeof(resp), false); + + float temp_c, relative_humidity; + if (crc8(resp[0], resp[1]) == resp[2]) + temp_c = -45 + 175 * (float)(resp[0] << 8 | resp[1]) / 0xffff; + else + temp_c = NAN; + + if (crc8(resp[3], resp[4]) == resp[5]) + relative_humidity = 100 * (float)(resp[3] << 8 | resp[4]) / 0xffff; + else + relative_humidity = NAN; + + struct sht30_result result = {.temp_c = temp_c, + .relative_humidity = relative_humidity}; + return result; +} + +int main() { + stdio_init_all(); + + i2c_init(i2c, 100 * 1000); + gpio_set_function(sda_pin, GPIO_FUNC_I2C); + gpio_set_function(scl_pin, GPIO_FUNC_I2C); + gpio_pull_up(sda_pin); + gpio_pull_up(scl_pin); + + sleep_ms(2000); + + sht30_reset_all(); + + sht30_start_capture(); + + while (true) { + sleep_ms(1000); + struct sht30_result result = sht30_fetch_data(); + printf("{\"temperature\": %f, \"humidity\": %f}\n", result.temp_c, + result.relative_humidity); + } + return 0; +} diff --git a/rp2040/sht30wifi/secret.h.example b/rp2040/sht30wifi/secret.h.example new file mode 100644 index 0000000..06b880e --- /dev/null +++ b/rp2040/sht30wifi/secret.h.example @@ -0,0 +1,6 @@ +#pragma once + +#define WIFI_SSID "ssid" +#define WIFI_PASSWORD "password" +#define INFLUXDB_HOST "2001:db8::acab" +#define INFLUXDB_TOKEN "token"