Ah, now I saw the issue. The original code divides the temperature by 1000, and I'm not doing that. That's why you are getting this crazy number. This code fixes that. Keep in mind that the number you will see is the temperature in C times 100 (so to keep at least 2 decimal digits. This means that 25.437 C = 2543
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
//-----------------------------------------------------------------------------
// DISCLAIMER: EDDITING THIS FILE CAN BREAK YOUR OPENPLC RUNTIME! IF YOU DON'T
// KNOW WHAT YOU'RE DOING, JUST DON'T DO IT. EDIT AT YOUR OWN RISK.
//
// PS: You can always restore original functionality if you broke something
// in here by clicking on the "Restore Original Code" button above.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// These are the ignored I/O vectors. If you want to override how OpenPLC
// handles a particular input or output, you must put them in the ignored
// vectors. For example, if you want to override %IX0.5, %IX0.6 and %IW3
// your vectors must be:
// int ignored_bool_inputs[] = {5, 6}; //%IX0.5 and %IX0.6 ignored
// int ignored_int_inputs[] = {3}; //%IW3 ignored
//
// Every I/O on the ignored vectors will be skipped by OpenPLC hardware layer
//-----------------------------------------------------------------------------
int ignored_bool_inputs[] = {2}; //sensor is connected to pin %IX0.2 so we need to disable it
int ignored_bool_outputs[] = {-1};
int ignored_int_inputs[] = {0}; //we are writing 1-wire sensor readings to %IW0
int ignored_int_outputs[] = {-1};
char devPath[128];
char dev[16];
//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures for your custom layer should be here.
//-----------------------------------------------------------------------------
void initCustomLayer()
{
unsigned char log_msg[1000];
DIR *dir;
struct dirent *dirent;
char path[] = "/sys/bus/w1/devices";
dir = opendir(path);
if (dir != NULL)
{
while ((dirent = readdir(dir)))
// 1-wire devices are links beginning with 28-
if (dirent->d_type == DT_LNK && strstr(dirent->d_name, "28-") != NULL)
{
strcpy(dev, dirent->d_name);
sprintf(log_msg, "\nDevice: %s\n", dev);
log(log_msg);
}
(void)closedir(dir);
}
else
{
sprintf(log_msg, "Couldn't open the w1 devices directory\n");
log(log_msg);
}
// Assemble path to OneWire device
sprintf(devPath, "%s/%s/w1_slave", path, dev);
}
//-----------------------------------------------------------------------------
// This function is called by OpenPLC in a loop. Here the internal input
// buffers must be updated with the values you want. Make sure to use the mutex
// bufferLock to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateCustomIn()
{
// Example Code - Overwritting %IW3 with a fixed value
// If you want to have %IW3 constantly reading a fixed value (for example, 53)
// you must add %IW3 to the ignored vectors above, and then just insert this
// single line of code in this function:
// if (int_input[3] != NULL) *int_input[3] = 53;
unsigned char log_msg[1000];
char tmpData[6];
ssize_t numRead;
char buf[256];
int fd = open(devPath, O_RDONLY);
if (fd == -1)
{
sprintf(log_msg, "Couldn't open the w1 device\n");
log(log_msg);
}
while ((numRead = read(fd, buf, 256)) > 0)
{
strncpy(tmpData, strstr(buf, "t=") + 2, 5);
float tempC = strtof(tmpData, NULL);
//save value in OpenPLC buffer
if (int_input[0] != NULL) *int_input[0] = (uint16_t)(tempC / 10);
//print values in Dashboard
//sprintf(log_msg, "Device: %s - Temp: %.3f C %.3f F\n\n", dev, (tempC / 1000), (tempC / 1000) * 9 / 5 + 32));
//log(log_msg);
}
close(fd);
}
//-----------------------------------------------------------------------------
// This function is called by OpenPLC in a loop. Here the internal output
// buffers must be updated with the values you want. Make sure to use the mutex
// bufferLock to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateCustomOut()
{
// Example Code - Sending %QW5 value over I2C
// If you want to have %QW5 output to be sent over I2C instead of the
// traditional output for your board, all you have to do is, first add
// %QW5 to the ignored vectors, and then define a send_over_i2c()
// function for your platform. Finally you can call send_over_i2c() to
// send your %QW5 value, like this:
// if (int_output[5] != NULL) send_over_i2c(*int_output[5]);
//
// Important observation: If your I2C pins are used by OpenPLC I/Os, you
// must also add those I/Os to the ignored vectors, otherwise OpenPLC
// will try to control your I2C pins and your I2C message won't work.
}