Click here to Skip to main content
15,073,911 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys, i am c programming user. i have a project using raspberry Pi. i have a temperature sensor ds18b20 and its code, i able show its reading value in uxterm or Terminal. i recently make a simple gtk (gui) that do not have any button or any toolbar. i only able make a label that does not connect to the sensor reading value. how do i combine these code in order to make a gtk (gui) that able display the temperature sensor reading value and able update its reading all the time in any second when temperature change? the project is only want C programming not other language. the below code are

/*dsb18b20 sensor c-language*/

#include <wiringPi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>

#define		BUFSIZE		128

typedef unsigned char uchar;
typedef unsigned int  uint;

float tempRead(void)
	float temp;
	int i, j;
    int fd;
	int ret;

	char buf[BUFSIZE];
	char tempBuf[5];
	fd = open("/sys/bus/w1/devices/28-0314979400b5/w1_slave", O_RDONLY);

	if(-1 == fd){
		perror("open device file error");
		return 1;

		ret = read(fd, buf, BUFSIZE);
		if(0 == ret){
		if(-1 == ret){
			if(errno == EINTR){
			return 1;

		if(buf[i] == 't'){
				tempBuf[j] = buf[i+2+j]; 	

	temp = (float)atoi(tempBuf) / 1000;


	return temp;

int main(void)
	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !");
		return 1; 
    float temp;
		temp = tempRead();
		printf("Current temperature : %0.3f\n", temp);
    return 0;


/*simple gtk c-language*/

#include <gtk/gtk.h>

void main(int argc, char *argv[])
   gtk_init (&argc, &argv);
   GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);

   gtk_window_set_default_size(GTK_WINDOW(win), 250, 150);

   GtkWidget *lbl = gtk_label_new ("100 C");
   gtk_container_add (GTK_CONTAINER (win), lbl);

   gtk_widget_show_all (win);



i am trying to replaced label "100 C" with live sensor reading. my gtk able shows reading value of sensors. can someone help me?

What I have tried:

i still starter learning c-programming, current i had no idea how to combine these 2 code.
Updated 11-Mar-20 2:46am

Have a look at this page Asynchronous GUI Update in GTK[^], see gdk_threads_add_timeout example.

Note you may use sprintf[^] (just like you've used printf) to set the buffer that, in turn, will be used to update the label.
You can change the contents of a label with the gtk_label_set_text()[^] function and set up a callback function that gets called each second with g_timeout_add_seconds()[^]. Your callback function should read the temperature using the tempRead() function from the first code sample, format the float value into a string with snprintf() and set the label.
In your main program loop you should sample the temperature at fixed intervals, or use some event handler to capture changes. Whenever a new temperature value is captured you pass that to the GTK function to updtate the display.
boonC 11-Mar-20 6:22am
i mean is the sensor is already connect to raspberry pi, when i open the created gtk, it will show the temperature value and its value will automatically update per second according the sensor detect.
Richard MacCutchan 11-Mar-20 6:30am
Yes, but you need to capture the data from the sensor, convert it into some readable text string, and pass it to the GTK function to be displayed.
boonC 11-Mar-20 7:00am
may i ask if printf show variable reading value, is it possible to transfer the printf reading to inside gtk, how i compile the code using c-language?
Richard MacCutchan 11-Mar-20 7:27am
Sorry, I do not know GTK, but I assume you just add the GTK function to your main program, and call it from your main method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900