1

New errors after suggested fix

import fcntl, socket, struct, dweepy, time, platform, random, sqlite3

from grovepi import*

potentiometer = 2 #A2
led = 5 #D5
ultrasonic_ranger = 4 #D4
dht_sensor_port = 7 #D7
Relay_pin = 2 #D2



from threading import Thread

publisher_state = False
publisherrotary_state = False
publishersonic_state = False
publisherhumidity_state = False
def getTemp():

        [temp,humidity] = dht(dht_sensor_port,0)
        return temp

def getultrasonic():
        distant = ultrasonicRead(ultrasonic_ranger)
        return distant

def gethumidity():

        [temp,humidity] = dht(dht_sensor_port,0)
        return humidity

def getrotary():
        i = analogRead(potentiometer)
        return i

def listener(publisher):
    for dweet in dweepy.listen_for_dweets_from('PaulPi'):
        content = dweet["content"]
        should_publish = content["start"]
        print (should_publish)
        if should_publish == "true":
            # start the publisher thread
            global publisher_state
            publisher_state = True
            if not publisher.is_alive():
                publisher = Thread(target=publisher_method_dan)
            publisher.start()
        else:
            publisher_state = False
            print ("wasn't true")

def listenersonic(publishersonic):
    for dweet in dweepy.listen_for_dweets_from('PaulPi3'):
        content = dweet["content"]
        shouldsonic_publish = content["start"]
        print (shouldsonic_publish)
        if shouldsonic_publish == "true":
            # start the publisher thread
            global publishersonic_state
            publishersonic_state = True
            if not publishersonic.is_alive():
                publishersonic = Thread(target=publisher_method_sonic)
            publishersonic.start()
        else:
            publishersonic_state = False
            print ("wasn't true")

def listenerrotary(publisherrotary):
    for dweet in dweepy.listen_for_dweets_from('PaulPi5'):
        content = dweet["content"]
        shouldrotary_publish = content["start"]
        print (shouldrotary_publish)
        if shouldrotary_publish == "true":
            # start the publisher thread
            global publisherrotary_state
            publisherrotary_state = True
            if not publisherrotary.is_alive():
                publisherrotary = Thread(target=publisher_method_rotary)
            publisherrotary.start()
        else:
            publisherrotary_state = False
            print ("wasn't true")

def listenerhumidity(publisherhumidity):
    for dweet in dweepy.listen_for_dweets_from('PaulPi7'):
        content = dweet["content"]
        shouldhumidity_publish = content["start"]
        print (shouldhumidity_publish)
        if shouldhumidity_publish == "true":
            # start the publisher thread
            global publisherhumidity_state
            publisherhumidity_state = True
            if not publisherhumidity.is_alive():
                publisherhumidity = Thread(target=publisher_method_humidity)
            publisherhumidity.start()
        else:
            publisherhumidity_state = False
            print ("wasn't true")

def publish():
    dict = {}
    dict["Temperature"] = 'getTemp'
    dweepy.dweet_for('PaulPi2', dict)

def publishhumidity():
    dict = {}
    dict["humidity"] = 'gethumidity'
    dweepy.dweet_for('PaulPi8', dict)

def publishsonic():
    dict = {}
    dict["ultrasonic"] = 'getultrasonic'
    dweepy.dweet_for('PaulPi4', dict)

def publishrotary():
    dict = {}
    dict["rotary"] = 'getrotary'
    dweepy.dweet_for('PaulPi6', dict)

def publisher_method_dan():
    while publisher_state:
          dict = {}
          dict["Temperature"] = getTemp()
          result =  dweepy.dweet_for('PaulPi2', dict)
          print (result)
          time.sleep(5)
          print ("publishing ending")

def publisher_method_humidity():
    while publisherhumidity_state:
          dict = {}
          dict["humidity"] = gethumidity()
          result =  dweepy.dweet_for('PaulPi8', dict)
          print (result)
          print ("publishing ending")

def publisher_method_sonic():
    while publishersonic_state:
          dict = {}
          dict["ultrasonic"] = getultrasonic()
          result =  dweepy.dweet_for('PaulPi4', dict)
          print (result)
          print ("publishing ending")

def publisher_method_rotary():
    while publisherrotary_state:
          dict = {}
          dict["rotary"] = getrotary()
          result_rotary =  dweepy.dweet_for('PaulPi6', dict)
          print (result_rotary)
          time.sleep(5)
          print ("publishing ending")

publisher_thread = Thread(target=publisher_method_rotary)
listener_thread = Thread(target=listenerrotary, args=(publisher_thread,))
listener_thread.start()

publisher_thread = Thread(target=publisher_method_humidity)
listener_thread = Thread(target=listenerhumidity, args=(publisher_thread,))
listener_thread.start()

publisher_thread = Thread(target=publisher_method_sonic)
listener_thread = Thread(target=listenersonic, args=(publisher_thread,))
listener_thread.start()

publisher_thread = Thread(target=publisher_method_dan)
listener_thread = Thread(target=listener, args=(publisher_thread,))
listener_thread.start()

I'm receiving the following error once i run the python code with my Raspberry Pi, any help would be appreciated as i cannot seem to figure it out.

I am running this off of my Raspberry Pi and Android Phone

The Phone is being used to press buttons to start the sensors and to try receive information from dweet.io. I can see the info on dweet.io but cannot pull the info back to the phone displaying the relevant information.

I am very new to python coding.

Python Threading Error

1

1 Answer 1

2

You are getting error because you are trying to start the thread which had already started in the past. If I'm understood correctly, I think you are intending to check whether a thread is alive or not and if it is not alive, you would like to restart it.

To accomplish this,

Replace a code like this:

if not publisherhumidity.is_alive():
    publisherhumidity = Thread(target=publisher_method_humidity)
publisherhumidity.start()

With:

if not publisherhumidity.is_alive():
    publisherhumidity = Thread(target=publisher_method_humidity)
    publisherhumidity.start() #--> place this statement inside if block
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Shubham, I have indented the code properly and the previous error is gone but now there are new errors. Any thoughts on what might be going on ?
I have added another screenshot of the errors. If you could have a look please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.