David ParrinoSebastian Gondor
Published

StellarNet Real-Time Cellular-Wireless Spectrometer

We are sending commands and receiving real-time UV-Vis-NIR spectroscopic data from a linear pixel array spectrometer over Cellular-WiFi

AdvancedShowcase (no instructions)4 hours2,076
StellarNet Real-Time Cellular-Wireless Spectrometer

Things used in this project

Story

Read more

Code

StellarNet and Hologram Nova

Python
This is the Python script to remotely control a StellarNet spectrometer using Hologram Nova
import stellarnet
import time
import datetime
import json

from Hologram.HologramCloud import HologramCloud

# Hologram cloud object
hologram = HologramCloud(dict(), network='cellular')

pull_time = 1800    #Pull time. Every 30 minutes
device_id = '0001'  #Default ID name

#############################################
# Spectrometer object
#############################################
class Spectrometers(object):
    _map = None

    def _get_map(self):
        global _map

        if not Spectrometers._map:
            Spectrometers._map = {d.get_device_id():{'device':d,'config_id':0} \
                                      for d in stellarnet.find_devices()}
            
        return Spectrometers._map

    def __getitem__(self, device_id):
        try:
            return self._get_map()[device_id]
        except KeyError:
            abort(404)

    def get_device_ids(self):
        return self._get_map().keys()

spectrometers = Spectrometers()


#############################################
# Set configuration
#############################################
def handle_config(_device_id, parameters):
    spectro = spectrometers[_device_id]
    device = spectro['device']
    device.set_config(**parameters)

#############################################
# Get connected spectrometers
#############################################
def get_spectrometers():
    device_ids = spectrometers.get_device_ids()
    return json.dumps({'device_ids':device_ids, 
                    'timestamp':int(time.time()*1000)})

#############################################
# Get spectrum
#############################################
def get_spectro(_device_id):
    spectrometer = spectrometers[_device_id]
    spectrum = spectrometer['device'].read_spectrum()
    return json.dumps({'data':spectrum[0:1700], 
                    'timestamp':int(time.time()*1000),
                    'config_id':spectrometer['config_id']})

#############################################
# Get spectrometer configuration
#############################################
def get_config(_device_id):
    spectrometer = spectrometers[_device_id]
    config = spectrometer['device'].get_config()
    return json.dumps({'config':config, 
                    'timestamp':int(time.time()*1000),
                    'config_id':spectrometer['config_id']})

#############################################
# Send main function
#############################################
def send_message(_message, _topic, _timeout):
    # Connect to the cloud
    print (datetime.datetime.now())
    connection_status = hologram.network.getConnectionStatus()
    print ("Connection status: {}".format(connection_status))

    if (connection_status == 0):
        # Only connects if not connected yet
    	connect_status = hologram.network.connect()
    	if(connect_status == False):
    		print ("Connection failed")
    		return
    	else:
    		print ("Connected to network")

    # Send message
    response_code = hologram.sendMessage(_message, topics=_topic, timeout=_timeout)
    # Log send status
    print ("Send result: {}".format(hologram.getResultString(response_code)))

#############################################
# Send spectrum to the cloud
#############################################
def send_spectrum():
    # Get the spectrum
    spectrum_message = get_spectro(device_id)
    print ("Sending spectrum of size: {}".format(len(spectrum_message)))
    # Send message
    send_message(spectrum_message, "spectrum", 20000)

#############################################
# Send configuration to the cloud
#############################################
def send_config():
    # Get config
    config = get_config(device_id)
    print ("Sending config message of size: {}".format(len(config)))
    # Send message
    send_message(config, "config", 500)

#############################################
# Send spectrometers list to the cloud
#############################################
def send_spectrometers():
    #Get spectrometers
    spectrometers = get_spectrometers()
    print ("Sending spectrometers message of size: {}".format(len(spectrometers)))
    # Send message
    send_message(spectrometers, "spectrometers", 500)
		
#############################################
# Process incoming sms
#############################################
def check_sms():
    global pull_time
    global device_id
    sms_obj = hologram.popReceivedSMS()
    print (datetime.datetime.now())
    if sms_obj is not None:
        print("SMS: New message:{}".format(sms_obj.message))

        command, value = sms_obj.message.split("=")
        if command == "DEV_ID":
            device_id = value
            print("SMS: New dev ID:{}".format(device_id))
        elif command == "PULL_TIME":
            value_int = int(value)
            if value_int > 0 and value_int < 600:
                pull_time = value_int
                print("SMS: New pull time:{}".format(pull_time))
            else:
                print("SMS: Wrong parameter. Pull time:{}".format(pull_time))
        elif command == "GET_SPECTRUM":
            send_spectrum()
        elif command == "GET_CONFIG":
            send_config()
        elif command == "SET_CONFIG":
            print("Set Config: {}".format(value))
            handle_config(device_id, value)
        else:
            print("SMS: Wrong command")
    else:
        print("SMS: No messages")


#############################################
# Main loop
#############################################
while True:
    send_spectrum()
    check_sms()
    time.sleep(pull_time)

Credits

David Parrino

David Parrino

1 project • 1 follower
Sebastian Gondor

Sebastian Gondor

1 project • 1 follower
Thanks to Scott Jordan.

Comments