Skip to content

Commit

Permalink
Merge pull request #95 from AccelerationConsortium/docs2
Browse files Browse the repository at this point in the history
docs and src scripts openflexure rs232
  • Loading branch information
sgbaird authored Oct 24, 2024
2 parents 11cc653 + e87644a commit f7a4aa4
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 19 deletions.
13 changes: 13 additions & 0 deletions src/ac_training_lab/openflexure/huggingface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Openflexure Microscope
emoji: 🐨
colorFrom: yellow
colorTo: green
sdk: streamlit
sdk_version: 1.38.0
app_file: app.py
pinned: false
license: mit
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
10 changes: 5 additions & 5 deletions src/ac_training_lab/openflexure/huggingface/gui_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ def show():
# caption='PIL Image', use_column_width=True)
import streamlit as st
from microscope_demo_client import MicroscopeDemo
from my_secrets import HIVEMQ_BROKER

host = "marooncarder-nvscfy.a02.usw2.aws.hivemq.cloud"
port = 8883
microscopes = [
"microscope",
Expand All @@ -15,7 +15,7 @@ def show():

def get_pos_button():
microscope = MicroscopeDemo(
host,
HIVEMQ_BROKER,
port,
microscopeselection + "clientuser",
access_key,
Expand All @@ -30,7 +30,7 @@ def get_pos_button():

def take_image_button():
microscope = MicroscopeDemo(
host,
HIVEMQ_BROKER,
port,
microscopeselection + "clientuser",
access_key,
Expand All @@ -46,7 +46,7 @@ def take_image_button():

def focus_button():
microscope = MicroscopeDemo(
host,
HIVEMQ_BROKER,
port,
microscopeselection + "clientuser",
access_key,
Expand All @@ -59,7 +59,7 @@ def focus_button():

def move_button():
microscope = MicroscopeDemo(
host,
HIVEMQ_BROKER,
port,
microscopeselection + "clientuser",
access_key,
Expand Down
18 changes: 9 additions & 9 deletions src/ac_training_lab/openflexure/huggingface/key_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def show():
# import pymongo.mongo_client
import requests
import streamlit as st
from my_secrets import api_token, base_url, brokerip, uri
from my_secrets import HIVEMQ_API_TOKEN, HIVEMQ_BASE_URL, HIVEMQ_BROKER, MONGODB_URI
from pymongo.mongo_client import MongoClient

# SET UP ON DATABASE you need to make a variable for the time called the
Expand All @@ -23,7 +23,7 @@ def show():
]
brokerport = "8883"

client = MongoClient(uri)
client = MongoClient(MONGODB_URI)
db = client[database_name]
collection = db[collection_name]

Expand All @@ -45,9 +45,9 @@ def check_variable(variable_name):
return f"An error occurred: {e}"

def create_user(username, password):
api_url = base_url + "/mqtt/credentials"
api_url = HIVEMQ_BASE_URL + "/mqtt/credentials"
headers = {
"Authorization": f"Bearer {api_token}",
"Authorization": f"Bearer {HIVEMQ_API_TOKEN}",
"Content-Type": "application/json",
}

Expand All @@ -57,19 +57,19 @@ def create_user(username, password):

def delete_user(username):
headers = {
"Authorization": f"Bearer {api_token}",
"Authorization": f"Bearer {HIVEMQ_API_TOKEN}",
"Content-Type": "application/json",
}

api_url = base_url + "/mqtt/credentials/username/" + username
api_url = HIVEMQ_BASE_URL + "/mqtt/credentials/username/" + username
requests.delete(api_url, headers=headers)

def role_user(username, role):
headers = {
"Authorization": f"Bearer {api_token}",
"Authorization": f"Bearer {HIVEMQ_API_TOKEN}",
"Content-Type": "application/json",
}
api_url = base_url + "/user/" + username + "/roles/" + role + "/attach"
api_url = HIVEMQ_BASE_URL + "/user/" + username + "/roles/" + role + "/attach"
requests.put(api_url, headers=headers)

def update_variable(variable_name, new_value):
Expand Down Expand Up @@ -114,7 +114,7 @@ def button():

st.write("Keys will last 30 minutes before being overridable")
st.write("Broker IP:")
st.code(brokerip)
st.code(HIVEMQ_BROKER)
st.write("Broker port:")
st.code(brokerport)
st.write("Usernames:")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
paho-mqtt
Pillow
pymongo
requests
streamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
paho-mqtt==2.1.0
Pillow==9.5.0
pymongo==3.11.0
requests==2.32.3
streamlit==1.37.1
10 changes: 5 additions & 5 deletions src/ac_training_lab/openflexure/huggingface/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
paho-mqtt==2.1.0
Pillow==9.5.0
pymongo==3.11.0
requests==2.32.3
streamlit==1.37.1
paho-mqtt
Pillow
pymongo
requests
streamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def simulate_rs232_data():
# Define the ASCII codes for the components of "11.2 g"
data = [
ord("+"), # Positive sign (+)
ord(" "), # Space
ord("1"), # First digit
ord("1"), # Second digit
ord("."), # Decimal point
ord("2"), # Third digit
ord(" "), # Space
ord("g"), # Unit 1
ord(" "), # Unit 2 (can be another unit, if applicable)
ord(" "), # Unit 3 (optional or placeholder)
ord("\n"), # End/return (new line)
]

# Convert ASCII codes to characters and form the message
message = "".join(chr(byte) for byte in data)

# Simulate the RS232 data being sent/received
return message


# Test the function
if __name__ == "__main__":
rs232_message = simulate_rs232_data()
print("Simulated RS232 Data:")
print(rs232_message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import time

from machine import UART, Pin

# Initialize UART interfaces
uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))

# Data to be sent
txData = b"RS232 receive test...\r\n"
print(f"Writing to uart0: {txData}")
uart0.write(txData)
time.sleep(0.1)

timeout = 10 # Timeout in seconds
start_time = time.time()

print("Starting UART communication...")

while True:
current_time = time.time()
if current_time - start_time > timeout:
print("Timeout exceeded. Exiting...")
break

# Check for data on uart0
if uart0.any() > 0:
print("Data available on uart0")
while uart0.any() > 0: # Channel 0 is spontaneous and self-collecting
rxData0 = uart0.read()
if rxData0:
decoded_data = rxData0.decode("utf-8").strip()
print(f"uart0 received: {decoded_data}")
uart0.write(decoded_data)
if uart0.any() == 0:
uart0.write("\r\n")

# Check for data on uart1
if uart1.any() > 0:
print("Data available on uart1")
while uart1.any() > 0: # Channel 1 is spontaneous and self-collecting
rxData1 = uart1.read()
if rxData1:
decoded_data = rxData1.decode("utf-8").strip()
print(f"uart1 received: {decoded_data}")
uart1.write(decoded_data)
if uart1.any() == 0:
uart1.write("\r\n")

time.sleep(0.5) # Add a small delay to avoid busy-waiting
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import time

from machine import UART, Pin

# Initialize UART interfaces
uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))

# Data to be sent
txData = b"RS232 receive test...\r\n"
print(f"Writing to uart0: {txData}")
uart0.write(txData)
time.sleep(0.1)

timeout = 10 # Timeout in seconds
start_time = time.time()

print("Starting UART communication...")

while True:
current_time = time.time()
if current_time - start_time > timeout:
print("Timeout exceeded. Exiting...")
break

# Check for data on uart0
if uart0.any() > 0:
print("Data available on uart0")
while uart0.any() > 0: # Channel 0 is spontaneous and self-collecting
rxData0 = uart0.read()
if rxData0:
decoded_data = rxData0.decode("utf-8").strip()
print(f"uart0 received: {decoded_data}")
uart0.write(decoded_data)
if uart0.any() == 0:
uart0.write("\r\n")

# Check for data on uart1
if uart1.any() > 0:
print("Data available on uart1")
while uart1.any() > 0: # Channel 1 is spontaneous and self-collecting
rxData1 = uart1.read()
if rxData1:
decoded_data = rxData1.decode("utf-8").strip()
print(f"uart1 received: {decoded_data}")
uart1.write(decoded_data)
if uart1.any() == 0:
uart1.write("\r\n")

time.sleep(0.5) # Add a small delay to avoid busy-waiting
33 changes: 33 additions & 0 deletions src/ac_training_lab/picow/mass-balance/us-solid/_scripts/scale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import time

from machine import UART, Pin

# Initialize UART interfaces
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))

