3

I have this "QinHeng Electronics HL-340 USB-Serial adapter" and a couple of DS18B20 temperature/humidity sensor but I can't get to read data using digitemp. Also tried with this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main(int argc, char* argv[]) {

    printf("Hllp stpd");
    struct termios serial;
    char inBuff[100];
    char logBuff[100];
    char *buffer;
    char *logPtr;
    int wcount = 0;
    int j;
    int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
    FILE *log;

    if (fd == -1) {
        perror(argv[2]);
        return -1;
    }

    if (tcgetattr(fd, &serial) < 0) {
        perror("Getting configuration");
        return -1;
    }

    //////////////////// Set up Serial Configuration ////////////
    serial.c_iflag = 0;
    serial.c_oflag = 0;
    serial.c_lflag = 0;
    serial.c_cflag = 0;

    serial.c_cc[VMIN] = 0;
    serial.c_cc[VTIME] = 10;

    serial.c_cflag = B9600 | CS8 | CREAD;
    fcntl(fd,F_SETFL,0);
    tcsetattr(fd, TCSANOW, &serial); // Apply configuration
    //////////////////////////////////////////////////////////////


    int rcount;
    buffer = &inBuff[0];
    logPtr = &logBuff[0];

    while(1)
    {
        buffer = &inBuff[0];
        memset(buffer,0,sizeof(inBuff));
        logPtr = &logBuff[0];
        memset(logPtr,0,sizeof(logBuff));

        while( (rcount = read(fd,buffer,1)) > 0)
        {
            if (rcount < 0) {
                perror("Read");
                return -1;
            }
            buffer++;
        }

        for(j=0;j<sizeof(inBuff);j++)
        {
            if(inBuff[j] == '+') {
                while(inBuff[j++] != '\r'){
                    *logPtr = inBuff[j-1];logPtr++;
                }
                *logPtr = '\r';logPtr++;
                *logPtr = '\n';logPtr++;
            }
        }
        printf("%s",logBuff);
        printf("\r\n");
        log = fopen("log.txt","w");
        fwrite(logBuff,1,sizeof(logBuff),log);
        fclose(log);
    }
    close(fd);
}

no success at all. Any hints?

rmorelli74
  • 31
  • 2

1 Answers1

3

You don't need any adapters to connect 2x DS18B20 temperature sensors to a Raspberry Pi. These sensors use the 1-wire interface which is built into the Pi. Only a resistor is required. The sensors can be connected in parallel and identified by a unique id.

https://thepihut.com/blogs/raspberry-pi-tutorials/18095732-sensors-temperature-with-the-1-wire-interface-and-the-ds18b20

enter image description here

CoderMike
  • 7,102
  • 1
  • 11
  • 16