0

I am having an issue with the following code of Python 3 (the line at 5th position from bottom count=count+1):

import base64
import json
from google.cloud import iot_v1
import os
from twilio.rest import Client

account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
count = 0


def device_config(config):
    client = iot_v1.DeviceManagerClient()
    name = client.device_path(<project_ID>,
                              <Region>,  <Registry>, <Device_ID>)
    binary_data = bytes(config, 'utf-8')
    client.modify_cloud_to_device_config(name, binary_data)


def hello_pubsub(event, context):
    if 'data' in event:
        data = event['data']
        data = base64.b64decode(data)
        data = data.decode('utf-8')
        data = json.loads(data)

        temperature = float(data['temperature'])

        if temperature > 25.0:
            device_config("ledon")
            if count < 1:
                client.calls.create( \
                    url=<URL>,
                    to=os.environ['TWILIO_TO'],
                    from_=os.environ['TWILIO_FROM'])
                count = count+1
        else:
            device_config("ledoff")
    else:
        print("Data is not present!")

This function will be called continuously (imagine like an infinite loop will call this function). I want to update count by 1 when first time temperature goes above 25 and for later calls, how high it may go, LED should be on, but calling shouldnot be done

saikumar
  • 179
  • 1
  • 3
  • 12

4 Answers4

8

in hello_pubsub(event, context) you assign to count in the line count = count+1. which makes python look for count in the function scope (where it has not been defined and therefore lookup on the right-hand-side [count+1] fails).

this might be a worhty read: https://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html#variable-scope-and-lifetime

you need to tell python that you mean the global count:

def hello_pubsub(event, context):
    global count
    ...

probably cleaner would be:

def hello_pubsub(count, event, context):
    ...

and then call it using:

hello_pubsub(count, event, context)
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
1

You are referencing count inside a function, whereas it is defined outside. You should pass count as a parameter.

clubby789
  • 2,543
  • 4
  • 16
  • 32
1

You should pass count into the function as a parameter, and increment it in the loop that will call the function. If you absolutely need to assign to it within the function, you can use the statement global count before you do the assignment.

Rob Streeting
  • 1,675
  • 3
  • 16
  • 27
1

You are trying to call a local variable into another function. You also need to pass the variable into the function. Hope this helps!

account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
global count = 0



def hello_pubsub(event, context,count):