- HOW TO INSTALL SQLITE DATABASE ON A RASPBERRYPI HOW TO
- HOW TO INSTALL SQLITE DATABASE ON A RASPBERRYPI CODE
Once again, I set up the scheduler to cause things to happen. Now that I know how, I'll have to think about doing the same thing when I adapt this to talk to my little XBee devices. While I was working on it, I decided to get a little bit fancy and actually put the location and IP addresses of the two thermostats in the data base, retrieving and using them in the communication process. This works really well to interrogate each thermostat and put the results into a data base. # I want to look into using atexit for this # all the real work is done by the scheduler # This is a priming read to show immediate results Sched.add_interval_job(ThermostatStatus, minutes=1) # schedule reading the thermostats for every minute # being called, because they take care of themselves # that way I don't have to worry about them Print whichOne + " reports: " + str(status)Ĭ.execute("update thermostats set 'temp-reading' = ?, " # open the database and set up the cursor (I don't have aĭbconn = nnect('/home/pi/database/desert-home')Ĭ.execute("select address from thermostats " # so I have to open and close the database within # The scheduler will run this as a separate thread # and separate the values into a list that can # the arduino thermostat, strip off the trailing cr,lf # After getting the status from the little web server on # now read the status that came back from it Website = openSite("HTTP://" + whichOne + "/status") Print "Cannot retrieve URL: Unknown error" Print "Cannot retrieve URL: " + str(e.code) + ": " + errorDesc
HOW TO INSTALL SQLITE DATABASE ON A RASPBERRYPI CODE
As usual, here's the code I came up with:įrom apscheduler.scheduler import SchedulerĮrrorDesc = There's not a huge set up, actually, there wasn't much set up at all.
HOW TO INSTALL SQLITE DATABASE ON A RASPBERRYPI HOW TO
Once again, there was a ton of information on the web about how to use Sqlite3, and things went pretty smoothly. The idea is that I can put up a web page that shows the status of the two thermostats and eventually add controls to change the settings. I chose to monitor my thermostats, since they are ethernet enabled and respond directly to requests, then to store their current state in the data base. So, after installing Sqlite3 on my Pi, I spent about two hours cobbling together enough code to try it out. Since the interface language is SQL, whatever I wind up doing can be ported to a larger data base tool when I need to. It seems like a reasonable choice for a really small system since it doesn't require a manager process and isn't split across multiple controllers. After reading way too much information on various data base managers, I decided to test Sqlite3.
I've been setting up a new Raspberry Pi to control my house and decided to experiment with a data base to store current data in so multiple processes can get at it. Mqtt_client.If you got here through a search engine looking for a tutorial on Sqlite3 on the Raspberry Pi, that's not what this is.
Mqtt_ername_pw_set(MQTT_USER, MQTT_PASSWORD) Mqtt_client = mqtt.Client(MQTT_CLIENT_ID) Sql = 'INSERT INTO sensors_data (topic, payload, created_at) VALUES (?, ?, ?)'Ĭursor.execute(sql, (message.topic, payload, int(time())))ĬREATE TABLE IF NOT EXISTS sensors_data ( MQTT_PASSWORD = 'YOUR MQTT USER PASSWORD'ĭef on_connect(mqtt_client, user_data, flags, conn_result):ĭef on_message(mqtt_client, user_data, message): We connect to the MQTT broker and starting a loop that invokes callback functions, handles reconnecting, etc. The user_data_set method is used to set the custom data that will be passed to callback functions. In the main function a connection is established to the SQLite database and a new table is created if it does not already exist. A message topic, data and Unix timestamp is saved to the database table. The on_message function is called when client receives message from the MQTT broker. Once the client has successfully connected, we subscribe to the message topic. The on_connect function is called when connection is established between a client and MQTT broker. SQLite database will be stored in the mqtt.db file. For example, home/temperature, home/humidity, etc. It means that we will receive all messages of a topic that begins with the home pattern.
We use the multi-level wildcard # in the topic. Each MQTT client should have unique ID which is used by an MQTT broker for identification. We define variables to store the IP address or hostname of MQTT broker, the network port, the username and the corresponding password.