User Tools

Site Tools


raspberrypi:temp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
raspberrypi:temp [2017/03/11 17:01] – aistisraspberrypi:temp [2020/03/20 18:29] (current) – aistis
Line 2: Line 2:
  
 {{:raspberrypi:pasted:20170225-112158.png}} {{:raspberrypi:pasted:20170225-112158.png}}
 +
 +{{:raspberrypi:pasted:20200320-182905.png}}
  
 panaudojus sita gida pavyko gauti temperaturas ir jas irasyti i faila: panaudojus sita gida pavyko gauti temperaturas ir jas irasyti i faila:
   * http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/   * http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/
   * https://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi   * https://www.modmypi.com/blog/ds18b20-one-wire-digital-temperature-sensor-and-the-raspberry-pi
 +
 +Čia iš komandinės eilutės parašius komandas galima pamatyti temperatūrą:
 +  ls /sys/bus/w1/devices/
 +{{:raspberrypi:pasted:20170311-170343.png}}
 +
 +Č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ų tada parasiau nedidele c programa kuri raso i loga temperaturas ir ją paleidau kas 10 sekundžių
Line 58: Line 67:
 } }
 </code> </code>
 +
 +reikia paleisti kad kas minute rasitu i faila:
 +  crontab -e
 +  
 +ideti tokia eilute:
 +<code>
 +* * * * * ./w1_temp_rec
 +</code>
 +  
 +  
  
 Paleidau 2 termometrus, viesnas matuoja temperatūrą namuose kitas lauke. Tada nubraižiau grafikus su matlab ir gavau toki vaizdą: Paleidau 2 termometrus, viesnas matuoja temperatūrą namuose kitas lauke. Tada nubraižiau grafikus su matlab ir gavau toki vaizdą:
Line 93: Line 112:
  
 {{:raspberrypi:pasted:20170225-120700.png}} {{:raspberrypi:pasted:20170225-120700.png}}
 +
 +
 +
 +Žemiau MYSQL irasymas į duomenų bazę temperatūros:
 +
 +<code c>
 +#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;   
 +}
 +
 +
 +</code>
raspberrypi/temp.1489244460.txt.gz · Last modified: by aistis