Skip to main content
Reacgen Biosystems

Code libraries

The code libraries handle the sensor communication for you, so you can take measurements with simple function calls. Talk to the probe over a wired I²C bus or over WiFi with WebSocket.

Download

Arduino / ESP32 (C++)

Header-documented class TurbidVision built on the standard Wire library. Works on any 3.3 V Arduino-compatible board (ESP32, ESP8266, Arduino Nano, etc.). On a 5 V board use a level shifter.

Install

In the Arduino IDE: Sketch → Include Library → Add .ZIP Library… and select the downloaded turbidvision-arduino.zip. Or unzip the TurbidVision folder into your Arduino/libraries/ directory and restart the IDE. For PlatformIO, add the folder to your project's lib/ directory.

Usage

#include <Wire.h>
#include <TurbidVision.h>

TurbidVision sensor;                 // default address 0x69 on Wire

void setup() {
  Serial.begin(115200);
  if (!sensor.begin()) {             // Wire.begin() + 400 kHz + presence check
    Serial.println("Turbid Vision not found at 0x69.");
    while (true) { delay(1000); }
  }
  sensor.startJob();
  sensor.waitForStatusBit(TV_STATUS_HAS_INITIAL, 180000);
}

void loop() {
  TVLatestReading latest;
  if (sensor.readLatestReading(latest) && latest.sampleValid()) {
    Serial.println(latest.normalizedReflectance, 6);
  }
  delay(500);
}

sensor.begin() calls Wire.begin() and sets the bus to 400 kHz. For custom ESP32 pins, call Wire.begin(sda, scl) yourself and pass sensor.begin(false). A bundled BasicReadout sketch ships in the library's examples/ folder; the full scenario set is on the Arduino examples page, and every method and constant is in the Arduino API reference.

Python

Class TurbidVision built on smbus2, for any Linux I²C host (Raspberry Pi, etc.).

Install

unzip turbidvision-python.zip
cd turbidvision-python
pip install .            # installs 'turbidvision' + its smbus2 dependency

Make sure I²C is enabled on the host (sudo raspi-config on a Raspberry Pi) and that your user can access /dev/i2c-* (usually the i2c group).

Usage

from turbidvision import TurbidVision

with TurbidVision(bus=1) as sensor:          # /dev/i2c-1, address 0x69
    if not sensor.is_present():
        raise SystemExit("Sensor not found")

    sensor.start_job()
    sensor.wait_for_status(sensor.STATUS_HAS_INITIAL, timeout=180)

    print("Density:", sensor.calibrated_density(), "g/L")
    print(sensor.read_status())

    snap = sensor.read_all()                 # full Snapshot dataclass
    print(snap.calibrated_density, snap.growth_rate)

    latest = sensor.latest_reading()         # immediate cached sample + age
    fresh = sensor.trigger_reading()         # fresh observational capture
    print(latest.age_ms, fresh.normalized_reflectance,
          fresh.calibrated_value, fresh.calibration_unit)

    sensor.stop_job()
    sensor.deactivate_laser()

You can also pass an already-open smbus2.SMBus instance with TurbidVision(bus=my_smbus) (it will not be closed for you). Errors raise TurbidVisionError (a subclass of IOError) on transport failure. A runnable demo ships in the package's examples/quickstart.py; the full scenario walkthrough is on the Python examples page, and every method and constant is in the Python API reference.

WebSocket (Python)

Class TurbidVisionWS talks to the probe over its WiFi WebSocket interface (ws://<device-ip>/ws) instead of a wired bus, so it runs on any networked host: a laptop, a server, or a Raspberry Pi. A background thread polls the device and surfaces each new reading through a blocking stream_readings() generator. Unlike I²C, telemetry is pulled on an interval (default 2 s) rather than read register by register.

Security: the /ws endpoint has no authentication. Anyone who can reach the device IP can issue every command, including destructive ones. Only expose the sensor on a trusted network.

Install

unzip turbidvision-websocket.zip
cd websocket
pip install .            # installs 'turbidvision_ws' + its websocket-client dependency

Point the client at the probe's IP address or its mDNS hostname (turbidvision.localwhere supported). The IP is shown on the System page of the device's web dashboard.

Usage

from turbidvision_ws import TurbidVisionWS, STATUS_HAS_INITIAL

with TurbidVisionWS("192.168.1.42") as probe:   # connects + starts RX thread
    probe.start_job()
    probe.wait_for_status(STATUS_HAS_INITIAL, timeout=60)

    # Blocking generator: yields each new reading as it streams in.
    for r in probe.stream_readings():
        print(r.reading_count, f"{r.calibrated_density:.4f} g/L")
        if r.reading_count and r.reading_count > 20:
            break

    probe.shutdown()                            # stop job + laser off

Push callbacks run on the background thread with probe.on("statusrs", cb) and probe.on("message", cb). Errors raise TurbidVisionError (a subclass of IOError) on transport failure, timeout, or a non-OK reply. A runnable demo ships in the package's examples/quickstart.py; the full scenario walkthrough is on the WebSocket examples page, and every method, dataclass, and constant is in the WebSocket API reference.

Common API surface

All three libraries expose the same concepts with idiomatic naming for each platform. This is a quick map; for the complete method lists, data structures, and constants see the Arduino API reference, the Python API reference, and the WebSocket API reference.

CapabilityArduinoPythonWebSocket
Connect / probebegin(), isConnected()open(), is_present()connect() / with …, connected
Named readscalibratedDensity(), …calibrated_density(), …status().calibrated_density, …
StatusreadStatus() → TVStatusread_status() → Statusstatus() → Snapshot
Full snapshotreadAll(TVReading&)read_all() → Snapshotstatus(), latest() → Snapshot
Atomic host readsreadLatestReading(), triggerReading()latest_reading(), trigger_reading()Not applicable
Job controlstartJob(), stopJob(), takeBlank(), takeCalReading()start_job(), stop_job(), take_blank(), take_cal_reading()start_job(), stop_job(), take_blank(), take_cal_reading()
LasersetLaserPowerPct(), deactivateLaser()set_laser_power_pct(), deactivate_laser()set_laser_power_pct(), set_laser_power_mw(), deactivate_laser()
PollingwaitForStatusBit(mask, …)wait_for_status(mask, …), wait_until(pred, …)wait_for_status(flag, …), wait_until(pred, …)

For the underlying register map, transaction structure, and status bits, see the I²C interface reference.