Page 1 of 1

Refresh Input

Posted: 26 Sep 2019, 04:57
by Yvonne
Hi, I am currently trying to make my code be able to sync the bit value of an virtual input from revpi called "Input_Bit_4". The issue I am facing right now is that the code, or rather the terminal (command line) can only take in one value and when I reset and on/off the revpi the code fails to read the new input unless I refresh the whole program. I want to ask if there is any way for my code to read the new input and execute accordingly without having to re-run the whole program manually? All help is greatly appreciated.

The following is a portion of my code that I am working with-
def light_stat (input_value):
input_value = revpi.io.Input_Bit_4.value
if input_value == 1.00:
light_value = True
print("Light Turned On")
else:
light_value = False
print("Light Turned Off")

return light_value

Re: Refresh Input

Posted: 30 Sep 2019, 14:47
by dirk
Hi I have moved your post to the RevPiModIO forum as the specialists here sure have the best knowledge about this library.

Re: Refresh Input

Posted: 04 Oct 2019, 11:43
by RevPiModIO
Hi Yvonne!

I need some more information about your problem! Do you use RevPiPyLoad to start your program on the RevPi on startup?

And it would be nice if you could post more of your source code. Do you use RevPiModIO(autorefresh=True)? Did your program end after setting the output - if yes, do you use .exit() on the end of your program? Who is working with your virtual device? Your python program, only? Die values will stay persistent between starting and stopping your program, but not after a reboot of the revpi.

You wrote "Bit" Value. A virtual device has only byte / word values, so you should use the io.NAME.replace_io("new_name", "?", bit=0) function to convert the io to your data type.

Regards, Sven

Re: Refresh Input

Posted: 10 Oct 2019, 05:00
by Yvonne
Hi Sven, thanks you so much for the reply!
I tried using the exit() function but it did no purpose to the code and I did not use RevPiPyLoad as I am using HTTP instead of MQTT. As for the values, it changes in the piTest but not in the program.
The naming was given by the pictory and is running under the Boolean type.
The following is the code I am currently working with please take a look.

#!/usr/bin/python
import random
import requests
import time
import revpimodio2
import os
import uuid
import subprocess
from subprocess import PIPE, Popen
revpi = revpimodio2.RevPiModIO(autorefresh=True)
input_value = float(revpi.io.Input_Bit_4.value)
scan_value = float(revpi.io.Input_Bit_4.value)
rada_value = float(revpi.io.Input_Bit_5.value)
TOKEN = "BBFF-cBDjtysoQbVrWDaejfaGm6fDCR7ovo" # Assign your Ubidots TOKEN
DELAY = 3 # Set the delay desired to post the data
s = requests.Session()
s.headers.update({'x-test': 'true'})

def build_json(variable_1, value_1, variable_2, value_2, variable_3, value_3, variable_4, value_4, variable_5, value_5, variable_6, value_6):
try:
data = {variable_1: value_1, variable_2: value_2, variable_3: value_3, variable_4: value_4, variable_5: value_5, variable_6: value_6}
return data
except:
return None

def post_variable(device, value_1, value_2, value_3, value_4, value_5, value_6):
try:
url = "https://industrial.api.ubidots.com/api/v1.6/devices/" + device
headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
data = build_json("temperature", value_1, "light", value_2, "voltage",value_3, "Scanner", value_4, "Radar", value_5, "E-Stop", value_6)
response = requests.post(url=url, headers=headers, json=data)
return response.json()
except:
pass

def ubidots_light():
if input_value == 1:
light_value = True
else:
light_value = False
print("Input: ",input_value)
print("Ubi Light: ",light_value)
print("-"*20)
return light_value

def measure_temp():
temp = os.popen("vcgencmd measure_temp").readline()
temp1 = temp.replace("temp=","")
temp_value = temp1.replace("'C","")
print(temp1)
return temp_value

def measure_volts():
volt = os.popen("vcgencmd measure_volts core").readline()
volt1 = volt.replace("volt=","")
volt_value = volt1.replace("V","")
print(volt1)
return volt_value

def scanner (scan_value):
if scan_value == 1:
scanner_value = True
else:
scanner_value = False
print("Scanner: ",scan_value)
print("Scanner Indicator: ",scanner_value)
print("-"*20)
return scanner_value

def radar (rada_value):
if rada_value == 1:
radar_value = True
else:
radar_value = False
print("Radar: ",rada_value)
print("Radar Indicator: ",radar_value)
print("-"*20)
return radar_value

def e_stop (e_value):
if e_value == 1:
estop_value = True
else:
estop_value = False
print("E-Stop: ",e_value)
print("E-Stop Indicator: ",estop_value)
print("-"*20)
return estop_value

if __name__ == "__main__":
device_mac = ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) for ele in range(0,8*6,8)][::-1])
while True:

e_value = random.randint(0,1)
scanner_value = scanner(scan_value)
radar_value = radar(rada_value)
estop_value = e_stop(e_value)
temp_value = measure_temp()
light_value = ubidots_light()
volt_value = measure_volts()
time.sleep(DELAY)
post_variable(device_mac, temp_value, light_value, volt_value, scanner_value, radar_value, estop_value)
exit()

Please enlighten me with your help and I am truly grateful :)
Regards,
Yvonne

Re: Refresh Input

Posted: 10 Oct 2019, 18:42
by RevPiModIO
Hi Yvonne!

That will not work!

You have to use your "revpi" object to set IOs of it!
To set just variables with the same name as the piCtory names will not work.

Code: Select all

revpi.io.pictory_name.value

# Something from your code should look like (if the piCtory name is rada_value:
print("Radar: ", revpi.io.rada_value.value)

# If the piCtory name is just rada, it have to look like this:
print("Radar: ", revpi.io.rada.value)

Please check out our examples to see, how it will work https://revpimodio.org/en/blogs/examples/!

Regards, Sven