Witam serdecznie.
Ostatnimi czasy zająłem się programowaniem Arduino i postanowiłem zrobić sobie namiastkę projektu, który nazwałem "wykrywanie ruchu w lesie". Czujniki ruchu, termometr oraz zegar czasu rzeczywistego połączone są z płytką - cały układ wysyła mi na port COM dane takiej oto postaci:
Dane te chcę zapisać do bazy danych poprzez program w środowisku Processing, napisany w JAVIE. Program pobiera dane z portu szeregowego.
Znalazłem kod, który trochę przerabiałem - osiągnąłem efekt taki, że zapisuje mi tylko pierwszą linijkę do bazy, bo sprawdza czy pobrany bajt jest znakiem końca linii.
Niestety nie wiem jak zmienić poniższy fragment tak, aby każda informacja z COMU lądowała w osobnej tabeli.
Bardzo proszę o konstruktywne opinie, pomoc i wskazówki.
// check if there is data waiting
if (port.available() > 0)
{
// read one byte
int inByte = port.read();
// PONIŻEJ KOD DO WSTAWIENIA GODZINY
// if byte is not newline just add it to the buffer
if (inByte != 10)
buffer = buffer + char(inByte);
//if byte is newline
else if (inByte == 10)
{
// newline reached, let's process the data
if (buffer.length() > 1)
{
// make sure there is enough data
// chop off the last character, it's a carriage return
// (a carriage return is the character at the end of a
// line of text)
buffer = buffer.substring(0,buffer.length() -1);
// turn the buffer from string into an integer number
godzina = buffer;
// clean the buffer for the next read cycle
buffer = "";
// We're likely falling behind in taking readings
// from Arduino. So let's clear the backlog of
// incoming sensor readings so the next reading is
// up-to-date.
port.clear();
}
}
// PONIŻEJ KOD DO WSTAWIENIA DATY
if (inByte != 10)
buffer = buffer + char(inByte);
else if (inByte == 10)
{
if (buffer.length() > 1)
{
buffer = buffer.substring(0,buffer.length() -1);
godzina = buffer;
buffer = "";
port.clear();
}
}
//ITD. -> MIEJSCOWOSC,TEMP...
//ALE NIE DZIAŁA
//Wypisywanie otrzymanych danych
System.out.println(godzina);
System.out.println(data);
//OBSŁUGA BAZY DANYCH
String user = "root";
String pass = "";
String database = "arduino";
// connect to database of server "localhost"
dbconnection = new MySQL( this, "localhost", database, user, pass );
if ( dbconnection.connect() )
{
// now send data to database
dbconnection.execute( "INSERT INTO `arduino`.`wykrycia` (`godzina`, `data`, `miejscowosc`) VALUES ('"+godzina+"','"+data+"','"+miejscowosc+"');");
dbconnection.execute( "INSERT INTO `arduino`.`informacje` (`temperatura`, `stan baterii`, `wykryto`) VALUES ('"+temp+"','"+bateria+"','"+wykryto+"');");
}
}