User Tools

Site Tools


raspberrypi:temp

This is an old revision of the document!


Rasperry Pi turi 1-wire protokolo palaikymą. Pats populiariausias W1 įrenginys yra skaitmeninis termometras DS18B20

panaudojus sita gida pavyko gauti temperaturas ir jas irasyti i faila:

Čia iš komandinės eilutės parašius komandas galima pamatyti temperatūrą:

ls /sys/bus/w1/devices/

Čia 28-0316881dadff yra termometro/mikroschemos numeris. Jei yra keli tai bus kelios tokios direktorijos. Ir Temperatūra yra 22,250 laipsnių pagal Celsijų.

tada parasiau nedidele c programa kuri raso i loga temperaturas ir ją paleidau kas 10 sekundžių

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
/*
gcc -Wall -pthread -o w1_temp_rec w1_temp_rec.c && sudo ./w1_temp_rec
sudo ./w1_temp_rec
 
watch -n10 ./w1_temp_rec
*/
 
int main()
{
    FILE * temp_log = fopen("temp_log.txt", "a");
 
    time_t now;
    time(&now);
    char buf[sizeof "2011-10-08T07:07:09Z"];
    strftime(buf, sizeof buf, "%FT%TZ", gmtime(&now));
    //printf("%s\n", buf);
 
    printf("record temperature readings to file\n");
    FILE * fp;
    char * line = NULL;
    size_t len = 0;
    fp = fopen("/sys/bus/w1/devices/28-031688217aff/w1_slave", "r");
    getline(&line, &len, fp); //printf("%s", line);
    getline(&line, &len, fp); 
    strftime(buf, sizeof buf, "%FT%TZ", gmtime(&now)); 
    printf("%s OUT %s", buf, line);
    fprintf(temp_log, "%s OUT %s", buf, line);
 
    fclose(fp);
 
    fp = fopen("/sys/bus/w1/devices/28-03168812d2ff/w1_slave", "r");
    getline(&line, &len, fp); //printf("%s", line);
    getline(&line, &len, fp); 
    strftime(buf, sizeof buf, "%FT%TZ", gmtime(&now)); 
    printf("%s IN %s", buf, line);
    fprintf(temp_log, "%s IN %s", buf, line);
    fclose(fp);
 
    fclose(temp_log);
 
 
    if (line) free(line);
    return 0;
}

reikia paleisti kad kas minute rasitu i faila:

crontab -e

ideti tokia eilute:

* * * * * ./w1_temp_rec

Paleidau 2 termometrus, viesnas matuoja temperatūrą namuose kitas lauke. Tada nubraižiau grafikus su matlab ir gavau toki vaizdą:

%% read temperature log file and plot chart
fileID = fopen('\\aistis\pihome\home\pi\temp_log.txt','r');
C = textscan(fileID,'%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %f','Delimiter',' =');
fclose(fileID);
d = struct('date',num2cell(datenum(C{1},'yyyy-mm-ddTHH:MM:SSZ')), ...
    'type', C{2}, 'temp', num2cell(C{end}./1000));
 
figure(1);clf;
l = strcmp({d.type},'IN');
plot([d(l).date],[d(l).temp]);
dynamicDateTicks;grid on;title('temperature in the house');
xlabel('time');ylabel('temperature in celcius');
 
figure(2);clf;
l = strcmp({d.type},'OUT');
plot([d(l).date],[d(l).temp],'r');
dynamicDateTicks;grid on;title('temperature outside');
xlabel('time');ylabel('temperature in celcius');

Ir gavau tokį vaizdą.

Čia kelios nuotraukos

Žemiau MYSQL irasymas į duomenų bazę temperatūros:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
 
#include <mysql/mysql.h>
 
//#include <pigpio.h>
 
//get temperature from w1 sendor and insert into database
 
// specify sensor names to insert into database
// temp_mysql 28-0316881dadff 28-0316881dadf1 28-0316881dadf2
 
// pataisyti skripta kad rasytu i db 
// sudeti senus duomenis i baze
// paliesti rasyti naujus duomenis i baze
 
// nunesti rpi aistis i kampa 
// pritvirtinti laida prie sienos
// nupirkti balto laido ir baltu laikikliu kikek? irt dezute juoda ant faxverko vandeniui nelaidzia
 
 
//TODO: auto create table
//TODO: verify CRC value YES in sensor file
 
