Page 1 of 1

DHT11 from file.log to MySql Databaese

Posted: Fri May 16, 2014 5:51 pm
by adnenyak
Hi all,
i Have an issue to write from a log file to a data base
look i have a C program Read values from DHT11 sensor

Code: Select all

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAX_TIME 85
#define DHT11PIN 7
#define ATTEMPTS 5
int dht11_val[5]={0,0,0,0,0};
 
int dht11_read_val()
{
  uint8_t lststate=HIGH;
  uint8_t counter=0;
  uint8_t j=0,i;
  for(i=0;i<5;i++)
     dht11_val[i]=0;
  pinMode(DHT11PIN,OUTPUT);
  digitalWrite(DHT11PIN,LOW);
  delay(18);
  digitalWrite(DHT11PIN,HIGH);
  delayMicroseconds(40);
  pinMode(DHT11PIN,INPUT);
  for(i=0;i<MAX_TIME;i++)
  {
    counter=0;
    while(digitalRead(DHT11PIN)==lststate){
      counter++;
      delayMicroseconds(1);
      if(counter==255)
        break;
    }
    lststate=digitalRead(DHT11PIN);
    if(counter==255)
       break;
    // top 3 transistions are ignored
    if((i>=4)&&(i%2==0)){
      dht11_val[j/8]<<=1;
      if(counter>16)
        dht11_val[j/8]|=1;
      j++;
    }
  }
  // verify checksum and print the verified data
  if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))
  {
    printf("%d.%d\n",dht11_val[0],dht11_val[1]);
    return 1;
  }
  else
    return 0;
}
 
int main(void)
{
  int attempts=ATTEMPTS;
  if(wiringPiSetup()==-1)
    exit(1);
  while(attempts)
  {
    int success = dht11_read_val();
    if (success) {
      break;
    }
    attempts--;
    delay(500);
  }
  return 0;
}
and with on other Script

Code: Select all

* * * * * echo `/home/pi/wiringPi/dht11` >> /home/pi/temp.log
this script product one temp.log file contain the values of DHT11 every 60 secondes like this

Code: Select all

36.0
36.0
36.0
the problem i want to read the temp.log with a python code or PHP code and insert it into a table (mysql data base)
i try to do it but i have some problem
my PHP code

Code: Select all

<?php

mysql_connect('localhost', 'user', 'pass') or die(mysql_error()); 
mysql_select_db('database') or die(mysql_error()); 

$lines = file('temp.log'); 
$humidity = ""; 

foreach($lines as $line => $hum) { 
    $humidity .= "'$hum',"; 
} 

mysql_query("INSERT_INTO `humidite` VALUES (NULL , $humidity 'test_record')") or die(mysql_error());  
?>
the humidite TABLE have 2 variable on AI 'id' and humidity_Values, when i try my code i have the error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT_INTO `humidite` VALUES (NULL , '36.0 ','36.0 ','36.0 ','36.0 ','36.0 ','3' at line 1
if any one have a python solution thanks :)

Re: DHT11 from file.log to MySql Databaese

Posted: Fri May 16, 2014 6:20 pm
by adnenyak
I transform a log file (temp.log) to (temp.txt) can get the same result with python??

Re: DHT11 from file.log to MySql Databaese

Posted: Fri May 16, 2014 8:21 pm
by adnenyak
this problem is resolved, with a Python Code
with open("temp1.txt") as f:
lines=f.readlines()
print lines[-1]
this print the last values of the temp.txt and rest to put it into database :)
:D :D :D :D :D :D :D