Almost done. I popped in a while ago to ask about writing 2 values to a sql database and was helped immensely, so I am hoping to get one more bit of help.
I am currently setting up remote sensor units that will pull the data, readings from a dht11 and 5 vernier sensor probes.
The code sets up variables that hold the slope and intercepts for linear calibrations, gets the reading from the analog port and calculates the calibrated measurement.
The code then compiles it (to send to SD for backup(not implemented yet...)) and sends the individual values to the linux side of the yun.
Here is the code.
#include <FileIO.h>
#include <DHT.h>
#include <Process.h>
#include <Console.h>
#include <math.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int sensor1Port = A0; //Thermometer
int sensor2Port = A1;
int sensor3Port = A2;
int sensor4Port = A3;
int sensor5Port = A4;
int sensor1Value = 0;
int sensor2Value = 0;
int sensor3Value = 0;
int sensor4Value = 0;
int sensor5Value = 0;
void setup() {
Bridge.begin(); // Initialize Bridge
FileSystem.begin(); //Initialize FileSystem
Console.begin();
Console.println("You're connected to the Console!!!!");
Serial.begin(115200);
// initialize digital pin 13 as an output (light to show start of process).
pinMode(13, OUTPUT);
}
void loop() {
// Sensor Slope Intercepts
int slopeSensor1 = 5;
int interceptSensor1 = 1;
int slopeSensor2 = 5;
int interceptSensor2 = 1;
int slopeSensor3 = 5;
int interceptSensor3 = 1;
int slopeSensor4 = 5;
int interceptSensor4 = 1;
int slopeSensor5 = 5;
int interceptSensor5 = 1;
//Get the date and time
String dataString;
dataString += getTimeStamp();
String datestring = dataString;
Console.println(dataString);
//DHT11 readings
int h = dht.readHumidity();
int t = dht.readTemperature();
//Light to show start of process
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) to show start
//Start Calculations
int data; //reading from the A/D converter (10-bit)
float Temp; //the print below does the division first to avoid overflows
data=analogRead(sensor1Port); // read count from the A/D converter
sensor1Value=Thermistor(data); // and convert it to CelsiusSerial.print(Time/1000); //display in seconds, not milliseconds
//Passing raw values to calculating process
data = analogRead(sensor2Port);
sensor2Value = sensorValueCalculate(slopeSensor2, interceptSensor2, data);
data = analogRead(sensor3Port);
sensor2Value = sensorValueCalculate(slopeSensor3, interceptSensor3, data);
data = analogRead(sensor4Port);
sensor2Value = sensorValueCalculate(slopeSensor4, interceptSensor4, data);
data = analogRead(sensor5Port);
sensor2Value = sensorValueCalculate(slopeSensor5, interceptSensor5, data);
//Create Datastring to show in console and then write to CSV
dataString += ("," + String(t) + "," + String(h) + "," + String(sensor1Value) + "," + String(sensor2Value) + "," + String(sensor3Value) + "," + String(sensor4Value) + "," + String(sensor5Value));
Console.println(dataString);
Console.println("Sending data to SQL database");
// Send data to MySQL database
Process p;
p.begin("/mnt/sda1/db.php");
p.addParameter(String(datestring));
p.addParameter(String(t));
p.addParameter(String(h));
p.addParameter(String(sensor1Value));
p.addParameter(String(sensor2Value));
p.addParameter(String(sensor3Value));
p.addParameter(String(sensor4Value));
p.addParameter(String(sensor5Value));
p.run();
Console.println("Finished sending data to SQL database");
digitalWrite(13, LOW); // turn the LED on (HIGH is the voltage level) to show start
delay(500); //Wait for one hour to run again
}
// This function return a string with the time stamp
String getTimeStamp() {
String result;
Process time;
// date is a command line utility to get the date and the time
// in different formats depending on the additional parameter
time.begin("date");
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
// T for the time hh:mm:ss
time.run(); // run the command
// read the output of the command
while(time.available()>0) {
char c = time.read();
if(c != '\n')
result += c;
}
return result;
}
//This function will calculate the calibrated Sensor Value
int sensorValueCalculate(int slope, int intercept, int RawData) {
Console.println("Calculating");
int CalibratedReading = intercept + RawData * slope;
Console.println(CalibratedReading);
return CalibratedReading;
}
float Thermistor(int Raw) //This function calculates temperature from ADC count
{
long Resistance;
float Resistor = 15000; //fixed resistor
float Temp; // Dual-Purpose variable to save space.
Resistance=( Resistor*Raw /(1024-Raw));
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later
Temp = 1 / (0.00102119 + (0.000222468 * Temp) + (0.000000133342 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celsius
return Temp; // Return the Temperature
}
This is the db.php file that runs.
#!/usr/bin/php-cli
<?php
$datetimestamp = $argv[1];
$temperature = $argv[2];
$humidity = $argv[3];
$sensor1Value = $argv[4];
$sensor2Value = $argv[5];
$sensor3Value = $argv[6];
$sensor4Value = $argv[7];
$sensor5Value = $argv[8];
$DBServer = '192.168.0.99';
$DBUser = 'root';
$DBPass = 'password';
$DBName = 'sensors';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
// check connection
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
$sql="INSERT INTO sensor_data (datetimestamp, Ambienttemp, Ambienthumidity, Sensor1value, Sensor2value, Sensor3value, Sensor4value, Sensor5value) VALUES ($datetimestamp, $temperature, $humidity, $sensor1Value, $sensor2Value, $sensor3Value, $sensor4Value, $sensor5Value)";
if($conn->query($sql) === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
?>
as with last time, sshing into the yun and running this from the prompt puts data into the database.
/mnt/sda1/db.php 80 40 34 34 34 34 34 34
Running the arduino program above gives all the indications that it is passing the data but nothing ends up in the database.
I would imagine that like last time, I have some basic error that is killing it.
Thoughts?