Quick Start, Set up a LabRat and Logging Inator#
This is a guide to get startd logging with LabRat and a Inator logging device.
Requirements
A computer with Python installed
A microprocessor (we are using a Pico W which has built in Wifi)
A DHT11 temperature and humidity sensor (we used the version from Seeed Studios)
The first step is to set up the Inator to log data from. We will make a DHT_Inator in this example where we connect a DHT11 Temperature and Humidity Sensor to our Pico. But everything here can be applied to any sensor you would like to use.
Logger set up#
To set up the pico as a logger follow the instructions in the tutorial to install the CircuitPython firmware
The next step is to connect the DHT sensor.
Make the following connections:
DHT11 Pin |
Pico Pin |
|---|---|
GND |
GND |
VCC |
3.3V out |
NC |
Not connected |
SIG |
GPO1 |
Next we will set some test code on the Pico to connect to the DHT11.
A library is required, it is not included in the core CircuitPython package you uploaded to the Pico but can be very easily added using the instructions here.
import time
import board
import adafruit_dht
dht = adafruit_dht.DHT11(board.GP0)
while True:
try:
temp = dht.temperature
humidity = dht.humidity
print(f"Temperature: {temp} C \t Humidity: {humidity}%")
except RuntimeError as e:
print("Reading from DHT failure: ", e.args)
time.sleep(5)
This should print the temperature and humidity recorded by the DHT11 to your serial connection. For more info on how to connect the Pico to serial and upload code the instructions are here.
LabRat set up#
THe next step is to install the LabRat control and logging software
Go to the repo[inator-project/LabRat] and download the repo contents, either use Git or just download the zip.
The first step is to install the necessary Python libraries. Files for installation from pip or conda (or mamba) are in the installation folder
conda env create -f environment.yml
pip install -r /path/to/requirements.txt
If you now run
python labrat_log.py -h
You should get something like the following
Readying Logging at:2026-02-21T16:17:47+00:00
usage: labrat_log.py [-h] [-sql SQL] [-secrets SECRETS] [-setupsql] [--createdb] [--dev_file DEVFILE] [-file FILE]
[--setfile] [-ser_port SER_PORT] [-baud BAUD]
CONN
Purpose: Connect sensors and log to DB
positional arguments:
CONN Which connection will be monitored. Can be mqtt for a MQTT connectionserial for a wired Serial
Connection or flask for a Flaks based API
options:
-h, --help show this help message and exit
-sql SQL The SQL database path. Should be absolute
-secrets SECRETS The path to the toml fil with secrets, such as log ins are kept
-setupsql If added this flag will run the create Inator DB function.This will then require extra flags
such as the device info file location
--createdb If setup flag added this will create a new DB if the given db doesn't exist
--dev_file DEVFILE If the setup flag is added this will load the device informationThis is the path to the JSON
file that contains the deviceinformation to be loaded into the SQL
-file FILE If set this is the file path to a text file where data can be logged.Should be an absolute path
--setfile If set this will run a set up file function, for headers etc...
-ser_port SER_PORT The COM port to connect to for serial
-baud BAUD The Baud rate for the serial connection
Connecting it together#
Now we will combine the logging Pico with the LabRat programme to save data from a sensor into a sqlite database. There are several ways to make this connection. Choose which ever you prefer/need.
If we are connecting over serial we will use the following code on the pico
import os
import microcontroller
import time
import json
import board
import adafruit_dht
dht = adafruit_dht.DHT11(board.GP0)
#Retrieve variables from settings.toml
inatorname = os.getenv("INATORNAME")
accTime = os.getenv('ACQUIRETIME') #how often to acquire and transmit data
while True:
temp = dht.temperature
humidity = dht.humidity
#Create dictionary
json_dict = {"inator":inatorname,"temperature":temp,"humidity":humidity} # Send data with JSON syntax
json_data = json.dumps(json_dict)
print(f"{json_data}")
time.sleep(accTime)
Where we would have the following settings.toml file saved on the pico. We shall call the Inator teminator_1 and acquire for 30 seconds
INATORNAME = "teminator_1" # Name of the device, will be used when sending data (put name between the quote marks)
ACQUIRETIME = 30 # The value (in seconds) of how often to acquire data and log for
To add the inator to the database we need a device_info.json file
{
"devices": [
{
"device_name": "teminator_1",
"device_guid": "",
"num_sensors": 2,
"device_info": "Has DHT11 sensor",
"device_type": "pico",
"device_location": "TBD",
"device_active": 1,
"connection": "Serial",
"sensors": [
{
"sens_name": "temperature",
"measures": "Temperature",
"returns": "Celsius",
"calib": "1:1",
"range": "0-50",
"info": "Seeed DHT11 on GP0",
"comments": "None"
},
{
"sens_name": "humidity",
"measures": "Humidity",
"returns": "%",
"calib": "1:1",
"range": "0-100",
"info": "Seeed DHT11 on GP0",
"comments": "None"
}
]
}
]
}
The most important thing here is that the device_name and sens_name must match the names sent from the pico, otherwise the logging will fail It is also necessary to have A GUID set for the device. To generate this run
python -c "import uuid
print(uuid.uuid4())"
The next step is to find out which COM port the Pico is on.
On Windows go to the device manager and look under ports to see which COM port is connected
On linux
ls /dev/tty*
Will display a list of all tty devices. The pico will probably be either ttyAM0 or ttyAM1 if in doubt plug and unplug it. If you get permission errors run
sudo adduser <red-user> dialout
We can now (finally) set up the LabRat, run the following replacing
python labrat_log.py -sql ./datalog.sql -setupsql --createdb --dev_file ./device_info.json -ser_port <PORT> -baud 11520 serial
This should start the programme, create the sqlite database, add tables for the teminator_1 device and then start listening on the serial port
When you need to run it again the command can be simplifed to:
python labrat_log.py -sql ./datalog.sql -ser_port <PORT> -baud 11520 serial
as the database exists and has the correct tables
We are now set up for logging