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?

  • Are you running it as root? – user96931 Feb 5 at 21:02
  • The DS18B20 has a 1 wire interface, why are you trying to use serial? – CoderMike Feb 5 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) raspberrypi.stackexchange.com/questions/100203/…, (2) raspberrypi.stackexchange.com/questions/102078/…. 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 6 at 1: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 7 at 1:10
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

| improve this answer | |
  • 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 8 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 8 at 11:55
  • 1
    With a breadboard? – rmorelli74 Feb 8 at 17:40
  • 2
    No, breadboards are generally for testing. I’ve soldered connections and resistor onto veroboard. – CoderMike Feb 8 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.