0

So currently I am these two method where one reads the RF Data from another device constantly and another method sends that data every so often.

How could I do this? I need the RF Data incoming to be constantly updated and received while the sendData() method just grabs the data from the global variable whenever it can.

Heres the code below so far but it's not working...

import httplib, urllib
import time, sys
import serial
from multiprocessing import Process

key = 'MY API KEY'
rfWaterLevelVal = 0

ser = serial.Serial('/dev/ttyUSB0',9600)

def rfWaterLevel():
    global rfWaterLevelVal

    rfDataArray = ser.readline().strip().split()
    print 'incoming: %s' %rfDataArray
    if len(rfDataArray) == 5:
        rfWaterLevelVal = float(rfDataArray[4])
        print 'RFWater Level1: %.3f cm' % (rfWaterLevelVal)
        #rfWaterLevel = 0

def sendData():
    global rfWaterLevelVal

    params = urllib.urlencode({'field1':rfWaterLevelVal, 'key':key})
    headers = {"Content-type" : "application/x-www-form-urlencoded","Accept": "text/plain"}
    conn = httplib.HTTPConnection("api.thingspeak.com:80", timeout = 5)
    conn.request("POST", "/update", params, headers)
    #print 'RFWater Level2: %.3f cm' % (rfWaterLevelVal)
    response = conn.getresponse()
    print response.status, response.reason
    data = response.read()
    conn.close()

while True:
    try:
        rfWaterLevel()
        p = Process(target=sendData(), args())
        p.start()
        p.join()

        #Also tried threading...did not work..
        #t1 = threading.Thread(target=rfWaterLevel())
        #t2 = threading.Thread(target=sendData())
        #t1.start()
        #t1.join()
        #t2.join()
    except KeyboardInterrupt:
        print "caught keyboard interrupt"
        sys.exit()

Please help!

Just to clarify, I need rfWaterLevel() method to run constantly as the rf data is incoming constantly, and I need sendData() to just be called as soon as it's ready to send again (roughly every 5 seconds or so). But it seems as if, if there is any sort of delay to the incoming rf data then rf data stops updating itself (the received end) and thus the data being sent is not accurate to what is being sent from the rf transmitter.

Thanks in advance!

Verglas
  • 79
  • 2
  • 10

1 Answers1

1

I can't give you a full solution but I can guide you into the right direction.

Your code has three problems.

  1. Process starts (as the name suggests) a new process and not a new thread. A new process cannot share data with the old process. You should use mutlithreading instead. Have a look at threading as explained here

  2. You are calling rfWaterLevel() inside the main thread. You need to start the second thread before entering the while Loop.

  3. Your are creating the second thread again and again inside the while Loop. Create it only once and put the while Loop inside the function

Your basic program structure should be like this:

import time

def thread_function_1():
    while True:
        rfWaterLevel()

def thread_function_2():
    while True:
        sendData()
        time.sleep(5)

# start thread 1
thread1 = Thread(target = thread_function_1)
thread1.start()

# start thread 2
thread2 = Thread(target = thread_function_2)
thread2.start()

# wait for both threads to finish
thread1.join()
thread2.join()
Community
  • 1
  • 1
Holger Waldmann
  • 101
  • 1
  • 3
  • Thanks for the answer, just a question, what if the first thread is not "supposed" to stop? So what I mean is, the first threading function is supposed to just constantly run as the rf data comes in constantly... do I just get rid of the "join()" statements for both? – Verglas Aug 01 '16 at 02:03