Buon giorno a tutti , premesso che è da un po che leggo questo utilissimo forum è la prima volta che scrivo.
Il mio progetto finale è un sistema Domotico fondamentalmente composto da quattro parti principali: Arduino per interfacciarsi con i vari I\O , Un programma in Processing che gestisce le comunicazioni verso e da un DB My SQL, una pagina in php che gestisce tutte le informazioni.
Al momento ho collegato un sensore di temperatura DS1820 (1Wire)
e tramite processing riesco a scrivere sul DB la temperatura e l'orario del campionamento
Code Processing:
import de.bezier.data.sql.*; // mysql database library
import processing.serial.*; // serial library
// initialize values
int inByte;
int intensity;
int s = second();
int m = minute();
int h = hour();
float temperatura;
MySQL msql;
Serial arduinoPort;
void setup(){
background(0);
size(100, 100);
println(Serial.list());
// serial settings
arduinoPort = new Serial(this, Serial.list()[1], 9600);
println(arduinoPort);
// MySQL database settings
String user = "root";
String pass = "bamiba";
String database = "casa"; msql = new MySQL(this, "localhost:3307", database, user, pass);
println( "connetto a DB ");
}
void draw(){
// read value from serial port
byte[] inBuffer = new byte[1];
if (arduinoPort.available() > 0) {
inBuffer = arduinoPort.readBytes();
arduinoPort.readBytes(inBuffer);
if (inBuffer != null) {
String temperatura = new String(inBuffer);
// write value to database
if(msql.connect()){
// stabilisce ora e data della campionatura
int s = second();
int m = minute();
int h = hour();
int[] orav = new int[3];
orav[0] = h;
orav[1] = m;
orav[2] = s;
int da = day();
int mt = month();
int ye = year();
int[] datav = new int[3];
datav[0] = da;
datav[1] = mt;
datav[2] = ye;
String ora = join(nf(orav, 0), ":");
String data = join(nf(datav, 0), "/");
print(ora);
print(" ");
print(data);
print(" Temperatura: ");
print(temperatura);
//scrive su DB temperatura e Ora
msql.execute( "INSERT INTO temperatura (Temp,Ora,Data) VALUE ('"+temperatura+"','"+ora+"','"+data+"') ");
}
}
}
}
arduino code
#include <OneWire.h>
//init the one wire interface on pin 10
OneWire ow(10);
//write here the address you receive from the other program
byte sensor[8] = {0x10, 0x27, 0x38, 0x83, 0x00, 0x08, 0x00, 0x55};
void setup(void) {
Serial.begin(9600);
}
void writeTimeToScratchpad(byte* address){
//reset the bus
ow.reset();
//select our sensor
ow.select(address);
//CONVERT T function call (44h) which puts the temperature into the
ow.write(0x44,1);
//sleep a second for the write to take place
delay(1000);
}
void readTimeFromScratchpad(byte* address, byte* data){
//reset the bus
ow.reset();
//select our sensor
ow.select(address);
//read the scratchpad (BEh)
ow.write(0xBE);
for (byte i=0;i<9;i++){
data[i] = ow.read();
}
}
float getTemperature(byte* address){
int tr;
byte data[12];
writeTimeToScratchpad(address);
readTimeFromScratchpad(address,data);
//put in temp all the 8 bits of LSB (least significant byte)
tr = data[0];
//check for negative temperature
if (data[1] > 0x80){
tr = !tr + 1; //two's complement adjustment
tr = tr * -1; //flip value negative.
}
//COUNT PER Celsius degree (10h)
int cpc = data[7];
//COUNT REMAIN (0Ch)
int cr = data[6];
//drop bit 0
tr = tr >> 1;
//calculate the temperature based on this formula :
//TEMPERATURE = TEMP READ - 0.25 + (COUNT PER Celsius Degree - COUNT REMAIN)
// (COUNT PER Celsius Degree)
return tr - (float)0.25 + (cpc - cr)/(float)cpc;
}
//fahrenheit to celsius conversion
float f2c(float val){
float aux = val - 32;
return (aux * 5 / 9);
}
//celsius to fahrenheit conversion
float c2f(float val){
float aux = (val * 9 / 5);
return (aux + 32);
}
// Scrive su seriale il valore di temp
void loop(void) {
float temperatura;
temperatura = getTemperature(sensor);
Serial.println(temperatura);
delay(500);
}
Come già detto sopra l'accrocchio funziona solo che a volte il programma in processig legge solo parte della temperatura
esempio 22,55.... legge solo ,55
qualcuno mi puo suggerire un metodo di trasmissione tra Arduino e Processing "più preciso" considerando che il prossimo step è Processing che legge una data tabella nel DB e trasferisce ad arduino un INT (che verrà associato ad un comando es. eccita relè ecc..)
grazie in anticipo
Lorenzo