I cannot save data sent from Arduino Uno to Raspberry Pi.

Thread Starter

duyguc

Joined Oct 19, 2021
3
I am trying to send sensor reading from Arduino Uno to raspberry pi and I want to save this incoming sensor reading to SQLite which I downloaded to Raspberry pi. I used lm35 sensor on Arduino Uno and i used the i2c way for communication between Arduino Raspberry pi. I managed to transmit the data from the sensor to the arduino uno and from there to the Raspberry Pi. Then I showed this data in the interface I created with the guizero library on Raspberry Pi. With a button I added, I enabled the temperature data to be updated when the button is pressed. But my shortcoming here was missing the data in between and not being able to see all the data. For this, I decided to download SQLite and save the data here, but no matter how much I searched, I could not be successful. I tried many sample codes but could not adapt any of them against this application. I leave the code used below. I know that I make mistakes from time to time, but no matter how hard I searched, I couldn't find the right one. Is the SQLite I'm using a wrong choice? Any chance of what I'm doing getting fixed and still working? I don't know how to write the code in the marked field, I can't find it either. I'm pretty new to this stuff. Please help, thanks. (note:The marked place (!!!???) is wrong, but I can't find the correct one.)

Python:
from guizero import App, Text, PushButton

import smbus
import smbus as SMBus
import sqlite3
import time
import datetime

bus=smbus.SMBus(1)

address= 0x2a

app=App(title="Arduino'dan veri alma")
app.bg="white"
app.fg="black"


#dattabse bağla
conn=sqlite3.connect('deneme1.db')
c=conn.cursor()

#tablo oluşturma
def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS tablo(unix REAL, tarih REAL, sicaklik REAL)')

#verilerin tabloya yazılması
def dynamic_data_entry():
    unix=int(time.time())
    tarih=str(datetime.datetime.fromtimestamp(unix).strftime('%Y,-%m-%d %H:%M:%S'))
    
    sicaklik=address.read_retry(3,5) (!!!???)
    
    c.execute("INSERT INFO tablo (unix, tarih, nem, sicaklik) VALUES (?, ?, ?, ?)",
              (unix, tarih, nem, sicaklik))
    conn.commit()
data = bus.read_byte(address)

def gorev1(n):
    for i in range(0,int(n)):
        dynamic_data_entry()
        time.sleep(1)
        
create_table()
x=input('kaç saniyelik veri kaydedilsin?')
y=input('kaç sn öncesini görmek istersin?')
gorev1(x)

c.close()
conn.close()
 
I don't know Python and have never worked with SQLite before, but decided to take a look.

I did notice one difference from https://docs.python.org/3/library/sqlite3.html

Code:
cur.execute('''CREATE TABLE stocks(date text, trans text, symbol text, qty real, price real)''')
and yours...

Code:
c.execute('CREATE TABLE IF NOT EXISTS tablo(unix REAL, tarih REAL, sicaklik REAL)')
They have more quotes around the CREATE TABLE command.

I also found https://www.programcreek.com/python/example/6844/sqlite3.Error which has examples of how to get error information.

Are you running the script in a shell or as part of something else? My initial thought was a permissions issue or it's not able to find the file.

Good luck
 

Thread Starter

duyguc

Joined Oct 19, 2021
3
I don't know Python and have never worked with SQLite before, but decided to take a look.

I did notice one difference from

Code:
cur.execute('''CREATE TABLE stocks(date text, trans text, symbol text, qty real, price real)''')
and yours...

Code:
c.execute('CREATE TABLE IF NOT EXISTS tablo(unix REAL, tarih REAL, sicaklik REAL)')
They have more quotes around the CREATE TABLE command.

I also found https://www.programcreek.com/python/example/6844/sqlite3.Error which has examples of how to get error information.

Are you running the script in a shell or as part of something else? My initial thought was a permissions issue or it's not able to find the file.

Good luck
Thank you for your help. I ran the commands through the Thony Python IDE.
I will review the pages you mentioned and make changes and let you know the result.
 

RayB

Joined Apr 3, 2011
17
I do this often but I take instrumentation data from Mega for aircraft engine, send it over serial as a faux JSON, and use Node-RED to parse, manipulate, display the engine performance data.

C++:
/* Faux JSON file format https://groups.google.com/forum/#!topic/node-red/pvY36rlRUdI */

void analog_13(void) {                                   /* Gasoline fuel remaining (gals) */
  float temp  = float(fuelRemain() );
  Serial.print("{\"Gas_Lvl\":"); Serial.print(temp); Serial.print(",");
  return;
}

void analog_1(void) {                                   /* Oil temperature */
  float temp  = float(EngOilTemp() );
  Serial.print ("\"Oil_Tmp\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void analog_3(void) {                                   /* CylHead Front */
  float temp  = float (CylHeadFront());
  Serial.print("\"CyHd_Fnt\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void analog_0(void) {
  Serial.print("\"H2O_Tmp\":");    Serial.print(EngineCoolantTemp());  Serial.print (",");
}

void analog_15(void) {                                   /* Gasoline fuel pressure */
  float temp = fuelPressure();                                        /* fuel pressure is a fp math function */
  Serial.print("\"Gas_Prs\":"); Serial.print(temp);  Serial.print(",");
  return;
}


void analog_2(void) {                                   /* Engine Oil Pressure */
  float temp = float (EngineOilPres());
  Serial.print("\"Oil_Prs\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void analog_4(void) {                                   /* Cylinder Head rear temperature */
  float temp = float (CylHeadRear());
  Serial.print("\"CyHd_Rear\":"); Serial.print(temp);  Serial.print(",");
  Serial.print("\"CHT2_Tmp\":");  Serial.print(temp);  Serial.print(",");
  return;
}

void analog_12(void) {                                   /* Battery voltage measured */
  float temp = float (BatteryVoltage());
  Serial.print("\"Voltage\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void analog_10(void) {                                  /* Amps measured */
  float temp = float (BatteryAmps());
  Serial.print("\"Current\":"); Serial.print(temp);  Serial.println("}");
  return;
}

void thermo_0(void) {                                  /* Exhaust Gas left */
  float temp  = float (thermocouple_channel( 0));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

//    ***** NOTE:  YOU MUST UNCOMMENT IN THE MAIN TAB THE CALLING ROUTINE AND UNCOMMENT+EDIT TO PROVIDE NODERED WITH A NEW LABEL *****
/*
void thermo_1(void) {
  float temp  = float (thermocouple_channel( 1));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_2(void) {
  float temp  = float (thermocouple_channel( 2));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_3(void) {
  float temp  = float (thermocouple_channel( 3));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_4(void) {
  float temp  = float (thermocouple_channel( 4));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_5(void) {
  float temp  = float (thermocouple_channel( 5));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_6(void) {
  float temp  = float (thermocouple_channel( 6));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_7(void) {
  float temp  = float (thermocouple_channel( 7));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_8(void) {
  float temp  = float (thermocouple_channel( 8));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_9(void) {
  float temp  = float (thermocouple_channel( 9));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_10(void) {
  float temp  = float (thermocouple_channel(10));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_11(void) {
  float temp  = float (thermocouple_channel(11));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_12(void) {
  float temp  = float (thermocouple_channel(12));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_13(void) {
  float temp  = float (thermocouple_channel(13));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_14(void) {
  float temp  = float (thermocouple_channel(14));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

void thermo_15(void) {
  float temp  = float (thermocouple_channel(15));
  Serial.print("\"EGT_Lft\":"); Serial.print(temp);  Serial.print(",");
  return;
}

*/
The magic can be found from the forum link in the comment, line #1

Node-RED is a fantastic package on Raspberry Pi.


Ray
 

RayB

Joined Apr 3, 2011
17
Sample serial output from Mega to rPi and Node-RED

Code:
{"H2O_Tmp":0,"Oil_Tmp":302.00,"Oil_Prs":0.00,"CyHd_Fnt":302.00,"CyHd_Rear":302.00,"Current":-62.00,"Voltage":0.00,"Gas_Lvl":7.00,"Gas_Prs":0.00,"Ktemp_1":0.00,"Ktemp_2":0.00,"Ktemp_3":0.00,"Ktemp_4":0.00,"Ktemp_5":0.00,"Ktemp_6":0.00,"Ktemp_7":0.00,"Ktemp_8":0.00,"Ktemp_9":0.00,"Ktemp_10":0.00,"Ktemp_11":0.00,"Ktemp_12":0.00,"Ktemp_13":0.00}
Note beginning and ending braces, quoted labels, and comma delimiters.
 

Thread Starter

duyguc

Joined Oct 19, 2021
3
Mega'dan rPi'ye ve Node-RED'e örnek seri çıktı

[KOD]{"H2O_Tmp":0,"Oil_Tmp":302.00,"Oil_Prs":0.00,"CyHd_Fnt":302.00,"CyHd_Rear":302.00,"Akım":-62.00,"Voltaj":0.00,"Gas_Lvl" :7.00,"Gas_Prs":0.00,"Ktemp_1":0.00,"Ktemp_2":0.00,"Ktemp_3":0.00,"Ktemp_4":0.00,"Ktemp_5":0.00,"Ktemp_6":0.00,"Ktemp_7":0.00 ,"Ktemp_8":0,00,"Ktemp_9":0,00,"Ktemp_10":0,00,"Ktemp_11":0,00,"Ktemp_12":0,00,"Ktemp_13":0,00}
[/KOD]

Başlangıç ve bitiş ayraçlarını, alıntılanan etiketleri ve virgül sınırlayıcıları not edin.
[/ALINTI]
Thanks, i will take note :
 
Top