tag:blogger.com,1999:blog-909746912855922547.post4251160897577140837..comments2024-03-28T02:11:48.240-07:00Comments on Network Automation Nerds: LittleBits CloudBit with Python Requests - Part 2 Input and OutputEric Chou http://www.blogger.com/profile/11336226720003537549noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-909746912855922547.post-20254874071262123442018-05-31T20:30:45.251-07:002018-05-31T20:30:45.251-07:00Hi, what if I don't want the stream mode but I...Hi, what if I don't want the stream mode but I only want to read the value once? Like temperature? Ty! this was very helpful4everlagartohttps://www.blogger.com/profile/02163591173048049470noreply@blogger.comtag:blogger.com,1999:blog-909746912855922547.post-48470527592233705112015-08-04T17:29:53.973-07:002015-08-04T17:29:53.973-07:00Hi Jonathan, the nice folks at LittleBits is RMA&#...Hi Jonathan, the nice folks at LittleBits is RMA'ing the old cloudBit. The new one should arrive soon and I will try it out with some IoT streaming and display solutions like Xively or Push. Eric Chou https://www.blogger.com/profile/11336226720003537549noreply@blogger.comtag:blogger.com,1999:blog-909746912855922547.post-91697158657301529022015-08-01T17:57:34.711-07:002015-08-01T17:57:34.711-07:00That helps me understand better. Thanks again.
Re...That helps me understand better. Thanks again. <br />Re my question about a single (non-streaming) poll response on the current input value, Turns out the issue has been discussed on github with the littlebits team<br />https://github.com/littlebits/cloud-platform/issues/1<br />but from what I can follow there was, as of that time, still no easy non-streaming option (at least one that works with available python wrappers). <br />Jonathan Conninghttps://www.blogger.com/profile/02677747609952190958noreply@blogger.comtag:blogger.com,1999:blog-909746912855922547.post-64203979891284818642015-07-30T21:58:25.169-07:002015-07-30T21:58:25.169-07:00Unfortunately my cloudBit is not working at this t...Unfortunately my cloudBit is not working at this time, just open a case with LittleBits, so I can't test this out. But I think you just need to specify the encoding for line, i.e. line.encode("utf-8") then Python 3 should work as well. Like a lot of people, I have not transition to Python 3 yet. <br /><br />For the second question about current input, since the data is not stored anywhere, the easiest way I would do is just to cron (set execution of the script at specific time) if you are no Mac or Linux or the equivalent on Windows to execute the script at 4pm daily. Or you can continue to stream the data and write the output to a simple text file (along with timestamp) or simple database. <br /><br />Hope it helps. <br />Eric Chou https://www.blogger.com/profile/11336226720003537549noreply@blogger.comtag:blogger.com,1999:blog-909746912855922547.post-91026443614095706532015-07-29T20:11:45.282-07:002015-07-29T20:11:45.282-07:00Thanks! It worked just as advertised when I ran th...Thanks! It worked just as advertised when I ran the script in python 2 but I wanted to alert you and your readers that when I tried it in python3 I got the following error reading the input :<br /><br /> ---------------------------------------------------------------------------<br /> TypeError Traceback (most recent call last)<br /> in ()<br /> 6 for line in r.iter_lines():<br /> 7 if line:<br /> ----> 8 result = json.loads(line.split('data:')[1])<br /> 9 print(result['payload'])<br /><br /> TypeError: 'str' does not support the buffer interface<br /><br />According to [this stackoverflow answer](http://stackoverflow.com/questions/26945613/str-does-not-support-the-buffer-interface-python3-from-python2) it is because "Python 2, bare literal strings (e.g. 'string') are bytes, whereas in Python 3 they are unicode.". I haven't figured out how to fix that problem quite yet but I'll stick to python2 in the meantime..<br /><br />One more question. Suppose you don't want a constant stream but you just want to poll the device to just find out the value of the current input (say a temperature, or light sensor reading at just 4pm daily). I can think of ways to adapt your code to break out of the for loop, but is there a more elegant way? On the request call you set the parameter stream=True. Do you know if there is a 'non-stream' options?<br /><br />Thanks again!<br />Jonathan Conninghttps://www.blogger.com/profile/02677747609952190958noreply@blogger.com