From 2f5f753ab6466823ce7ca25d07b7633f9c255eb4 Mon Sep 17 00:00:00 2001 From: Reimar Date: Wed, 26 Mar 2025 09:58:43 +0100 Subject: [PATCH] Implement printing temperature on display --- iot/Makefile | 6 ++-- iot/devices/display.c | 60 +++++++++++++++++++++++++++++++++ iot/devices/display.h | 10 ++++++ iot/{ => devices}/temperature.c | 38 +++++++++++---------- iot/{ => devices}/temperature.h | 0 iot/main.c | 21 ++++++++++-- 6 files changed, 113 insertions(+), 22 deletions(-) create mode 100644 iot/devices/display.c create mode 100644 iot/devices/display.h rename iot/{ => devices}/temperature.c (62%) rename iot/{ => devices}/temperature.h (100%) diff --git a/iot/Makefile b/iot/Makefile index 0f0e886..62e1390 100644 --- a/iot/Makefile +++ b/iot/Makefile @@ -1,3 +1,5 @@ -all: main.c brokers/mqtt.c brokers/amqp.c temperature.c device_id.c - $(CC) -lmosquitto -lrabbitmq -lpthread -li2c main.c brokers/mqtt.c brokers/amqp.c temperature.c device_id.c +FILES=main.c brokers/mqtt.c brokers/amqp.c devices/temperature.c devices/display.c device_id.c + +all: $(FILES) + $(CC) -lmosquitto -lrabbitmq -lpthread -li2c $(FILES) diff --git a/iot/devices/display.c b/iot/devices/display.c new file mode 100644 index 0000000..6c90e08 --- /dev/null +++ b/iot/devices/display.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "display.h" + +#define JHD1313_BUS "/dev/i2c-2" +#define JHD1313_ADR 0x3e + +void display_set_cursor_pos(display_handle_t display, int x, int y) +{ + i2c_smbus_write_byte_data(display, 0x00, (y ? 0xC0 : 0x80) + x); +} + +void display_write_str(display_handle_t display, char *str) +{ + while (*str) { + display_write_char(display, *str); + str++; + } +} + +void display_write_char(display_handle_t display, char ch) +{ + i2c_smbus_write_byte_data(display, 0x40, ch); +} + +display_handle_t init_display() +{ + int file = open(JHD1313_BUS, O_RDWR); + + if (file < 0) { + perror("Error opening display device"); + exit(EXIT_FAILURE); + } + + if (ioctl(file, I2C_SLAVE, JHD1313_ADR) == -1) { + fprintf(stderr, "ERROR: setting address %d on i2c bus %s with ioctl() - %s", JHD1313_ADR, JHD1313_BUS, strerror(errno)); + exit(EXIT_FAILURE); + } + + // 2 line mode, 5x8 + i2c_smbus_write_byte_data(file, 0x00, 0x28); + // Display on, cursor on, blink off + i2c_smbus_write_byte_data(file, 0x00, 0x0C); + // Display clear + i2c_smbus_write_byte_data(file, 0x00, 0x01); + // Entry mode set + i2c_smbus_write_byte_data(file, 0x00, 0x06); + + i2c_smbus_write_byte_data(file, 0x00, 0x02); + + return file; +} + diff --git a/iot/devices/display.h b/iot/devices/display.h new file mode 100644 index 0000000..bc217c1 --- /dev/null +++ b/iot/devices/display.h @@ -0,0 +1,10 @@ +typedef int display_handle_t; + +display_handle_t init_display(); + +void display_set_cursor_pos(display_handle_t display, int x, int y); + +void display_write_str(display_handle_t display, char *str); + +void display_write_char(display_handle_t display, char ch); + diff --git a/iot/temperature.c b/iot/devices/temperature.c similarity index 62% rename from iot/temperature.c rename to iot/devices/temperature.c index 1719733..055dbbe 100644 --- a/iot/temperature.c +++ b/iot/devices/temperature.c @@ -11,8 +11,10 @@ #include "temperature.h" -#define MPC9808_BUS "/dev/i2c-2" -#define MPC9808_ADR 0x18 +#define MCP9808_BUS "/dev/i2c-2" +#define MCP9808_ADR 0x18 +#define MCP9808_MANID 0x0054 +#define MCP9808_DEVID 0x04 #define CONFIG_REG 0x01 #define TUPPER_REG 0x02 @@ -53,16 +55,16 @@ double get_temperature(temperature_handle_t file) temperature_handle_t init_temperature(void) { - int file = open(MPC9808_BUS, O_RDWR); + int file = open(MCP9808_BUS, O_RDWR); if (file < 0) { - fprintf(stderr, "Error opening temperature sensor device (%s): %s\n", MPC9808_BUS, strerror(errno)); - exit(1); + perror("Error opening temperature sensor device"); + exit(EXIT_FAILURE); } - if (ioctl(file, I2C_SLAVE, MPC9808_ADR) == -1) { - fprintf(stderr, "ERROR: setting address %d on i2c bus %s with ioctl() - %s", MPC9808_ADR, MPC9808_BUS, strerror(errno)); - exit(1); + if (ioctl(file, I2C_SLAVE, MCP9808_ADR) == -1) { + fprintf(stderr, "ERROR: setting address %d on i2c bus %s with ioctl() - %s", MCP9808_ADR, MCP9808_BUS, strerror(errno)); + exit(EXIT_FAILURE); } int32_t reg32; @@ -73,23 +75,23 @@ temperature_handle_t init_temperature(void) reg32 = i2c_smbus_read_word_data(file, MANID_REG); if (reg32 < 0) { - fprintf(stderr, "ERROR: Read failed on i2c bus register %d - %s\n", MANID_REG,strerror(errno)); - exit(1); + fprintf(stderr, "Read failed on i2c bus register %d: %s\n", MANID_REG, strerror(errno)); + exit(EXIT_FAILURE); } - if (bswap_16(reg16poi[0]) != 0x0054) { - fprintf(stderr, "Manufactorer ID wrong is 0x%x should be 0x54\n",__bswap_16(reg16poi[0])); - exit(1); + if (bswap_16(reg16poi[0]) != MCP9808_MANID) { + fprintf(stderr, "Invalid manufacturer ID: Expected 0x%x, got 0x%x\n", MCP9808_MANID, __bswap_16(reg16poi[0])); + exit(EXIT_FAILURE); } // Read device ID and revision reg32 = i2c_smbus_read_word_data(file, DEVID_REG); if (reg32 < 0) { - fprintf(stderr, "ERROR: Read failed on i2c bus register %d - %s\n", DEVID_REG,strerror(errno) ); - exit(1); + fprintf(stderr, "Read failed on i2c bus register %d - %s\n", DEVID_REG, strerror(errno)); + exit(EXIT_FAILURE); } - if (reg8poi[0] != 0x04) { - fprintf(stderr, "Manufactorer ID OK but device ID wrong is 0x%x should be 0x4\n",reg8poi[0]); - exit(1); + if (reg8poi[0] != MCP9808_DEVID) { + fprintf(stderr, "Invalid device ID - expected 0x%x, got 0x%x\n", MCP9808_DEVID, reg8poi[0]); + exit(EXIT_FAILURE); } return file; diff --git a/iot/temperature.h b/iot/devices/temperature.h similarity index 100% rename from iot/temperature.h rename to iot/devices/temperature.h diff --git a/iot/main.c b/iot/main.c index ad6014c..9c0184d 100644 --- a/iot/main.c +++ b/iot/main.c @@ -7,7 +7,8 @@ #include #include "brokers/amqp.h" -#include "temperature.h" +#include "devices/temperature.h" +#include "devices/display.h" #include "device_id.h" void *watch_temperature(void *arg) @@ -16,14 +17,21 @@ void *watch_temperature(void *arg) printf("Device ID: %s\n", device_id); - temperature_handle_t temp_handle = init_temperature(); + display_handle_t display = init_display(); + display_write_str(display, " "); + display_set_cursor_pos(display, 0, 1); + display_write_str(display, "Device....."); + display_write_str(display, device_id); + temperature_handle_t temp_handle = init_temperature(); get_temperature(temp_handle); while (true) { + // Retrieve data double temperature = get_temperature(temp_handle); size_t timestamp = time(NULL); + // Send JSON char *format = "{" "\"temperature\": %lf," "\"device_id\": \"%s\"," @@ -37,6 +45,15 @@ void *watch_temperature(void *arg) free(str); + // Print on display + str = malloc(17); + sprintf(str, "===[ %.1lf\xDF" "C ]===", temperature); + + display_set_cursor_pos(display, 0, 0); + display_write_str(display, str); + + free(str); + printf("Temperature: %lf\n", temperature); sleep(60);