Total noob looking for a simple python3 example

Is there any simple python tutorials or examples someone can point this refugee from Particleworld, looking for a fresh start?

The notecards seem very simple, but a couple of tests I have have done so far never sync the data back to the hub.

Looking for something simple that might do some monr data logging and send data back to the cloud every cfew minutes?

1 Like

Welcome! We have a repo on Github ready for you: GitHub - blues/note-python: Python API for Notecard. This should get you started. Please let us know how it goes.

1 Like

Welcome @vstadmin, former Particle-person here :smiley: If there’s anything I can do to make sure your Blues experience is great one, feel free to message me!

For Python, I recommend walking through one of our Python sensor tutorials, depending on whether you are using Python on a Pi or Looking for a CircuitPython example.

Hope that helps, and keep us posted!

1 Like

I took that python script and modified it slightly because I dont currently have an i2c temp sensor.

a few days ago, when I first ran the program, I was not immediately seeing anything in the event log at all, but later the events seemed to show up overnight.

Im now running my script again and not seeing any transactions in the event log.

My sample code is below:

import time
import json
import notecard
from periphery import I2C
import random

productUID = "com.xxxxxxx.yyyyy:zzzzzzz"
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port,0,0)

req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "periodic"
req["outbound"] = 10

print(json.dumps(req))

req = {"req" : "hub.sync"}

rsp = card.Transaction(req)
print(rap)

while True:

    r = random.randint(1,10)

    req = {"req" :"note.add"}
    req["file"] = "test.qo"
    req["start"] = True
    req["body"] = { "random":r }

    rsp = card.Transaction(req)
    print(rsp)

    time.sleep(10)

My output is:

{"req": "hub.set", "product": "com.vsthose.admin:vstcp2", "mode": "periodic", "outbound": 10}
{}
{'total': 1}
{'total': 2}
{'total': 3}

but I do not see it show up in the events?

How ado I tell that my modem is actually online?

How do I tell that I am actually sending packets?

Is there something I should be differently for simple requests to the events?

This is really brand new to me.

Thanks!

of course as soon as I say that.

I get a big dump in the log and see the events:

How do I get the events to come in more reliably when they actually occur?

The system seems to be “burpy” and I can’t see any information that gives me a clue of anything I can do to make it a better experience.

It really depends on how you configure the Notecard. If you want things to show up in the event table on Notehub closer to when the notes were written on the Notecard, check out this guide: Minimizing Latency - Blues Developers

And you can learn more about Notecard sync modes in the reference here: Minimizing Latency - Blues Developers

thanks for the bread crumb. I will read.

You have to remember that when someone comes in totally fresh, “where” to look for answers is definitely not obvious. We try, but sometimes the frustration levels when pushed up against deadlines gets the better of us and we miss things.

Thanks though. I will read thoroughly.

Hey @vstadmin ,

I was just looking through the code you posted. You are overwriting your hub.set request with req = {"req" : "hub.sync"}. Try to add card.Transaction(req) after req["outbound"] = 10.

Another reason you might not see notes appear immediately in Notehub is because you are using periodic mode and syncing approximately every 10 minutes:

So in this model, you are adding numerous notes to your Notecard (as evidenced by the total incrementing). But you won’t see them right away, as Notecard tries to be as careful as possible about using power!

If you want more immediate visibility in Notehub, you have a couple options:

  1. When adding your note with note.add, add sync = true to attempt an immediate sync (e.g. req["sync"] = True.
  2. Perform a manual hub.sync.

It’s also a priority of ours to get Python code examples up in the API reference as well FYI!

thanks for the insights!

based on what some have said, I have modified my sample code as follows:

import time
import json
import notecard
from periphery import I2C
import random

productUID = "com.xxxx.yyy.zzzzz"
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port,0,0)

req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "continuous"
req["sync"] = True

req["outbound"] = 15

print(json.dumps(req))


req = {"req" : "hub.sync"}

rsp = card.Transaction(req)
print(rsp)

