diff --git a/CHANGELOG.md b/CHANGELOG.md index 31d4b2aee..bcc7b6ca8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,15 +1,5 @@ ### New changes -* Plugins: Added GPS [(By ezod)](https://github.com/ezod/flipperzero-gps) works with module `NMEA 0183` via UART -* Plugins: Added i2c Tools [(By NaejEL)](https://github.com/NaejEL/flipperzero-i2ctools) -* Infrared: Updated universal remote assets (by @Amec0e) -* OFW -> WS: add protocol Acurite-606TX - And thanks to Dimme#1601 for recordings! -* OFW -> WS: history, added display of the channel (if any) in the general list -* OFW -> WS: added display of the button state if it is on the transmitter, and displaying the data that is in the signal -* OFW -> WS: fix batt info -* OFW -> WS: add protocol LaCrosse_TX141THBv2 -* OFW: FuriHal: add FuriHalCortexTimer, use it for i2c bus timeouts -* OFW: CMSIS DAP/DAP Link Debugger -* OFW: Fix FuriString oplist (init move) +* Plugins: Added Temperature Sensor Plugin - HTU21D / SI7021 [(By Mywk)](https://github.com/Mywk/FlipperTemperatureSensor) - [How to Connect](https://github.com/DarkFlippers/unleashed-firmware/blob/dev/applications/plugins/temperature_sensor/Readme.md) **Note: Version naming changed to be more clear what version is newer, now we using -> unlshd-123 where 123 is build number** diff --git a/ReadMe.md b/ReadMe.md index 8ff319f67..916357ce6 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -90,6 +90,7 @@ Also check changelog in releases for latest updates! - **TOTP (Authenticator)** [(by akopachov)](https://github.com/akopachov/flipper-zero_authenticator) - GPS [(By ezod)](https://github.com/ezod/flipperzero-gps) works with module `NMEA 0183` via UART (13TX, 14RX, GND pins on Flipper) - i2c Tools [(By NaejEL)](https://github.com/NaejEL/flipperzero-i2ctools) - C0 -> SCL / C1 -> SDA / GND -> GND | 3v3 logic levels only! +- Temperature Sensor Plugin - HTU21D / SI7021 [(By Mywk)](https://github.com/Mywk/FlipperTemperatureSensor) - [How to Connect](https://github.com/DarkFlippers/unleashed-firmware/blob/dev/applications/plugins/temperature_sensor/Readme.md) Games: - DOOM (fixed) [(By p4nic4ttack)](https://github.com/p4nic4ttack/doom-flipper-zero/) @@ -137,6 +138,12 @@ Games: ### **Plugins that works with external hardware** +## [- How to use: Temperature Sensor Plugin - HTU21D / SI7021](https://github.com/DarkFlippers/unleashed-firmware/blob/dev/applications/plugins/temperature_sensor/Readme.md) + +## [- How to use: [NMEA] GPS](https://github.com/DarkFlippers/unleashed-firmware/blob/dev/applications/plugins/gps_nmea_uart/README.md) + +## [- How to use: i2c Tools](https://github.com/DarkFlippers/unleashed-firmware/blob/dev/applications/plugins/flipper_i2ctools/README.md) + ## [- How to use: [NRF24] plugins](https://github.com/DarkFlippers/unleashed-firmware/blob/dev/documentation/NRF24.md) ## [- How to use: [WiFi] Scanner](https://github.com/SequoiaSan/FlipperZero-WiFi-Scanner_Module#readme) diff --git a/applications/plugins/temperature_sensor/Readme.md b/applications/plugins/temperature_sensor/Readme.md new file mode 100644 index 000000000..0311294ea --- /dev/null +++ b/applications/plugins/temperature_sensor/Readme.md @@ -0,0 +1,17 @@ +# Flipper Temperature Sensor - HTU21D / SI7021 + +[Original link](https://github.com/Mywk/FlipperTemperatureSensor) + +## What is this? + +A small app for the [Flipper Zero](https://flipperzero.one) that reads the [I2C](https://en.wikipedia.org/wiki/I%C2%B2C) signal from a HTU21D or Si7021 sensor and displays the current temperature and humidity. + +I'm using a [Sparkfun HTU21D sensor](https://learn.sparkfun.com/tutorials/htu21d-humidity-sensor-hookup-guide), also tested with a clone and with the Si7021 variant. + +![Flipper Temperature Sensor](docs/Flipper.png) + +![App](docs/App.png) + +## How to Connect the sensor +![Connection](docs/Connection.png) + diff --git a/applications/plugins/temperature_sensor/application.fam b/applications/plugins/temperature_sensor/application.fam new file mode 100644 index 000000000..4ed8dd2d2 --- /dev/null +++ b/applications/plugins/temperature_sensor/application.fam @@ -0,0 +1,14 @@ +App( + appid="temperature_sensor", + name="[GPIO] Temperature Sensor", + apptype=FlipperAppType.EXTERNAL, + entry_point="temperature_sensor_app", + cdefines=["APP_TEMPERATURE_SENSOR"], + requires=[ + "gui", + ], + stack_size=2 * 1024, + order=90, + fap_icon="temperature_sensor.png", + fap_category="GPIO", +) diff --git a/applications/plugins/temperature_sensor/docs/App.png b/applications/plugins/temperature_sensor/docs/App.png new file mode 100644 index 000000000..a0373bdbd Binary files /dev/null and b/applications/plugins/temperature_sensor/docs/App.png differ diff --git a/applications/plugins/temperature_sensor/docs/Connection.png b/applications/plugins/temperature_sensor/docs/Connection.png new file mode 100644 index 000000000..b38f5c250 Binary files /dev/null and b/applications/plugins/temperature_sensor/docs/Connection.png differ diff --git a/applications/plugins/temperature_sensor/docs/Flipper.png b/applications/plugins/temperature_sensor/docs/Flipper.png new file mode 100644 index 000000000..c85319593 Binary files /dev/null and b/applications/plugins/temperature_sensor/docs/Flipper.png differ diff --git a/applications/plugins/temperature_sensor/temperature_sensor.c b/applications/plugins/temperature_sensor/temperature_sensor.c new file mode 100644 index 000000000..721e2ad7c --- /dev/null +++ b/applications/plugins/temperature_sensor/temperature_sensor.c @@ -0,0 +1,299 @@ +/* Flipper App to read the values from a HTU21D Sensor */ +/* Created by Mywk - https://github.com/Mywk - https://mywk.net */ +#include +#include +#include + +#include +#include + +#include + +#include + +#define TS_DEFAULT_VALUE 0xFFFF + +#define HTU21D_ADDRESS (0x40 << 1) + +#define HTU21D_CMD_TEMPERATURE 0xE3 +#define HTU21D_CMD_HUMIDITY 0xE5 + +#define DATA_BUFFER_SIZE 8 + +// External I2C BUS +#define I2C_BUS &furi_hal_i2c_handle_external + +typedef enum { + TSSInitializing, + TSSNoSensor, + TSSPendingUpdate, +} TSStatus; + +typedef enum { + TSEventTypeTick, + TSEventTypeInput, +} TSEventType; + +typedef struct { + TSEventType type; + InputEvent input; +} TSEvent; + +extern const NotificationSequence sequence_blink_red_100; +extern const NotificationSequence sequence_blink_blue_100; + +static TSStatus temperature_sensor_current_status = TSSInitializing; + +// Temperature and Humidity data buffers, ready to print +char ts_data_buffer_temperature_c[DATA_BUFFER_SIZE]; +char ts_data_buffer_temperature_f[DATA_BUFFER_SIZE]; +char ts_data_buffer_humidity[DATA_BUFFER_SIZE]; + +// +// Executes an I2C cmd (trx) +// +// +// true if fetch was successful, false otherwise +// +static bool temperature_sensor_cmd(uint8_t cmd, uint8_t* buffer, uint8_t size) { + + uint32_t timeout = furi_ms_to_ticks(100); + bool ret = false; + + // Aquire I2C and check if device is ready, then release + furi_hal_i2c_acquire(I2C_BUS); + if (furi_hal_i2c_is_device_ready(I2C_BUS, HTU21D_ADDRESS, timeout)) + { + furi_hal_i2c_release(I2C_BUS); + + furi_hal_i2c_acquire(I2C_BUS); + // Transmit given command + ret = furi_hal_i2c_tx(I2C_BUS, HTU21D_ADDRESS, &cmd, 1, timeout); + furi_hal_i2c_release(I2C_BUS); + + if (ret) + { + uint32_t wait_ticks = furi_ms_to_ticks(50); + furi_delay_tick(wait_ticks); + + furi_hal_i2c_acquire(I2C_BUS); + // Receive data + ret = furi_hal_i2c_rx(I2C_BUS, HTU21D_ADDRESS, buffer, size, timeout); + furi_hal_i2c_release(I2C_BUS); + } + } + else + furi_hal_i2c_release(I2C_BUS); + + return ret; +} + +// +// Fetches temperature and humidity from sensor +// +// +// Temperature and humidity must be preallocated +// Note: CRC is not checked (3rd byte) +// +// +// true if fetch was successful, false otherwise +// +static bool temperature_sensor_fetch_data(double* temperature, double* humidity) { + bool ret = false; + + uint16_t adc_raw; + + uint8_t buffer[2] = { 0x00 }; + + // Fetch temperature + ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2); + + if (ret) + { + // Calculate temperature + adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1])); + *temperature = (float)(adc_raw * 175.72 / 65536.00) - 46.85; + + // Fetch humidity + ret = temperature_sensor_cmd((uint8_t)HTU21D_CMD_TEMPERATURE, buffer, 2); + + if (ret) + { + // Calculate humidity + adc_raw = ((uint16_t)(buffer[0] << 8) | (buffer[1])); + *humidity = (float)(adc_raw * 125.0 / 65536.00) - 6.0; + } + } + + return ret; +} + + +// +// Draw callback +// +static void temperature_sensor_draw_callback(Canvas* canvas, void* ctx) { + + UNUSED(ctx); + + canvas_clear(canvas); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 2, 10, "HTU21D Sensor"); + + canvas_set_font(canvas, FontSecondary); + canvas_draw_str(canvas, 2, 62, "Press back to exit."); + + switch (temperature_sensor_current_status) { + case TSSInitializing: + canvas_draw_str(canvas, 2, 30, "Initializing.."); + break; + case TSSNoSensor: + canvas_draw_str(canvas, 2, 30, "No sensor found!"); + break; + case TSSPendingUpdate: { + + canvas_draw_str(canvas, 6, 24, "Temperature"); + canvas_draw_str(canvas, 80, 24, "Humidity"); + + // Draw vertical lines + canvas_draw_line(canvas, 68, 16, 68, 50); + canvas_draw_line(canvas, 69, 16, 69, 50); + + // Draw horizontal line + canvas_draw_line(canvas, 3, 27, 144, 27); + + // Draw temperature and humidity values + canvas_draw_str(canvas, 14, 38, ts_data_buffer_temperature_c); + canvas_draw_str(canvas, 48, 38, "C"); + canvas_draw_str(canvas, 14, 48, ts_data_buffer_temperature_f); + canvas_draw_str(canvas, 48, 48, "F"); + canvas_draw_str(canvas, 78, 42, ts_data_buffer_humidity); + canvas_draw_str(canvas, 112, 42, "%"); + + } break; + default: + break; + } + +} + +// +// Input callback +// +static void temperature_sensor_input_callback(InputEvent* input_event, void* ctx) { + + furi_assert(ctx); + FuriMessageQueue* event_queue = ctx; + + TSEvent event = { .type = TSEventTypeInput, .input = *input_event }; + furi_message_queue_put(event_queue, &event, FuriWaitForever); + +} + +// +// Timer callback +// +static void temperature_sensor_timer_callback(FuriMessageQueue* event_queue) { + + furi_assert(event_queue); + + TSEvent event = { .type = TSEventTypeTick }; + furi_message_queue_put(event_queue, &event, 0); + +} + +// +// App entry point +// +int32_t temperature_sensor_app(void* p) { + + UNUSED(p); + + // Declare our variables + TSEvent tsEvent; + bool sensorFound = false; + double celsius, fahrenheit, humidity; + + FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(TSEvent)); + + // Register callbacks + ViewPort* view_port = view_port_alloc(); + view_port_draw_callback_set(view_port, temperature_sensor_draw_callback, NULL); + view_port_input_callback_set(view_port, temperature_sensor_input_callback, event_queue); + + // Create timer and register its callback + FuriTimer* timer = furi_timer_alloc(temperature_sensor_timer_callback, FuriTimerTypePeriodic, event_queue); + furi_timer_start(timer, furi_kernel_get_tick_frequency()); + + // Register viewport + Gui* gui = furi_record_open(RECORD_GUI); + gui_add_view_port(gui, view_port, GuiLayerFullscreen); + + // Used to notify the user by blinking red (error) or blue (fetch successful) + NotificationApp* notifications = furi_record_open(RECORD_NOTIFICATION); + + // Assign variables a default value + celsius = fahrenheit = humidity = TS_DEFAULT_VALUE; + + while (1) { + + furi_check(furi_message_queue_get(event_queue, &tsEvent, FuriWaitForever) == FuriStatusOk); + + // Handle events + if (tsEvent.type == TSEventTypeInput) { + + // Exit on back key + if (tsEvent.input.key == InputKeyBack) // We dont check for type here, we can check the type of keypress like: (event.input.type == InputTypeShort) + break; + + } + else if (tsEvent.type == TSEventTypeTick) { + + // Update sensor data + // Fetch data and set the sensor current status accordingly + sensorFound = temperature_sensor_fetch_data(&celsius, &humidity); + temperature_sensor_current_status = (sensorFound ? TSSPendingUpdate : TSSNoSensor); + + if (sensorFound) { + + // Blink blue + notification_message(notifications, &sequence_blink_blue_100); + + if (celsius != TS_DEFAULT_VALUE && humidity != TS_DEFAULT_VALUE)) { + + // Convert celsius to fahrenheit + fahrenheit = (celsius * 9 / 5) + 32; + + // Fill our buffers here, not on the canvas draw callback + snprintf(ts_data_buffer_temperature_c, DATA_BUFFER_SIZE, "%.2f", celsius); + snprintf(ts_data_buffer_temperature_f, DATA_BUFFER_SIZE, "%.2f", fahrenheit); + snprintf(ts_data_buffer_humidity, DATA_BUFFER_SIZE, "%.2f", humidity); + } + + } + else { + + // Reset our variables to their default values + celsius = fahrenheit = humidity = TS_DEFAULT_VALUE; + + // Blink red + notification_message(notifications, &sequence_blink_red_100); + + } + } + + uint32_t wait_ticks = furi_ms_to_ticks(!sensorFound ? 100 : 500); + furi_delay_tick(wait_ticks); + } + + // Dobby is freee (free our variables, Flipper will crash if we don't do this!) + furi_timer_free(timer); + gui_remove_view_port(gui, view_port); + view_port_free(view_port); + furi_message_queue_free(event_queue); + + furi_record_close(RECORD_NOTIFICATION); + furi_record_close(RECORD_GUI); + + return 0; +} diff --git a/applications/plugins/temperature_sensor/temperature_sensor.png b/applications/plugins/temperature_sensor/temperature_sensor.png new file mode 100644 index 000000000..b6fe6d7fe Binary files /dev/null and b/applications/plugins/temperature_sensor/temperature_sensor.png differ