Connecting to an Electronic Research Notebook

Contents

8. Connecting to an Electronic Research Notebook#

In the following we will show you how to connect and save data to an Electronic Research Notebook(ERN) using its API

Requirements

  1. A device capable to connecting to the internet we have used a Pico W for this example

  2. Access to an ERN, we used RSpace an open source ERN

The first part of the tutorial is the same as how to connect over http

We need to edit our settings.toml file in the CIRCUITPY drive to add the following variables

Fill in the URL you wish to connect to, including the https:// RSPACE_URL = “”

FIll in your RSpace API_key RSPACE_APIKEY = “”

API end point

for the RSPACE_URL you should take the URL to your Rspace instance and add /api/v1 at the end to give the correct end point

Warning

Remember this information is stored in plain text and so can be read by anyone with access to the Pico

Code set-up#

Now we can program our Pico to send data over WiFi to our ERN.

Below is an example for sending temperature and time data to an ERN.


import os
import adafruit_connection_manager
import wifi
import adafruit_requests
import microcontroller
import time

#Retrieve variables from settings.toml
ssid = os.getenv("WIFI_SSID")
password = os.getenv("WIFI_PASSWORD")

# Initalize Wifi, Socket Pool, Request Session
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)

# This code will scan for available Wifi networks
# Can comment out if you know what you wish to connect to
for network in wifi.radio.start_scanning_networks():
    print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
            network.rssi, network.channel))
wifi.radio.stop_scanning_networks()

# Connect to the Wi-Fi network
print(f"\nConnecting to {ssid}...")
try:
    wifi.radio.connect(ssid, password)
except OSError as e:
    print(f"❌ OSError: {e}")
print("✅ Wifi!\n")

temp = microcontroller.cpu.temperature
timetrial = time.monotonic()
json_data = {  "name": "hackathon iot cpu_temp " + str(time.time()),
    "fields": [ { "content": "cpu_temp: " + str(temp) + ", time: " + str(timetrial) } ] }
print(f" | ✅ Sending JSON ('key':'value'): {json_data}")

ern_url = os.getenv("RSPACE_URL") + "/documents"
with requests.post(ern_url, json = json_data, headers = {"apiKey" : os.getenv("RSPACE_APIKEY")}) as response:
        json_resp = response.json()
        print(f" | ✅ '{json_resp}' ")
        print("-" * 80 + "\n")