SmartThings Community

Raspberry Pi Temperature Sensor

device.setTemperature(x) produces the same error. I’ll keep trying…

It’s working… device is now a list (granted a list of 1), so I had to change the call to update to reference a single device:

void updateTemp() {
    def a_device = settings.tdevices.findAll { it.name == params['dev'] }
    update(a_device[0])
}

Is there any chance you could share your working code for multiple temperature probes? I have the original script set up which pulls temperature from a single probe fine, but am struggling to get it working with multiple probes (I have 4 connected). Thanks.

I’ve been trying to set up a pair of sensors (1 pi with 2 physical sensors, 1 device handler, 2 virtual sensors, 1 smartapp) and running into issues during the update process. I pass the deviceId in from python to match with the virtual sensor networkDeviceId. That part seems to work ok, but I get multiple updates and the first sensor’s value gets overwritten by the second.

Debug log:

9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:04 PM: debug yes, the temperatureScale is the same (F)
9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:04 PM: debug update, request: params: 70.03 F Outdoor Temperature Sensor
9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:04 PM: debug found device: Outdoor Temperature Sensor
9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:04 PM: debug SettingsTDevice: [Outdoor Temperature Sensor]
9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:04 PM: debug passed param dev: 28-0517c1ea54ff
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:03 PM: debug yes, the temperatureScale is the same (F)
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:03 PM: debug update, request: params: 70.03 F Outdoor Temperature Sensor
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:03 PM: debug found device: Outdoor Temperature Sensor
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:03 PM: debug SettingsTDevice: [Outdoor Temperature Sensor, Shed Temperature Sensor]
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:03 PM: debug passed param dev: 28-0517c1ea54ff
9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:01 PM: debug found device: null
9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:01 PM: debug SettingsTDevice: [Outdoor Temperature Sensor]
9d97695a-4f00-4c7e-b420-84fe7d2eb505  3:19:01 PM: debug passed param dev: 28-0517c1f437ff
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:00 PM: debug yes, the temperatureScale is the same (F)
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:00 PM: debug update, request: params: 71.94 F Shed Temperature Sensor
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:00 PM: debug found device: Shed Temperature Sensor
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:00 PM: debug SettingsTDevice: [Outdoor Temperature Sensor, Shed Temperature Sensor]
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:00 PM: debug passed param dev: 28-0517c1f437ff
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:00 PM: debug SettingsTDevice: [Outdoor Temperature Sensor, Shed Temperature Sensor]
95966712-a1b7-4ff7-8d0c-fd1d3a1e7f37  3:19:00 PM: debug passed param dev: 28-0517c1f437ff




I’m calling the python with cron every 15 mins and this is the code which loops over the 2 endpoints and seems to send it to the smartapp twice, but that should be covered by the null check in the smartapp during the update.

smartthings-temp-multi.py

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')
device_file = []
for i in range(0,2): 
 device_file.append(device_folder[i]+'/w1_slave')