while True:

    r = random.randint(1,10)
    
    req = {"req" :"note.add"}
    req["file"] = "vstdata.qo"
    req["start"] = True
    req["sync"] = True
    req["body"] = { "ID":"VSTCP2","Datestamp":"2021/03/26T07:39:43","Pressure":-3.67,"Cycles":3231,"Mode":0,"Faults":
    
    rsp = card.Transaction(req)
    print(rsp)
    
    time.sleep(15)

It runs once and it updates the cloud immediately, but apparently after the 15 second interval, it crashes the program?

I get:

python3 ./test2.py 
{"req": "hub.set", "product": "com.vsthose.admin:vstcp2", "mode": "continuous", "sync": true, "outbound": 15}
{}
{'total': 1}
Traceback (most recent call last):
  File "./test2.py", line 36, in <module>
    rsp = card.Transaction(req)
  File "/home/pi/.local/lib/python3.7/site-packages/notecard/notecard.py", line 311, in Transaction
    raise Exception("notecard request or response was lost")
Exception: notecard request or response was lost

Should I not be requesting the req = {"req" :"note.add"} a subsequent time?

I tried adding the setup above the loop:

req = {"req" :"note.add"}
req["file"] = "vstdata.qo"
req["start"] = True
req["sync"] = True

while True:

    req["body"] = { "ID":"VSTCP2","Datestamp":"2021/03/26T07:39:43","Pressure":-3.67,"Cycles":3231,"Mode":0,"Faults":
    
    rsp = card.Transaction(req)
    print(rsp)
    
    time.sleep(15)

But I still had the same error.

It looks like you’re still overwriting your hub.set request with your hub.sync request. Try this (not tested, but eyeballing it it looks accurate):

productUID = "com.xxxx.yyy.zzzzz"
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port,0,0)

req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "continuous"
req["sync"] = True
req["outbound"] = 15
rsp = card.Transaction(req)

print(json.dumps(req))

# fyi this hub.sync should not be needed
req = {"req" : "hub.sync"}
rsp = card.Transaction(req)
print(rsp)

while True:

    r = random.randint(1,10)
    
    req = {"req" :"note.add"}
    req["file"] = "vstdata.qo"
    req["sync"] = True
    req["body"] = { "ID":"VSTCP2","Datestamp":"2021/03/26T07:39:43","Pressure":-3.67,"Cycles":3231,"Mode":0,"Faults":
    
    rsp = card.Transaction(req)
    print(rsp)
    
    time.sleep(15)

With this cleaner code:

import time
import json
import notecard
from periphery import I2C
import random


productUID = "com.xxxx.yyy.zzzzz"
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port,0,0,debug=True)

req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "continuous"
req["sync"] = True
req["outbound"] = 15
rsp = card.Transaction(req)

print(json.dumps(req))
print(rsp)

while True:

    r = random.randint(1,10)
    
    req = {"req" :"note.add"}
    req["file"] = "vstdata.qo"
    req["sync"] = True
    req["body"] = { "ID":"VSTCP2","Datestamp":"2021/03/26T07:39:43","Pressure":-3.67,"Cycles":3231,"Mode":0,"Faults":
    
    rsp = card.Transaction(req)
    print(rsp)
    
    time.sleep(15)

I am getting a different message that is not obvious to me (because it worked a minute ago)
python ./test3.py
File “./test3.py”, line 33
rsp = card.Transaction(req)
^
SyntaxError: invalid syntax

Looks like the end of your JSON isn’t properly ended:

req["body"] = { "ID":"VSTCP2","Datestamp":"2021/03/26T07:39:43","Pressure":-3.67,"Cycles":3231,"Mode":0,"Faults":

Yeah I found that right about the same time.

Which now undercovers a different problem.

I fixed the JSON code and now it tells me:

python ./test3.py
Traceback (most recent call last):
  File "./test3.py", line 3, in <module>
    import notecard
ImportError: No module named notecard

I checked with pip3 and the module I have been using for days is indeed there. I have not made any changes to the top part of the code except for switching my ProductID in and out.

BTW, I really like your bird identifier. Thinking about doing that myself, If I can get all the part together.

Hmmm…you could try putting the notecard directory from the note-python library in a lib directory of the project. Curious to know if that will solve the issue.

I could. Its just very strange, because I have not deviated from this screen or the underlying terminal window and pip3 says the module iSight where its supposed to.

DUH… python v python3.

Somewhere I switched them on my command line.

I am still getting this message though:

python3 ./test3.py
Traceback (most recent call last):
File “./test3.py”, line 19, in
rsp = card.Transaction(req)
File “/home/pi/.local/lib/python3.7/site-packages/notecard/notecard.py”, line 311, in Transaction
raise Exception(“notecard request or response was lost”)
Exception: notecard request or response was lost

From this:

from periphery import I2C
import random
import time
import json
import notecard

productUID = "com.vsthose.admin:vstcp2"

#productUID = "com.xxxx.yyy.zzzzz"
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port,0,0)

req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "continuous"
req["sync"] = True
req["outbound"] = 15

rsp = card.Transaction(req)

print(json.dumps(req))
print(rsp)

while True:

    r = random.randint(1,10)
    
    req = {"req" :"note.add"}
    req["file"] = "vstdata.qo"
    req["sync"] = True
    req["body"] = { "ID":"VSTCP2","Datestamp":"2021/03/26T07:39:43","Pressure":-3.67,"Cycles":3231,"Mode":0,"Faults":1023}
    rsp = card.Transaction(req)
    print(rsp)
    
    time.sleep(15)

Same error as before, but I believe the culprits have been fixed.

Can you verify that you’re using the latest version of the note-python library AND your Notecard is on the latest firmware (1.5.3)?

1 Like

notecard is on 1.5.2.12200
and note-python is at 1.3.2