NameError: name 'resin' is not defined

support
raspberrypi3

#1

I am trying to use the resin.io API from a Raspberry device. The python code pushes some sample data to firebase properly. But once I want to push a resin environment variable I am unable to get the resin sdk for python. How should the library be referenced? The value I want to push is the UUID of the device connected to resin.io

#!/usr/bin/python

import time
import json
from resin import Resin
resin = Resin()
firebase_url = ‘https://temploggerpython.firebaseio.com/
#Setup a loop to send Temperature values at fixed intervals in seconds
fixed_interval = 60
#Get the dive ID from Resin
deviceUUID = resin.models.device.identify()
while 1:
try:
temperature_c = “25.58”
#current time and date
time_hhmmss = time.strftime(’%H:%M:%S’)
date_mmddyyyy = time.strftime(’%d/%m/%Y’)

    #current location name
    #temperature_location = 'Drammen-Norway';
    #print temperature_c + ',' + time_hhmmss + date_mmddyyyy + ',' + temperature_location
    print(temperature_c + ',' + time_hhmmss + date_mmddyyyy)
    #insert record
    data = { 'date':date_mmddyyyy,'time':time_hhmmss,'value':temperature_c }
    #result = requests.post(firebase_url + '/' + temperature_location + '/temperature.json', data=json.dumps(data) )
    result = requests.post(firebase_url + '/' + str(deviceUUID) + '/temperature.json', data=json.dumps(data) )

    print('Record inserted. Result Code = ' + str(result.status_code) + ',' + result.text)
    time.sleep(fixed_interval)
    
except IOError:
    print('Error! Something went wrong.')
    time.sleep(fixed_interval) 

I found this in the documentation of Resin SDK for Python:
deviceUUID = resin.models.device.identify()

But my terminal is complaining that as the below error:
25.04.17 21:07:54 (+0200) NameError: name ‘resin’ is not defined

How should be referenced properly?


#2

Hey there,

First of all, if you are only interested in getting the device UUID, you might not need the SDK at all; your code can simply read the RESIN_DEVICE_UUID environment variable, which is set by default by the on-device Supervisor when it starts your application container.

Now, with regards to the particular NameError, have you tried logging in (e.g. see https://github.com/resin-io/resin-sdk-python#basic-usage) before invoking the resin.models.device.identify() method? @nghiant2710 might also have a few ideas here on the proper usage of the Python SDK if something is missing.

Best, Kostas


#3

Hi Kostas,
Thanks for your answer. No, I did not tried to login as described on the link you have attached.
Can you give a hint how to read UUID env variable without the SDK? I am running the attached python code on a Raspberry Pi 3.


#4

hey @Biagio74, there is an env var for the UUID: RESIN_DEVICE_UUID. We expose it in the container so you can just read from it.

Regarding the SDK, it’s weird since the issue indicates that resin variable is not defined. Can you please attached your python script here so I can try it?

Also you need to login to use that method and it won’t return the UUID for you. You should use https://docs.resin.io/tools/python-sdk/#function-get_by_name-name- to get the UUID.


#5

Hi, thanks for the feedback.
Please see my python code at the link on DropBox. (I’m sorry I do not know how to attach nicely)
https://towr.co/2pkZ51u
The functions get by name might not work for me. These devices later should be sent out to fields with the resin.io image and once they are online then they should start pushing data to some cloud (i.e Firebase)
My idea is to understand from that cloud service data that which devices they are coming from. Pls see an attached image from Firebase. So, instead of the location hard coded previously to the device (in this case Drammen-Norway) that should be the UUID. And based on that and thanks to the SDK other data could be fetched.
Regards, Balazs


#6

Hi, just one more small comment.
Basically I would like to do the same as here: https://github.com/shaunmulligan/firebaseDTL but with Python :wink:


#7

Hey @Biagio74,

As @nghiant2710 mentioned you need to authenticate with the SDK before running device.identify().

But as we mentioned you don’t really need the sdk in this case because we expose the device uuid as an environment variable to your app so you could do…

deviceUUID  = os.environ['RESIN_DEVICE_UUID']

... your code

Does that answer your questions?


#8

Oh and also in your code you are missing

   from resin import Resin
   resin = Resin()

I’m not sure if you removed it when copying to dropbox but it would explain the NameError.


#9

Hi craig,
Thanks, now it works :slight_smile:
Here it is what I did. Define the variable as suggested, but without from resin import Resin
Best Regards, Balazs
PS.: guys, thanks for the great support


#10

Actually I removed it because of the error. And now, if I apply these codes then I have similar error. So, at the end it works without the import. Should it be mention resin in the requirements.txt? …I did not try now but before I did and did not help.
Here is the error in case import resin:
26.04.17 17:51:14 (+0200) Systemd init system enabled.
26.04.17 17:51:14 (+0200) systemd 215 running in system mode. (+PAM +AUDI
T +SELINUX +IMA +SYSVINIT +LIBCRYPTSETUP +GCRYPT +ACL +XZ -SECCOMP -APPAR
MOR)
26.04.17 17:51:14 (+0200) Detected virtualization ‘other’.
26.04.17 17:51:14 (+0200) Detected architecture ‘arm’.
26.04.17 17:51:14 (+0200) Set hostname to <25f7e8e>.
26.04.17 17:51:18 (+0200) Traceback (most recent call last):
26.04.17 17:51:18 (+0200) File “data_logger.py”, line 5, in
26.04.17 17:51:18 (+0200) from resin import Resin
26.04.17 17:51:18 (+0200) ImportError: No module named resin


#11

Yea sounds like you haven’t installed it? Add this to your Dockerfile to install it in the build step.

RUN pip install git+https://github.com/resin-io/resin-sdk-python.git


#12

yeap, this was missing