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
  • Are you running it as root? – user96931 Feb 05 '20 at 21:02
  • The DS18B20 has a 1 wire interface, why are you trying to use serial? – CoderMike Feb 05 '20 at 22:57
  • Hi @rmorelli74, Perhaps your 1-Wire driver is not that up to date. My answers to the following questions might help: (1) https://raspberrypi.stackexchange.com/questions/100203/ds18b20-temperature-sensor-rpi-3-4-driver-wiring-detection-and-python-progr, (2) https://raspberrypi.stackexchange.com/questions/102078/ds18b20-temperature-sensor-rj-x. Good luck and cheers. Ah, one more thing, I once ENABLED 1-Wire and installed DS18B20 driver, but then found serial/UART no longer worked. I disabled 1-Wire and found UART works again. So the two might not work together at the same time. – tlfong01 Feb 06 '20 at 01:39
  • @CoderMike ...yep, I bought the wrong adapter. I want to connect two DS18B20 using usb for heat pump send/return water temperature monitoring. – rmorelli74 Feb 07 '20 at 01:10

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
  • 6,982
  • 1
  • 10
  • 15
  • 1
    I can't do this because both sensors Will serve monitoring for my heat pump send and return, they have cable extension and Raspberry Will be attached on the wall – rmorelli74 Feb 08 '20 at 11:46
  • 1
    Why can't you do this? I have 5 temperature sensors in parallel, 4 monitoring my hot water tank and 1 monitoring the pipe to my radiators. – CoderMike Feb 08 '20 at 11:55
  • 1
    With a breadboard? – rmorelli74 Feb 08 '20 at 17:40
  • 2
    No, breadboards are generally for testing. I’ve soldered connections and resistor onto veroboard. – CoderMike Feb 08 '20 at 20:18