Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Read DS18B20 with Raspberry pi

Read DS18B20 with Raspberry pi

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    p_seg_rpi
    wrote on last edited by
    #1

    Hi,
    i have installed Qt Creator 3.2.1
    Based on Qt 5.3.2 (GCC 4.9.2, 32 bit)
    Built on May 11 2016 at 17:13:20 in my raspberry pi3

    i found header file and c++ code to read the temperature sensor ds18b20.
    i have a project file in qt creator where i designed two text labels for (+) and (-)
    i use them to increase or decrease the set point for temperature.
    i have a third text label3 (Temp) which should display the actual temperature.
    i added a LCDnumber module but i don't want to use it to display temperature but i want to keep it for later.

    So in my project there are some files like:
    -headers ( here i put the header file for ds18b20.h and there is already mainwindow.h)
    -source (here i have main.cpp and mainwindow.cpp)
    -form (here there is mainwindow.ui)

    i tried to put the cpp code to read the sensor in source and i don't know to use the textlabel3
    to read the sensor

    Here i can't attach my project file.
    so here is:
    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "ds18b20.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        this->connect(this->ui->button_plus, SIGNAL(clicked()), this,SLOT(on_button_plus_click()));
        this->connect(this->ui->button_minus, SIGNAL(clicked()), this,SLOT(on_button_minus_click()));
        this->counter = 19;
        this->ui->label_2->setText(QString::number(counter));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_button_plus_click()
    {
            counter++;
            ui->label_2->setText(QString::number(counter));
    }
    
    void MainWindow::on_button_minus_click()
    {
            counter--;
            ui->label_2->setText(QString::number(counter));
    }
    
    //void MainWindow::on_lcd_actual_temp_overflow()
    //{
    
    //}
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_button_plus_click();
    
        void on_button_minus_click();
    
        void on_lcd_actual_temp_overflow();
    
        void on_label_temp_linkActivated();
    
    private:
        Ui::MainWindow *ui;
        int counter;
    };
    
    #endif // MAINWINDOW_H
    

    ds18b20.h

    #ifndef DS18B20_H_
    #define DS18B20_H_
    #include <inttypes.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    #define CELCIUS 0
    #define FAHRENHEIT 1
    #define BUS "/sys/bus/w1/devices/"
    #define TEMPFILE "/w1_slave"
    
    class DS18B20 {
    public:
    	DS18B20(const char* address);
    	virtual ~DS18B20();
    	uint8_t getUnits();
    	void setUnits(uint8_t);
    	float getTemp();
    	float CtoF(float);
    private:
    	uint8_t unit_;
    	char* address_;
    	char path[47]; // path should be 46 chars
    
    };
    

    Now i don't know where to put this file:

    #include "DS18B20.h"
    
    DS18B20::DS18B20(const char* address) {
    	address_ = strdup(address);
    	unit_ = CELCIUS;
    	snprintf(path, 46, "%s%s%s", BUS, address_, TEMPFILE);
    }
    
    DS18B20::~DS18B20() {
    }
    
    float DS18B20::getTemp() {
    	FILE *devFile = fopen(path, "r");
    	if (devFile == NULL) {
    		printf("Count not open %s\n", path);
    		perror("\n");
    	}
    	float temp = -1;
    	if (devFile != NULL) {
    		if (!ferror(devFile)) {
    			unsigned int tempInt;
    			char crcConf[5];
    			fscanf(devFile, "%*x %*x %*x %*x %*x %*x %*x %*x %*x : crc=%*x %s", crcConf);
    			if (strncmp(crcConf, "YES", 3) == 0) {
    				fscanf(devFile, "%*x %*x %*x %*x %*x %*x %*x %*x %*x t=%5d", &tempInt);
    				temp = (float) tempInt / 1000.0;
    			}
    		}
    	}
    	fclose(devFile);
    
    	if (unit_ == CELCIUS) {
    		return temp;
    	} else
    		return CtoF(temp);
    }
    
    uint8_t DS18B20::getUnits() {
    	return unit_;
    }
    
    void DS18B20::setUnits(uint8_t u) {
    	unit_ = u;
    }
    
    float DS18B20::CtoF(float temp) {
    	return temp * 1.8 + 32;
    }
    

    I can find how to use temp object from cpp code
    but where to put this piece of code in my qt project ?
    Thank you
    for your help

    A 1 Reply Last reply
    0
    • P p_seg_rpi

      Hi,
      i have installed Qt Creator 3.2.1
      Based on Qt 5.3.2 (GCC 4.9.2, 32 bit)
      Built on May 11 2016 at 17:13:20 in my raspberry pi3

      i found header file and c++ code to read the temperature sensor ds18b20.
      i have a project file in qt creator where i designed two text labels for (+) and (-)
      i use them to increase or decrease the set point for temperature.
      i have a third text label3 (Temp) which should display the actual temperature.
      i added a LCDnumber module but i don't want to use it to display temperature but i want to keep it for later.

      So in my project there are some files like:
      -headers ( here i put the header file for ds18b20.h and there is already mainwindow.h)
      -source (here i have main.cpp and mainwindow.cpp)
      -form (here there is mainwindow.ui)

      i tried to put the cpp code to read the sensor in source and i don't know to use the textlabel3
      to read the sensor

      Here i can't attach my project file.
      so here is:
      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "ds18b20.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          this->connect(this->ui->button_plus, SIGNAL(clicked()), this,SLOT(on_button_plus_click()));
          this->connect(this->ui->button_minus, SIGNAL(clicked()), this,SLOT(on_button_minus_click()));
          this->counter = 19;
          this->ui->label_2->setText(QString::number(counter));
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_button_plus_click()
      {
              counter++;
              ui->label_2->setText(QString::number(counter));
      }
      
      void MainWindow::on_button_minus_click()
      {
              counter--;
              ui->label_2->setText(QString::number(counter));
      }
      
      //void MainWindow::on_lcd_actual_temp_overflow()
      //{
      
      //}
      

      main.cpp

      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          return a.exec();
      }
      

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      private slots:
          void on_button_plus_click();
      
          void on_button_minus_click();
      
          void on_lcd_actual_temp_overflow();
      
          void on_label_temp_linkActivated();
      
      private:
          Ui::MainWindow *ui;
          int counter;
      };
      
      #endif // MAINWINDOW_H
      

      ds18b20.h

      #ifndef DS18B20_H_
      #define DS18B20_H_
      #include <inttypes.h>
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <errno.h>
      
      #define CELCIUS 0
      #define FAHRENHEIT 1
      #define BUS "/sys/bus/w1/devices/"
      #define TEMPFILE "/w1_slave"
      
      class DS18B20 {
      public:
      	DS18B20(const char* address);
      	virtual ~DS18B20();
      	uint8_t getUnits();
      	void setUnits(uint8_t);
      	float getTemp();
      	float CtoF(float);
      private:
      	uint8_t unit_;
      	char* address_;
      	char path[47]; // path should be 46 chars
      
      };
      

      Now i don't know where to put this file:

      #include "DS18B20.h"
      
      DS18B20::DS18B20(const char* address) {
      	address_ = strdup(address);
      	unit_ = CELCIUS;
      	snprintf(path, 46, "%s%s%s", BUS, address_, TEMPFILE);
      }
      
      DS18B20::~DS18B20() {
      }
      
      float DS18B20::getTemp() {
      	FILE *devFile = fopen(path, "r");
      	if (devFile == NULL) {
      		printf("Count not open %s\n", path);
      		perror("\n");
      	}
      	float temp = -1;
      	if (devFile != NULL) {
      		if (!ferror(devFile)) {
      			unsigned int tempInt;
      			char crcConf[5];
      			fscanf(devFile, "%*x %*x %*x %*x %*x %*x %*x %*x %*x : crc=%*x %s", crcConf);
      			if (strncmp(crcConf, "YES", 3) == 0) {
      				fscanf(devFile, "%*x %*x %*x %*x %*x %*x %*x %*x %*x t=%5d", &tempInt);
      				temp = (float) tempInt / 1000.0;
      			}
      		}
      	}
      	fclose(devFile);
      
      	if (unit_ == CELCIUS) {
      		return temp;
      	} else
      		return CtoF(temp);
      }
      
      uint8_t DS18B20::getUnits() {
      	return unit_;
      }
      
      void DS18B20::setUnits(uint8_t u) {
      	unit_ = u;
      }
      
      float DS18B20::CtoF(float temp) {
      	return temp * 1.8 + 32;
      }
      

      I can find how to use temp object from cpp code
      but where to put this piece of code in my qt project ?
      Thank you
      for your help

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @p_seg_rpi You can just put it in your MainWindow class in mainwindow.h as a private variable.

      Something like:

      class MainWindow : public QMainWindow
      {
      private:
         DS18B20 *tempReader_;
      }
      
      // then you can allocate it in the mainwindow constructor
      MainWindow::MainWindow()
      {
         tempReader_ = new DS18B20(address);
      }
      

      Then to get a temp update just call tempReader_->getTemp(). You could use a QTimer to do this every second (or whatever) and then update your GUI control with the proper temp after every call to getTemp().

      Hope that helps.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      2
      • Z Offline
        Z Offline
        zeuqma
        wrote on last edited by
        #3

        Hello, i am very new this qt. I tested this example but can not work. Please somebody all of them write source code. How to use configuration ds 18b20

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved