Quickstart
After establishing a connection to the device and setting up your development environment, you can try out the first examples. Below are some basic code snippets in Python and curl that demonstrate single requests. More elaborate examples can be found under Code Examples.
Note
All the examples below use the ip address 10.0.0.2, which is the default address of the device. In case you changed it during setup, please make sure to update the ip address accordingly in the code snippets.
This code snippet tells the device to measure a new spectrum and returns the averaged result of 5 scans. It also requests the wavenumbers associated with the spectrometer resolution.
import requests
response = requests.get("http://10.0.0.2:5555/api/spectrometer/spectra/averaged?n_scans=5")
print(f"Current averaged spectrum: {response.json()}")
response = requests.get("http://10.0.0.2:5555/api/spectrometer/wavenumbers")
print(f"Wavenumbers: {response.json()}")
This code snippet reads out the current fan speed of the microcontroller while also checking the response status code for error handling.
Note
Subsequent examples don't show the import statement for brevity.
response = requests.get("http://10.0.0.2:5555/api/microcontroller/fan/speed")
# NOTE: you can check the status code for error handling
if response.status_code == 200:
result = response.json()
print(f"Fan speed is {result}")
This code snippet changes the spectrometer resolution to 2 cm⁻¹. It is using using a PUT request. The methods can be different for other endpoints (e.g. POST or GET).
Note
You can also use the port 80 instead of 5555 for HTTP requests.
# NOTE: we are using a PUT request to change the resolution
response = requests.put("http://10.0.0.2:5555/api/spectrometer/resolution", json={"resolution": 200})
# NOTE: the response code is not always 200 for successful requests, in this case we check for any 2xx code
# for successful resolution updates the response code is 204 No Content
if 200 <= response.status_code < 300:
print("Update successful")
This code snippet tells the device to measure a new spectrum and returns the averaged result of 5 scans.
import asyncio
from asyncua import Client
url = "opc.tcp://10.0.0.2:4840/freeopcua/server/"
async def main():
async with Client(url=url) as client:
result = await client.nodes.objects.call_method(
"2:Monipa/2:Spectrometer/2:Spectra/2:GetSpectraAveraged", 5
)
print(f"Current averaged spectrum: {result}")
if __name__ == "__main__":
asyncio.run(main())
This code snippet reads out the current fan speed of the microcontroller in a loop. The subscription handler prints out the new value whenever it changes.
Note
Subsequent examples only show the main function for brevity.
class SubHandler:
async def datachange_notification(self, node, val, data):
print(f"Value changed to: {val}")
async def main():
async with Client(url=url) as client:
node = await client.nodes.objects.get_child("2:Monipa/2:Microcontroller/2:TemperatureCore")
handler = SubHandler()
# NOTE: we create a subscription with 500 ms sampling interval, the actual update rate might be lower
subscription = await client.create_subscription(500, handler)
await subscription.subscribe_data_change(node)
print("Now following variable changes. Press Ctrl+C to stop.")
while True:
await asyncio.sleep(1)
This code snippet changes the spectrometer resolution to 2.5 cm⁻¹ by calling the appropriate method on the OPC UA server.
async def main():
async with Client(url=url) as client:
method = await client.nodes.objects.get_child("2:Monipa/2:Spectrometer/2:SetResolution")
await method.call_method(method, 250)
print("Update successful")
For more elaborate examples on how to use asyncua in general, check the asyncua repository.
This code snippet tells the device to measure a new spectrum and returns the averaged result of 5 scans. It also requests the wavenumbers associated with the spectrometer resolution.
curl -X 'GET' \
'http://10.0.0.2:5555/api/spectrometer/spectra/averaged?n_scans=5' \
-H 'accept: application/json'
curl -X 'GET' \
'http://10.0.0.2:5555/api/spectrometer/wavenumbers' \
-H 'accept: application/json'
This code snippet reads out the current fan speed of the microcontroller.
curl -X 'GET' \
'http://10.0.0.2:5555/api/microcontroller/fan/speed' \
-H 'accept: application/json'
This code snippet changes the spectrometer resolution to 3.5 cm⁻¹ while using a different HTTP method and sending data.