# Data to be sent
# txData = b"RS232 receive test...\r\n"
# print(f"Writing to uart0: {txData}")
# uart0.write(txData)
# time.sleep(0.1)

timeout = 10 # Timeout in seconds
start_time = time.time()

print("Starting UART communication...")

while True:
current_time = time.time()
if current_time - start_time > timeout:
print("Timeout exceeded. Exiting...")
break

# Check for data on uart0
if uart0.any() > 0:
print("Data available on uart0")
while uart0.any() > 0: # Channel 0 is spontaneous and self-collecting
rxData0 = uart0.read()
print(f"rxData0: {rxData0}")
print(rxData0)

time.sleep(0.1) # Add a small delay to avoid busy-waiting
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# connect the TX and RX pins of the Pico to test the UART communication
import time

from machine import UART, Pin

# Initialize UART interface
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))

# Data to be sent
txData = b"RS232 receive test...\r\n"
print(f"Writing to uart0: {txData}")
uart0.write(txData)
time.sleep(0.1)

timeout = 10 # Timeout in seconds
start_time = time.time()

print("Starting UART communication...")

while True:
current_time = time.time()
if current_time - start_time > timeout:
print("Timeout exceeded. Exiting...")
break

# Check for data on uart0
if uart0.any() > 0:
print("Data available on uart0")
while uart0.any() > 0: # Channel 0 is spontaneous and self-collecting
rxData0 = uart0.read()
if rxData0:
decoded_data = rxData0.decode("utf-8")
print(f"uart0 received: {decoded_data}")
uart0.write("{}".format(decoded_data))
if uart0.any() == 0:
uart0.write("\r\n")

time.sleep(0.5) # Add a small delay to avoid busy-waiting

0 comments on commit f7a4aa4

Please sign in to comment.