Raspberry PI USB DS18B20

 
 
Needed 
1 RaspberryPI
1 USB<>DS18B20
recommend install Raspbian
 
 
 
 
 
 
In terminal 
 
 
 
 
 
 
In web browser
 
 
 
 
 
 
 
 
C code
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
 
int main(int argc, char* argv[]) {
 
    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;
    //log = fopen("log.txt","w");
 
    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] == '+') {
//printf("\r\n");
while(inBuff[j++] != '\r'){
//printf("%c",inBuff[j-1]);
*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);
}
 
 
 
 
 
 
create log.txt file and set chmod -R 777 /path/to/file.log to folder
 
 
 
 
PHP Script
 
<?php
$handle = @fopen("log.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>
 
 
 
 
...............
Read 7079 times