raspberrypi:temp
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| raspberrypi:temp [2017/03/11 17:01] – aistis | raspberrypi:temp [2020/03/20 18:29] (current) – aistis | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| {{: | {{: | ||
| + | |||
| + | {{: | ||
| panaudojus sita gida pavyko gauti temperaturas ir jas irasyti i faila: | panaudojus sita gida pavyko gauti temperaturas ir jas irasyti i faila: | ||
| * http:// | * http:// | ||
| * https:// | * https:// | ||
| + | |||
| + | Čia iš komandinės eilutės parašius komandas galima pamatyti temperatūrą: | ||
| + | ls / | ||
| + | {{: | ||
| + | |||
| + | Čia 28-0316881dadff yra termometro/ | ||
| + | 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: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | reikia paleisti kad kas minute rasitu i faila: | ||
| + | crontab -e | ||
| + | | ||
| + | ideti tokia eilute: | ||
| + | < | ||
| + | * * * * * ./ | ||
| + | </ | ||
| + | | ||
| + | | ||
| Paleidau 2 termometrus, | Paleidau 2 termometrus, | ||
| Line 93: | Line 112: | ||
| {{: | {{: | ||
| + | |||
| + | |||
| + | |||
| + | Žemiau MYSQL irasymas į duomenų bazę temperatūros: | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #include < | ||
| + | |||
| + | //#include < | ||
| + | |||
| + | //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 | ||
| + | |||
| + | /* | ||
| + | sudo apt-get install libmysqlclient-dev -y | ||
| + | */ | ||
| + | |||
| + | /* | ||
| + | gcc -Wall -pthread -o temp_mysql temp_mysql.c `mysql_config --libs` | ||
| + | sudo ./ | ||
| + | */ | ||
| + | |||
| + | /* | ||
| + | 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; | ||
| + | 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; | ||
| + | |||
| + | // 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, | ||
| + | len_front = ins - orig; | ||
| + | tmp = strncpy(tmp, | ||
| + | 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, | ||
| + | fp = fopen(w1_sensor_file_name, | ||
| + | |||
| + | char crc_value_buf[100]; | ||
| + | double temperature; | ||
| + | fscanf(fp," | ||
| + | fscanf(fp," | ||
| + | fclose(fp); | ||
| + | temperature = temperature / 1000; | ||
| + | |||
| + | // | ||
| + | // | ||
| + | |||
| + | return temperature; | ||
| + | } | ||
| + | |||
| + | |||
| + | int main(int argc, char *argv[]) | ||
| + | { | ||
| + | //if (gpioInitialise() < 0) return -1; | ||
| + | |||
| + | MYSQL *con = mysql_init(NULL); | ||
| + | |||
| + | if (con == NULL) | ||
| + | { | ||
| + | fprintf(stderr, | ||
| + | exit(1); | ||
| + | } | ||
| + | |||
| + | if (mysql_real_connect(con, | ||
| + | " | ||
| + | { | ||
| + | fprintf(stderr, | ||
| + | mysql_close(con); | ||
| + | exit(1); | ||
| + | } | ||
| + | |||
| + | /*if (mysql_query(con, | ||
| + | { | ||
| + | fprintf(stderr, | ||
| + | mysql_close(con); | ||
| + | exit(1); | ||
| + | }*/ | ||
| + | |||
| + | |||
| + | printf(" | ||
| + | //const char * w1_sensor_nr1 = " | ||
| + | |||
| + | printf(" | ||
| + | | ||
| + | int i; | ||
| + | for (i=1; | ||
| + | { | ||
| + | char *w1_sensor_nr1 = argv[i]; | ||
| + | |||
| + | double t1 = get_temperature(w1_sensor_nr1); | ||
| + | printf(" | ||
| + | |||
| + | char sql_buf[2000]; | ||
| + | char * w1_sensor_nr2 = str_replace(w1_sensor_nr1," | ||
| + | printf(" | ||
| + | | ||
| + | sprintf(sql_buf, | ||
| + | printf(" | ||
| + | free(w1_sensor_nr2); | ||
| + | |||
| + | if (mysql_query(con, | ||
| + | { | ||
| + | fprintf(stderr, | ||
| + | mysql_close(con); | ||
| + | exit(1); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | mysql_close(con); | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
raspberrypi/temp.1489244460.txt.gz · Last modified: by aistis