/*   instaliuoti
sudo apt-get install libmysqlclient-dev -y
*/
 
/* 
gcc -Wall -pthread -o temp_mysql temp_mysql.c `mysql_config --libs`
sudo ./temp_mysql 28-031688217aff 28-03168812d2ff 
*/
 
/*
CREATE TABLE if not exist temperature3 (ts timestamp NOT NULL, value double NOT NULL, sensor_id bigint NOT NULL) ENGINE=MyISAM
 
*/
 
// You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with) {
    char *result; // the return string
    char *ins;    // the next insert point
    char *tmp;    // varies
    int len_rep;  // length of rep (the string to remove)
    int len_with; // length of with (the string to replace rep with)
    int len_front; // distance between rep and end of last rep
    int count;    // number of replacements
 
    // sanity checks and initialization
    if (!orig || !rep)
        return NULL;
    len_rep = strlen(rep);
    if (len_rep == 0)
        return NULL; // empty rep causes infinite loop during count
    if (!with)
        with = "";
    len_with = strlen(with);
 
    // count the number of replacements needed
    ins = orig;
    for (count = 0; (tmp = strstr(ins, rep)); ++count) {
        ins = tmp + len_rep;
    }
 
    tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
 
    if (!result)
        return NULL;
 
    // first time through the loop, all the variable are set correctly
    // from here on,
    //    tmp points to the end of the result string
    //    ins points to the next occurrence of rep in orig
    //    orig points to the remainder of orig after "end of rep"
    while (count--) {
        ins = strstr(orig, rep);
        len_front = ins - orig;
        tmp = strncpy(tmp, orig, len_front) + len_front;
        tmp = strcpy(tmp, with) + len_with;
        orig += len_front + len_rep; // move to next "end of rep"
    }
    strcpy(tmp, orig);
    return result;
}
 
double get_temperature(const char *w1_sensor_name)
{
    FILE * fp;
    char w1_sensor_file_name[1000];
    sprintf(w1_sensor_file_name, "/sys/bus/w1/devices/%s/w1_slave",w1_sensor_name);
    fp = fopen(w1_sensor_file_name, "r");
 
    char crc_value_buf[100];
    double temperature;
    fscanf(fp,"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %s",crc_value_buf);
    fscanf(fp,"%*s %*s %*s %*s %*s %*s %*s %*s %*s %*c %*c %lf",&temperature);
    fclose(fp);
    temperature = temperature / 1000;
 
    //printf("%s\n", crc_value_buf);
    //printf("%f\n", temperature);
 
    return temperature;
}
 
 
int main(int argc, char *argv[])
{
  //if (gpioInitialise() < 0) return -1;
 
  MYSQL *con = mysql_init(NULL);
 
  if (con == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      exit(1);
  }
 
  if (mysql_real_connect(con, "algo.lt", "algo_aistis", "aistis445445", 
          "algo_aistis", 0, NULL, 0) == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }  
 
  /*if (mysql_query(con, "insert into temperature (ts, value, location) values (LOCALTIMESTAMP(), 10, 'HOME_NORTH');")) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }*/
 
 
    printf("record temperature readings from w1 sensor and recor to mysql table\n");
    //const char * w1_sensor_nr1 = "28-031688217aff";
 
    printf("%d\n", argc);
 
    int i;
    for (i=1;i<argc;i++)
    {
        char  *w1_sensor_nr1 = argv[i];
 
        double t1 = get_temperature(w1_sensor_nr1);
        printf("%f\n",t1);
 
        char sql_buf[2000];
        char * w1_sensor_nr2 = str_replace(w1_sensor_nr1,"-","");
            printf("%s\n",w1_sensor_nr2);
 
        sprintf(sql_buf, "insert into temperature (ts, value, sensor_id) values (LOCALTIMESTAMP(), %lf, 0x%s);",t1,w1_sensor_nr2);
        printf("%s\n",sql_buf);
        free(w1_sensor_nr2);
 
        if (mysql_query(con, sql_buf)) 
        {
            fprintf(stderr, "%s\n", mysql_error(con));
            mysql_close(con);
            exit(1);
        }
    }
 
  mysql_close(con);
 
  return 0;   
}
raspberrypi/temp.1491773789.txt.gz · Last modified: by aistis