Parrot Flower Power variables in the Control Page...

Posted on
Sat Apr 05, 2014 10:37 am
angel2014 offline
Posts: 12
Joined: Apr 05, 2014

Parrot Flower Power variables in the Control Page...

I bought a sensor Parrot Flower Power for my lemon tree I have on my terrace. I have seen that there are API that allow you to access the information that the sensor sends to the cloud system of the parrot.
I state that I am not a Python programmer amateur, but I'd like to have a Python script or Applescript that I can to update the variables to be included in my Indigo Control Pages.
On Github I found a sample script that creates files with json sensor information.

https://github.com/parrot-flower-power/ ... pi-example

Can anyone help me to adapt it to use it on Indigo?

Posted on
Sat May 10, 2014 5:44 am
gbiski offline
Posts: 93
Joined: Dec 19, 2012

Re: Parrot Flower Power variables in the Control Page...

Hello,

Did you managed to interface it with Indigo.
I also have one and i cannot interface it.

thanks

Posted on
Sat May 10, 2014 8:18 am
manwithavan offline
User avatar
Posts: 76
Joined: Jan 14, 2014
Location: Melbourne Australia

Re: Parrot Flower Power variables in the Control Page...

I'd be interested to know too! :)

Posted on
Sat May 10, 2014 8:32 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Parrot Flower Power variables in the Control Page...

It does not look too difficult. You have to find a plugin developer that has the device(s) or will buy one...

Is it just an other watering system? What is better about it?

==> you have to promote it a bit!!

Karl

Posted on
Sat May 10, 2014 11:05 am
angel2014 offline
Posts: 12
Joined: Apr 05, 2014

Re: Parrot Flower Power variables in the Control Page...

I am a novice both with Indigo that with Python. I used this example:

https://github.com/parrot-flower-power/ ... pi-example

After several attempts using both python and applescript script that I got of the variables to be used in ControlPage, which is getting updated information from the cloud of Parrot.

Unfortunately I do not connect directly with Indigo devices (do not know the API). I do iApp with the Parrot on the iPhone.

Posted on
Sat May 10, 2014 12:34 pm
gbiski offline
Posts: 93
Joined: Dec 19, 2012

Re: Parrot Flower Power variables in the Control Page...

Hello,

Can you share with us the python and applescript that you have used ( of course without your passwords) ?
I have registered on API parrot and received the key but I did not manage to make it work with the example provided.

Thanks

Posted on
Sat May 10, 2014 3:05 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Parrot Flower Power variables in the Control Page...

before you start using the example code some comments:

from the code:
if not os.path.exists('json'): os.makedirs('json')

will create a directory json at some random place, better (replace yourusername with your user name) :
if not os.path.exists('/Users/yourusername/Documents/json'): os.makedirs('/Users/yourusername/Documents/json')

Also in indigo you should replace

import json

with

import simplejson as json

and some other little things.


Karl

Posted on
Sat May 10, 2014 3:22 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Parrot Flower Power variables in the Control Page...

This code should be a bit better for use with indigo
1. create "json" directory in your user directory ~/Documents
2. change json to simplejson

[EDIT]
3. replaced print with log file entry in indigo
without the device and userid, account .. I can't do more


Karl




changes indicated with ##

Code: Select all
import argparse
import datetime
import urllib, urllib2, os, pwd  ## added pwd
import simplesjon as json  ## changed