#outdoor_sensor = '/sys/bus/w1/devices/28-0517c1ea54ff/w1_slave'
#shed_sensor = '/sys/bus/w1/devices/28-0517c1f437ff/w1_slave'
output_log = '/home/jclifford/tempsensor/SToutput.log'

 
def read_temp_raw(i):
    catdata = subprocess.Popen(['cat',device_file[i]],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    out,err = catdata.communicate()
    out_decode = out.decode('utf-8')
    lines = out_decode.split('\n')
    return lines
 
def read_temp(i):
    lines = read_temp_raw(i)
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_f
        
def getDeviceId(i):
    deviceId = os.path.basename(os.path.normpath(device_folder[i]))
    return deviceId
 
def main():
    endpoints_url = "https://graph.api.smartthings.com/api/smartapps/endpoints/%s?access_token=%s" % (client, access_token)
    r = requests.get(endpoints_url)
    if (r.status_code != 200):
       print("Error: " + r.status_code)
    else:
       theendpoints = json.loads( r.text )       
       for i in range(0,2):
         temp_f = read_temp(i)
         deviceId = getDeviceId(i)
         print("deviceId: " + str(deviceId)+ " TempF: " + str(temp_f))
         for endp in theendpoints:
            uri = endp['uri']
            temp_url = uri + ("/update/" + str(deviceId)+ "/%.2f/F" % temp_f)
            headers = { 'Authorization' : 'Bearer ' + access_token }
            #print(temp_url)
            r = requests.put(temp_url, headers=headers)

       quit()
 
main()

Snippet of the updating code from the smartapp:

void updateTemp() {
	log.debug "passed param dev: ${params['dev']}"
	log.debug "SettingsTDevice: ${settings.tdevice}"

    def a_device = settings.tdevice.find { it.deviceNetworkId == params['dev'] }

    log.debug "found device: ${a_device}"

    if(a_device != null){ update(a_device) }    
}

Any help is much appreciated.

Just found this thread and I need some help here. I have a Raspian Pi3. I have 3 temp sensors on it and they are connected and reporting temperatures. I installed the DHT and the ST app and also updated Python and Curl like the instructions say. When I run the python script I get a:
Traceback (most recent call last):
File “updateFromPi.py”, line 68, in
main()
File “updateFromPi.py”, line 54, in main
print("Error: " + r.status_code)
TypeError: cannot concatenate ‘str’ and ‘int’ objects

Error.

I do not know how to fix this. Can someone help? I eventually want to get all the sensors working, I will eventually have more than three.

Thank you for any help

Using the
php page from my Pi I get to the smarthings page where I can authorize the token but when I hit Authorize I get a “Page isn’t working” Any Ideas??

the error on line 54 is simple enough - just enclose r.status_code in str()

ie print("Error: " + str(r.status_code))

Does it say “Page isn’t working” or “cannot authorize at this time”?

if the latter, note what I said above:
“One thing to point out (that I learned the hard way) is that your Smartapp must not have errors or throw exceptions when you are trying to authorize it. If it does, you will get a cryptic message saying that it cannot authorize you at this time, try later or contact support. The Smartapp doesn’t actually have to do anything though, just be free of errors.”

Page isn’t working. Is the message. I will make that correction and see what happens later this evening. Thanks

Well,
Fixing the code at line 54 stopped the error that I was getting. When running th epython code I now get an error 401. I suspect that is because I do not have the Auth code. opening the accesscode.php I get to the point where I am authorizing the devices and I get a page loading then a refresh and a “Page is not working”. I assume if all was working then I would have gotten another page that would give me the auth code, right? What other way is there t get the code?

Thanks

Has anyone gotten this to work with multiple temp sensors? So far I cannot get it to work at all :frowning: I found a way to get the Auth code I think. At least I was able to create a code to use with all Smatapps. I get no errors with the python script. It runs for about 15 minutes and exits with no errors. In live logging I see a update to a temp device but no data in the app.

I know this thread is a bit old, but I am hoping someone can help me.

I am trying to use the php script to get my auth token. I get as far as getting the “code” which is 6 characters, and it redirects to the php script, but then the script seems to get stuck, there is no response and the screen is just blank. From what I can tell, this code plus the the clientID and Secret should get me the access token.

Any ideas as to what the problem could be?

Have you looked into the much newer OmniThing? It is being actively developed and supported. Just an idea…

Thanks for your quick response, it looks fairly interesting, unfortunately, the documentation leaves a lot to be desired. I cannot figure out how to configure my DS18B20 temperature sensor to be read in as a device.

Right now it looks like only DHT sensors are suppoerted.

It is my son’s project. If you ask for a specific sensor to be added, he will probably do so. The platform is very flexible and very object oriented.

wow. cool. It’s a pretty standard temperature sensor that comes in a waterproof package. I want to use it to monitor my son’s fish tank

You can do it today using my ST_Anything project on a NodeMCU ESP8266 board. It already has support for the DS18B20 temperature sensors.

Hi

Please may you assist me as I am a total newbie and not tech savvie at all. I have managed to follow all steps successfully, however I get stuck at the script for the PHP (to get the Auth Token). I am not sure how to implement this script in order to get the Token - Do I add it to the Device Handler/ SmartApp or do I run it independently and if so how would I go about doing this.
This is hopefully the last hurdle… as I have multiple raspberry pi’s with sensors conected that I use to monitor mt Vivariums and would like ti intergrate it in to Smarthings/Actiontiles

Thanks in advance

I fumbled my way through this a few days ago using my mac! You run it independently by turning on PHP via Terminal, building a little local website in a folder using Textedit, and opening that site in your browser. It sounds intimidating but it was actually very easy.

I am running into a problem where the temperature gets updated to Smartthings but then quits after an indeterminate amount of time. It could run for a few minutes or a few hours but eventually, it stops. If I restart the 'updateFromPi.py" script it works again, for a while. Does anyone have any thoughts on how I can fix this?

What I did was have the script update smartthings 30 times, waiting 60 seconds in between each time. Then I set up a cron job to run it every 30 minutes.

1 Like