Implement printing temperature on display
This commit is contained in:
parent
60c0fc9475
commit
2f5f753ab6
@ -1,3 +1,5 @@
|
|||||||
all: 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
|
||||||
$(CC) -lmosquitto -lrabbitmq -lpthread -li2c main.c brokers/mqtt.c brokers/amqp.c temperature.c device_id.c
|
|
||||||
|
all: $(FILES)
|
||||||
|
$(CC) -lmosquitto -lrabbitmq -lpthread -li2c $(FILES)
|
||||||
|
|
||||||
|
60
iot/devices/display.c
Normal file
60
iot/devices/display.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include <linux/i2c-dev.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <i2c/smbus.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
10
iot/devices/display.h
Normal file
10
iot/devices/display.h
Normal file
@ -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);
|
||||||
|
|
@ -11,8 +11,10 @@
|
|||||||
|
|
||||||
#include "temperature.h"
|
#include "temperature.h"
|
||||||
|
|
||||||
#define MPC9808_BUS "/dev/i2c-2"
|
#define MCP9808_BUS "/dev/i2c-2"
|
||||||
#define MPC9808_ADR 0x18
|
#define MCP9808_ADR 0x18
|
||||||
|
#define MCP9808_MANID 0x0054
|
||||||
|
#define MCP9808_DEVID 0x04
|
||||||
|
|
||||||
#define CONFIG_REG 0x01
|
#define CONFIG_REG 0x01
|
||||||
#define TUPPER_REG 0x02
|
#define TUPPER_REG 0x02
|
||||||
@ -53,16 +55,16 @@ double get_temperature(temperature_handle_t file)
|
|||||||
|
|
||||||
temperature_handle_t init_temperature(void)
|
temperature_handle_t init_temperature(void)
|
||||||
{
|
{
|
||||||
int file = open(MPC9808_BUS, O_RDWR);
|
int file = open(MCP9808_BUS, O_RDWR);
|
||||||
|
|
||||||
if (file < 0) {
|
if (file < 0) {
|
||||||
fprintf(stderr, "Error opening temperature sensor device (%s): %s\n", MPC9808_BUS, strerror(errno));
|
perror("Error opening temperature sensor device");
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(file, I2C_SLAVE, MPC9808_ADR) == -1) {
|
if (ioctl(file, I2C_SLAVE, MCP9808_ADR) == -1) {
|
||||||
fprintf(stderr, "ERROR: setting address %d on i2c bus %s with ioctl() - %s", MPC9808_ADR, MPC9808_BUS, strerror(errno));
|
fprintf(stderr, "ERROR: setting address %d on i2c bus %s with ioctl() - %s", MCP9808_ADR, MCP9808_BUS, strerror(errno));
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t reg32;
|
int32_t reg32;
|
||||||
@ -73,23 +75,23 @@ temperature_handle_t init_temperature(void)
|
|||||||
reg32 = i2c_smbus_read_word_data(file, MANID_REG);
|
reg32 = i2c_smbus_read_word_data(file, MANID_REG);
|
||||||
|
|
||||||
if (reg32 < 0) {
|
if (reg32 < 0) {
|
||||||
fprintf(stderr, "ERROR: Read failed on i2c bus register %d - %s\n", MANID_REG,strerror(errno));
|
fprintf(stderr, "Read failed on i2c bus register %d: %s\n", MANID_REG, strerror(errno));
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (bswap_16(reg16poi[0]) != 0x0054) {
|
if (bswap_16(reg16poi[0]) != MCP9808_MANID) {
|
||||||
fprintf(stderr, "Manufactorer ID wrong is 0x%x should be 0x54\n",__bswap_16(reg16poi[0]));
|
fprintf(stderr, "Invalid manufacturer ID: Expected 0x%x, got 0x%x\n", MCP9808_MANID, __bswap_16(reg16poi[0]));
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read device ID and revision
|
// Read device ID and revision
|
||||||
reg32 = i2c_smbus_read_word_data(file, DEVID_REG);
|
reg32 = i2c_smbus_read_word_data(file, DEVID_REG);
|
||||||
if (reg32 < 0) {
|
if (reg32 < 0) {
|
||||||
fprintf(stderr, "ERROR: Read failed on i2c bus register %d - %s\n", DEVID_REG,strerror(errno) );
|
fprintf(stderr, "Read failed on i2c bus register %d - %s\n", DEVID_REG, strerror(errno));
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (reg8poi[0] != 0x04) {
|
if (reg8poi[0] != MCP9808_DEVID) {
|
||||||
fprintf(stderr, "Manufactorer ID OK but device ID wrong is 0x%x should be 0x4\n",reg8poi[0]);
|
fprintf(stderr, "Invalid device ID - expected 0x%x, got 0x%x\n", MCP9808_DEVID, reg8poi[0]);
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return file;
|
return file;
|
21
iot/main.c
21
iot/main.c
@ -7,7 +7,8 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "brokers/amqp.h"
|
#include "brokers/amqp.h"
|
||||||
#include "temperature.h"
|
#include "devices/temperature.h"
|
||||||
|
#include "devices/display.h"
|
||||||
#include "device_id.h"
|
#include "device_id.h"
|
||||||
|
|
||||||
void *watch_temperature(void *arg)
|
void *watch_temperature(void *arg)
|
||||||
@ -16,14 +17,21 @@ void *watch_temperature(void *arg)
|
|||||||
|
|
||||||
printf("Device ID: %s\n", device_id);
|
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);
|
get_temperature(temp_handle);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
// Retrieve data
|
||||||
double temperature = get_temperature(temp_handle);
|
double temperature = get_temperature(temp_handle);
|
||||||
size_t timestamp = time(NULL);
|
size_t timestamp = time(NULL);
|
||||||
|
|
||||||
|
// Send JSON
|
||||||
char *format = "{"
|
char *format = "{"
|
||||||
"\"temperature\": %lf,"
|
"\"temperature\": %lf,"
|
||||||
"\"device_id\": \"%s\","
|
"\"device_id\": \"%s\","
|
||||||
@ -37,6 +45,15 @@ void *watch_temperature(void *arg)
|
|||||||
|
|
||||||
free(str);
|
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);
|
printf("Temperature: %lf\n", temperature);
|
||||||
|
|
||||||
sleep(60);
|
sleep(60);
|
||||||
|
Loading…
Reference in New Issue
Block a user