parser = argparse.ArgumentParser(prog='test_api',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--client_id',required=True)
parser.add_argument('--client_secret',required=True)
parser.add_argument('--username',required=True)
parser.add_argument('--password',required=True)

args = parser.parse_args()

## new ##
yourMACid   =   pwd.getpwuid( os.getuid() )[ 0 ]


## new ##
if not os.path.exists('/Users/'+yourMACid+'/Documents/json'):   os.makedirs('/Users/'+yourMACid+'/Documents/json')

basename = "https://apiflowerpower.parrot.com"

client_id = args.client_id
client_secret = args.client_secret
username = args.username
password = args.password


oauth_bearer = {}

def URLRequest(url, params, method="GET", headers={}):
   if method == "POST":
      return urllib2.Request(url, headers=headers, data=urllib.urlencode(params))
   else:
      return urllib2.Request(url + "?" + urllib.urlencode(params),headers=headers)


def api_json(url, params, method='GET', headers={}):
   request = URLRequest(basename+url, params, method, headers=headers)
   request.add_header('Accept-Language', 'en_us')
   result = urllib2.urlopen(request)

   # print url
   # print "status", result.getcode()
   # print result.headers

   result_string = result.read()
   return json.loads(result_string)


def test_api_and_dump_to_json(key, url):
   json_data = api_json(url, {}, headers=oauth_bearer, method='GET')
   dump = json.dumps(json_data, indent=4, sort_keys=True)
 
## changed ##   
   dirname = '/Users/'+yourMACid+'/Documents/json/' + username
   if not os.path.exists(dirname):   os.makedirs(dirname)
   
   
   out = open( dirname + "/" + key+'.json','w')
   out.write(dump)
   out.close()
   return json_data
   

# first get OAuth token
result = api_json('/user/v1/authenticate', {'grant_type':'password','client_id':client_id,'client_secret':client_secret,'username':username, 'password':password}, method='POST')
access_token = result['access_token']

oauth_bearer = {'Authorization':'Bearer '+access_token}

# some generic search in the PlantDB
for key,url in (
   ('api-4.02-search-rosa','/search/v5/plants/rosa?generate_index=ASC'),
   ('api-5.01-plants','/plant_library/v1/plant/6677'),
   ('api-5.06-plants','/plant_library/v1/plants/6677,146,405,5671,7548,3461,3462,2394,6003,710'),
   ):
   indigo.server.log( key )
   test_api_and_dump_to_json(key,url)

# user data : sync (garden_locations and sensors) and statuses.
for key,url in (
   ('api-1.25-sync','/sensor_data/v2/sync?include_s3_urls=1'),
   ('api-1.28-status','/sensor_data/v1/garden_locations_status'),
   ('api-3.05-images','/image/v2/location/user_images'),
   ):
   indigo.server.log(key) ## changed
   test_api_and_dump_to_json(key,url)


# get last week samples for each garden_locations.
sync_message = api_json('/sensor_data/v2/sync?include_s3_urls=1', {}, headers=oauth_bearer, method='GET')
for location in sync_message['locations']:
   location_identifier = location['location_identifier']
   indigo.server.log( location_identifier)  ## changed
   last_sample_utc_string = location["last_sample_utc"]
   if last_sample_utc_string:
      last_sample_utc = datetime.datetime.strptime(last_sample_utc_string,'%Y-%m-%d %H:%M:%S UTC')
      from_datetime_utc = (last_sample_utc - datetime.timedelta(days=7)).strftime("%Y-%m-%dT%H:%M:%SZ")
      to_datetime_utc   = last_sample_utc.strftime("%Y-%m-%dT%H:%M:%SZ")
      url = '/sensor_data/v2/sample/location/' + location_identifier + '?from_datetime_utc=' + from_datetime_utc + '&to_datetime_utc=' + to_datetime_utc
      indigo.server.log( url )  ## changed
      test_api_and_dump_to_json('api-1.03-samples-'+location_identifier, url)

Posted on
Sun May 11, 2014 11:10 am
gbiski offline
Posts: 93
Joined: Dec 19, 2012

Re: Parrot Flower Power variables in the Control Page...

Hello,

Thanks for your help.

I created the json folder in my documents folder and i put the file test_api.py containing your modified code.

I am trying to run an applescript from Indigo within action groups containing the following code:

python test_api.py --client_id XXXXX --client_secret XXXXX--username XXXXX --password XXXXX
or test_api.py --client_id XXXXX --client_secret XXXXX--username XXXXX --password XXXXX
where XXXXX are my parrot passwords.

But i am geting errors all the time.

What is my mistake ?

thanks
George

Posted on
Sun May 11, 2014 11:57 am
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Parrot Flower Power variables in the Control Page...

put the python code into an indigo action/server/script (there where you put python or applescript code.

btw: the json directory gets created automatically

replace the args section

delete:===
parser = argparse.ArgumentParser(prog='test_api',formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--client_id',required=True)
parser.add_argument('--client_secret',required=True)
parser.add_argument('--username',required=True)
parser.add_argument('--password',required=True)

args = parser.parse_args()
delete========
## new ##
yourMACid = pwd.getpwuid( os.getuid() )[ 0 ]
## new ##
if not os.path.exists('/Users/'+yourMACid+'/Documents/json'): os.makedirs('/Users/'+yourMACid+'/Documents/json')
basename = "https://apiflowerpower.parrot.com"
# change this:::::::::::::
client_id = "your id here"
client_secret = "your secret here"
username = "yours username here"
password = "your password here"
# change this::::::::::::::
then click on run

Posted on
Sun May 11, 2014 12:15 pm
gbiski offline
Posts: 93
Joined: Dec 19, 2012

Re: Parrot Flower Power variables in the Control Page...

I have done these but when i click run i am getting the error embedded script: No module named argapse

Posted on
Sun May 11, 2014 1:16 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Parrot Flower Power variables in the Control Page...

delete first line in code. all the "arg" things should go

Posted on
Sun May 11, 2014 1:25 pm
gbiski offline
Posts: 93
Joined: Dec 19, 2012

Re: Parrot Flower Power variables in the Control Page...

Now it gives:
import simplesjon as json ## changed

no module named simplesjon

;-(

Posted on
Sun May 11, 2014 1:43 pm
autolog offline
Posts: 3991
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Parrot Flower Power variables in the Control Page...

gbiski wrote:
Now it gives:
import simplesjon as json ## changed

no module named simplesjon

;-(

'simplesjon' should be 'simplejson' I think :)

Posted on
Sun May 11, 2014 1:58 pm
gbiski offline
Posts: 93
Joined: Dec 19, 2012

Re: Parrot Flower Power variables in the Control Page...

Thanks a lot!!!!
It worked!!

I must find a way to put the results on a variable.

Who is online

Users browsing this forum: No registered users and 33 guests