0

Adafruit and Raspberry PI receive value and act
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit and Raspberry PI receive value and act

by joestahl on Sun Jan 03, 2016 10:06 am

Hello, I'm learning python so sorry for the ignorance in advance.

I'm writing a script to port values from my Raspberry Pi to Adafruit.io and I figured out the sending my sensors data to Adafruit which works but is clunky (I'll clean it up later.)

Now I'm trying to add an "ON"/"OFF" button in Adafruit and then have that received value execute a file or activated a pin (GPIO.LOW). I've gotten the python script to receive the value and I can seem to get the "if" statement to work write.
I code is below and I've highlighted the areas that I'm having issue with.

Any chance someone can take a look?

Start of Copy>

#Import Python Modules

import sys
import time
import Adafruit_DHT
import RPi.GPIO as GPIO

# Import Adafruit IO MGTT Client

from Adafruit_IO import MQTTClient

# Set io.adafruit Key and Username

ADAFRUIT_IO_KEY = 'API key'
ADAFRUIT_IO_USERNAME = 'username'

### not to self, add calculation to conver the value from old to 0 - 100.

# Define IO callback functions

def connected(client):
print 'Connected to Adafruit IO! Listening for feed changes...'
# subscribe to feed in a dashdoard
client.subscribe('Relay')

def disconnected(client):
print 'Disconnect from Adafruit IO!'
sys.exit(1)

def message(client, feed_id, payload):
# feed_id or {0} represents the name of the feed in the message to the Adafruit IO service.
# Payload or {1} represents the value being sent.
print 'Feed {0} recieved new vaule: {1}'.format(feed_id, payload)
if '{0}'.format(feed_id) == 'OFF':
execfile("relaytest.py")

# Create a connection instance using the IO Key and Username

client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

#Setup the callback functions define above

client.on_connect = connected
client.on_disconnect = disconnected
client.on_message = message

#connect to Adafruit Server

client.connect()

client.loop_background()

# Print the value and a message if it's over 100. Notice that the value is
# converted from string to int because it always comes back as a string from IO.

data = client.subscribe('Relay')
if data == 'ON':
execfile("/downloads/relaytest.py")

time.sleep(10)

relay = int(data.value)
print('Relay: {0}'.format(relay))
if relay == ON:
print "worked ON"
if relay == ('OFF'):
print "worked OFF"


<end of copy

Copyable window below:
Code: Select all | TOGGLE FULL SIZE
#Import Python Modules

import sys
import time
import Adafruit_DHT
import RPi.GPIO as GPIO

# Import Adafruit IO MGTT Client

from Adafruit_IO import MQTTClient

# Set io.adafruit Key and Username

ADAFRUIT_IO_KEY       = 'API key'
ADAFRUIT_IO_USERNAME  = 'username'

### not to self, add calculation to conver the value from old to 0 - 100.

# Define IO callback functions

def connected(client):
    print 'Connected to Adafruit IO! Listening for feed changes...'
    # subscribe to feed in a dashdoard
    client.subscribe('Relay')

def disconnected(client):
    print 'Disconnect from Adafruit IO!'
    sys.exit(1)

def message(client, feed_id, payload):
    # feed_id or {0} represents the name of the feed in the message to the Adafruit IO service.
    # Payload or {1} represents the value being sent.
    print 'Feed {0} recieved new vaule: {1}'.format(feed_id, payload)
    if '{0}'.format(feed_id) == 'OFF':
     execfile("relaytest.py")

# Create a connection instance using the IO Key and Username

client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

#Setup the callback functions define above

client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message

#connect to Adafruit Server

client.connect()

client.loop_background()

# Print the value and a message if it's over 100.  Notice that the value is
# converted from string to int because it always comes back as a string from IO.

data = client.subscribe('Relay')
if data == 'ON':
    execfile("/downloads/relaytest.py")

time.sleep(10)

relay = int(data.value)
print('Relay: {0}'.format(relay))
if relay == ON:
    print "worked ON"
if relay == ('OFF'):
    print "worked OFF"


joestahl
 
Posts: 2
Joined: Sun Jan 03, 2016 9:35 am

Re: Adafruit and Raspberry PI receive value and act

by joestahl on Sun Jan 03, 2016 6:14 pm

Resolved issue, but I still think the execution could be cleaner.

In the "def message(client, feed_id, payload)"

I added the following if statement.

Code: Select all | TOGGLE FULL SIZE
def message(client, feed_id, payload):
    # Message function will be called when a subscribed feed has a new value.
    # The feed_id parameter identifies the feed, and the payload parameter has
    # the new value.
    print 'Feed {0} received new value: {1}'.format(feed_id, payload)
    if '{1}'.format(feed_id, payload) == "ON":
       GPIO.output(17, GPIO.LOW)
    if '{1}'.format(feed_id, payload) == "OFF":
       GPIO.output(17, GPIO.HIGH)


I was hoping that I could us var 'relay' instead of the .format version but maybe I can make a var like this

relay = '{1}'.format(feed_id, payload)
if relay == "ON":
do whatever
instead

signed "I like cheese"

joestahl
 
Posts: 2
Joined: Sun Jan 03, 2016 9:35 am

Re: Adafruit and Raspberry PI receive value and act

by Scientist1995 on Fri Jan 25, 2019 3:22 pm

HIE,
my name is soumil shah
here is a code to your problem
or I would say whenever button changes on the website you get a message



Code: Select all | TOGGLE FULL SIZE
# Import standard python modules.
import sys
import time
import random

# Import Adafruit IO MQTT client.
from Adafruit_IO import MQTTClient


ADAFRUIT_IO_KEY = 'XXXXXXXXXXXXXX'
ADAFRUIT_IO_USERNAME = 'XXXXXX'
IO_FEED = 'XXXXXX'

def connected(client):
    print ('Connected to Adafruit IO! Listening for feed changes...')
    # subscribe to feed in a dashdoard
    client.subscribe('test')

def disconnected(client):
    print ('Disconnect from Adafruit IO!')
    sys.exit(1)

def message(client, feed_id, payload):
    # feed_id or {0} represents the name of the feed in the message to the Adafruit IO service.
    # Payload or {1} represents the value being sent.
    print ('Feed {0} recieved new vaule: {1}'.format(feed_id, payload))

    if '{1}'.format(feed_id, payload) == "ON":
        print("ON Worked ")
    if '{1}'.format(feed_id, payload) == "OFF":
        print("OFF worked ")


client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)

client.on_connect    = connected
client.on_disconnect = disconnected
client.on_message    = message
client.loop_background()
client.connect()

while 1:
    data = client.on_message



Last edited by brubell on Fri Jan 25, 2019 4:22 pm, edited 1 time in total.
Reason: removing personal information

Scientist1995
 
Posts: 1
Joined: Thu Sep 29, 2016 12:33 am

Please be positive and constructive with your questions and comments.