Package: obniz
obniz.py: sdk for pythonobnizsdk for python.
You can useobnizBoardorobnizOS.Control obniz from python.This sdk works withobniz api.Compatible withPython 3.6+.UsageimportasynciofromobnizimportObnizasyncdefonconnect(obniz):obniz.io0.drive("5v")obniz.io0.output(True)obniz.io1.pull("3v")obniz.io1.drive("open-drain")obniz.io1.output(False)obniz.io2.drive("3v")obniz.io2.output(True)defcallback(voltage):print("change to{}v".format(voltage))obniz.ad3.start(callback)pwm=obniz.get_free_pwm()pwm.start({"io":4})pwm.freq(1000)pwm.duty(50)uart=obniz.getFreeUart()uart.start({"tx":5,"rx":6,"baud":9600})defonreceive(data,text):print(data)uart.onreceive=onreceiveuart.send("Hello")obniz=Obniz('0000-0000')obniz.onconnect=onconnectasyncio.get_event_loop().run_forever()InstallationInstall obniz via pippipinstallobnizand import it on python file.fromobnizimportObnizConnectDetails ondoc/connectionTo use obniz, instantiate obniz with obniz id. and set onconnect callback function. It will be called when connected to obniz successfully.importasyncioasyncdefonconnect(obniz):passobniz=Obniz('0000-0000')obniz.onconnect=onconnectasyncio.get_event_loop().run_forever()You are able to use everything on obniz after connect.asyncdefonconnect(obniz):obniz.io0.drive("5v")obniz.io0.output(True)obniz.io1.pull("3v")obniz.io1.drive("open-drain")obniz.io1.output(False)obniz.io2.drive("3v")obniz.io2.output(True)defcallback(voltage):print("change to{}v".format(voltage))obniz.ad3.start(callback)pwm=obniz.get_free_pwm()pwm.start({"io":4})pwm.freq(1000)pwm.duty(50)uart=obniz.getFreeUart()uart.start({"tx":5,"rx":6,"baud":9600})defonreceive(data,text):print(data)uart.onreceive=onreceiveuart.send("Hello")ExampleEasy to integrate python libraries like TensorFlow.
(need to installtensorflowandopencv-python)importasynciofromobnizimportObnizimportcv2importnumpyasnpimporttensorflowastffromtensorflowimportkerasclass_names=['T-shirt/top','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']fashion_mnist=keras.datasets.fashion_mnist(train_images,train_labels),(test_images,test_labels)=fashion_mnist.load_data()train_images=train_images/255.0test_images=test_images/255.0model=keras.Sequential([keras.layers.Flatten(input_shape=(28,28)),keras.layers.Dense(128,activation=tf.nn.relu),keras.layers.Dense(10,activation=tf.nn.softmax)])model.compile(optimizer=tf.train.AdamOptimizer(),loss='sparse_categorical_crossentropy',metrics=['accuracy'])model.fit(train_images,train_labels,epochs=5)test_loss,test_acc=model.evaluate(test_images,test_labels)print('Test accuracy:',test_acc)defset_angle(pwm,angle):max=2.4min=0.5val=((max-min)*angle)/180.0+minpwm.pulse(val)asyncdefonconnect(obniz):obniz.io0.output(False)obniz.io1.output(True)pwm=obniz.get_free_pwm()pwm.start({"io":2})pwm.freq(50)cap=cv2.VideoCapture(0)prev=NonewhileTrue:ret,frame=cap.read()frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)ret,frame=cv2.threshold(frame,127,255,cv2.THRESH_BINARY_INV)height,width=frame.shapex=heightifheight<widthelsewidthy=heightifheight<widthelsewidthsquare=np.zeros((x,y),np.uint8)x1=int((width-x)/2)x2=int(width-(width-x)/2)y1=int((height-y)/2)y2=int(height-(height-y)/2)square=frame[y1:y2,x1:x2]cv2.imshow("frame",square)img=cv2.resize(square,(28,28),interpolation=cv2.INTER_AREA)img=(np.expand_dims(img/255.0,0))predictions_single=model.predict(img)answer=np.argmax(predictions_single[0])ifprev!=answer:print("answer:{}".format(class_names[answer]))set_angle(pwm,answer/9*180)prev=answerifcv2.waitKey(1)&0xFF==ord('q'):asyncio.get_event_loop().stop()breakawaitasyncio.sleep(0.1)cap.release()cv2.destroyAllWindows()obniz=Obniz('0000-0000')obniz.debugprint=Trueobniz.onconnect=onconnectasyncio.get_event_loop().run_forever()DocumentationYou can find the documentation onthe website.