Re: Parrot Flower Power data via API
Posted: Friday 23 September 2016 12:51
Can you point me to a guide on how this API is set up on a Pi?
thanks in advance.
thanks in advance.
Open source Home Automation System
https://forum.domoticz.com/
Please come back and let us know how you get on with this.Justinb81 wrote: I found a new API for their new Flower pot, I'll try to connect to that one and see if I can get communications back up via the BLE dongle so I can get notifications again. IFTTT is broken as well so I might just write and share a Python script for domoticz to get some automation back (watering my garden).
That would be amazing.Justinb81 wrote:I am going to work out a way to send the collected data (the values the bridge sends to the cloud) directly to domoticz as well, Parrot has demonstrated that it will kill off a cloud api without notice when they stop selling a product.
Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
# il faut le module requests
# sudo pip install requests
import requests
from requests.auth import HTTPBasicAuth
from pprint import pformat # here only for aesthetic
#import time
import config_Domoticz
#~~~~~~~~~~ Parametres Domoticz ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
domoticz_ip=config_Domoticz.domoticz_ip
domoticz_port=config_Domoticz.domoticz_port
user=config_Domoticz.domoticz_user
password=config_Domoticz.domoticz_password
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~ Parametres Parrot ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
# First we set our credentials
username = 'email'
password = 'mdp'
# Get your client credentials here: https://apiflowerpower.parrot.com/api_access/signup
client_id = 'email'
client_secret = 'XXXXXXXXXXXXX'
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~ Parametres de la plante ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# c'est le nom que vous avez donne chez Parrot
Nom_Plante='Ficus'
### Parametrage des idx des widget text de Domoticz ##########################
periph_idx= { 'Plante_Lumiere_Status' : 132,
'Plante_Lumiere_Valeur' : 124,
'Plante_Temp_Status' : 135,
'Plante_Temp_Valeur' : 125,
'Plante_Humidite_Status' : 136,
'Plante_Humidite_Valeur' : 126,
'Plante_Engrais_Status' : 137,
'Plante_Engrais_Valeur' : 127,
'Plante_Batterie_Valeur' : 128,
}
##################################################################################
### Option Widget battery ########################################################
# si wbattery = autre chose que 0 on active le widget dédié au niveau de batterie
wbattery=1
##################################################################################
### Mode debug ##################################################################
# si debug = autre chose que 0 on affiche les données recues
debug=1
##################################################################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~FIN DES PARAMETRAGES~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
######
def maj_widget_texte(domoticz_idx,inst_key,battery):
requete='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+domoticz_idx+'&svalue='+inst_key+'&battery='+str(battery)
r=requests.get(requete,auth=HTTPBasicAuth(user,password))
if debug!=0:
#print r
print "Index:"+domoticz_idx+" - Valeur:"+inst_key
#########
######
def maj_widget_alert(domoticz_idx,inst_key1,inst_key2,battery):
if inst_key1 == 'status_ok':
# OK => green
inst_key1='1'
if inst_key1 == 'status_warning':
# warning => orange
inst_key1='3'
if inst_key1 == 'status_critical':
# critical => red
inst_key1='4'
#on rajoute un espace pour une meilleure lisibilité
inst_key2=' '+inst_key2+' '
requete='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+domoticz_idx+'&nvalue='+inst_key1+'&svalue='+inst_key2+'&battery='+str(battery)
r=requests.get(requete,auth=HTTPBasicAuth(user,password))
if debug!=0:
#print r
print "Index:"+domoticz_idx+" - Alert_color:"+inst_key1+" - Valeur:"+inst_key2
req = requests.get('https://apiflowerpower.parrot.com/user/v1/authenticate',
data={'grant_type': 'password',
'username': username,
'password': password,
'client_id': client_id,
'client_secret': client_secret,
})
response = req.json()
if debug!=0:
print ('############# Authentification###############')
print('Server response: {0}'.format(pformat(response)))
print ('##############################################')
# Get authorization token from response
access_token = response['access_token']
auth_header = {'Authorization': 'Bearer {token}'.format(token=access_token)}
# From now on, we won't need initial credentials: access_token and auth_header
# will be enough.
# Get sync data
req = requests.get('https://apiflowerpower.parrot.com/sensor_data/v3/sync',
headers=auth_header)
response = req.json()
if debug!=0:
print ('############# Sensor datas Sync ###############')
print ('Sync sensor data')
print('Server response: {0}'.format(pformat(response)))
print ('##############################################')
sync_locations = response['locations']
#print "SYNC_LOCATIONS"
#print sync_locations
# Build a dict to link location_identifiers to plant name
loc2name = {l['location_identifier']: l['plant_nickname'] for l in sync_locations}
# pour recuperer le niveau de la batterie, il faut lier le numero de serie
# du Flower Parot au nom de la plante choisie afin de relever le bon niveau de batterie
# pour une plante donnée
# Build a second dict to link sensor_serial to plant name
name2serial = {l['plant_nickname']: l['sensor_serial'] for l in sync_locations}
#print "NAME2SERIAL"
#print name2serial
#print "voici le serial du flower power de la plante : "+Nom_Plante
serial=name2serial[Nom_Plante]
#print serial
# Get locations status
req = requests.get('https://apiflowerpower.parrot.com/sensor_data/v3/' +
'garden_locations_status',
headers=auth_header)
response = req.json()
if debug!=0:
print ('############# Garden location Status###############')
print (response)
print ('############# Garden location Status End###############')
#### on recupere le niveau de la batterie du flower power grace au bon serial
ch=response['sensors']
#print ch
serial2battery = {l['sensor_serial']: l['battery_level'] for l in ch}
#print "SERIAL2BATTERY"
#print serial2battery
batt=serial2battery[serial]
#print batt
battery_level=batt['level_percent']
if debug!=0:
print ('-------------------------------------------------------------------')
print "voici le niveau de batterie du flower power de la plante "+Nom_Plante
print battery_level
print ('-------------------------------------------------------------------')
# Si vous souhaitez avoir un widget dédié avec le niveau de la batterie du flower power
if wbattery!=0:
domoticz_idx=str(periph_idx['Plante_Batterie_Valeur'])
maj_widget_texte(domoticz_idx,str(battery_level),battery_level)
status_locations = response['locations']
Nom_Plante=Nom_Plante+":"
for loc in status_locations:
plante=('{plant}:'.format(plant=loc2name[loc['location_identifier']].encode('utf-8')))
print plante
if plante == Nom_Plante:
print('Plante trouvée')
for metric in ['light', 'soil_moisture', 'air_temperature', 'fertilizer']:
data = loc[metric]
if debug!=0:
print ('############# metrics###############')
print(' {metric}:'.format(metric=metric))
#print(' instruction_key: {0}'.format(data['instruction_key']))
print('#####################################')
inst_key=format(data['instruction_key'])
status_key=format(data['status_key'])
if metric == 'light':
domoticz_idx=str(periph_idx['Plante_Lumiere_Status'])
maj_widget_alert(domoticz_idx,status_key,inst_key,battery_level)
z=data['gauge_values']
# Conversion de la valeur en Lux (1 PAR = 53.93 Lux)
valeur=z['current_value']*53.93
valeur="%.2f" % valeur
domoticz_idx=str(periph_idx['Plante_Lumiere_Valeur'])
maj_widget_texte(domoticz_idx,valeur,battery_level)
if metric == 'soil_moisture':
domoticz_idx=str(periph_idx['Plante_Humidite_Status'])
maj_widget_alert(domoticz_idx,status_key,inst_key,battery_level)
z=data['gauge_values']
valeur="%.2f" % z['current_value']
domoticz_idx=str(periph_idx['Plante_Humidite_Valeur'])
maj_widget_texte(domoticz_idx,valeur,battery_level)
if metric == 'air_temperature':
domoticz_idx=str(periph_idx['Plante_Temp_Status'])
maj_widget_alert(domoticz_idx,status_key,inst_key,battery_level)
z=data['gauge_values']
valeur="%.1f" % z['current_value']
domoticz_idx=str(periph_idx['Plante_Temp_Valeur'])
maj_widget_texte(domoticz_idx,valeur,battery_level)
if metric == 'fertilizer':
domoticz_idx=str(periph_idx['Plante_Engrais_Status'])
maj_widget_alert(domoticz_idx,status_key,inst_key,battery_level)
z=data['gauge_values']
valeur="%.1f" % z['current_value']
domoticz_idx=str(periph_idx['Plante_Engrais_Valeur'])
valeur=str("%.1f" % (float(valeur)*100/5))
maj_widget_texte(domoticz_idx,valeur,battery_level)
Code: Select all
pi@raspberrypi:~/domoticz-flower-power $ node FP2DOM.js 9003B7E7E2B1
Processing: 9003B7E7E2B1
module.js:339
throw err;
^
Error: Cannot find module 'requestify'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/home/pi/domoticz-flower-power/FP2DOM.js:12:18)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
I fixed this issue quite easily:terrorsource wrote:Im trying the "domoticz-flower-power" now too.
Getting an error:Which DummyDevices do i need to create in Domoticz and how to fill the value automatically?Code: Select all
pi@raspberrypi:~/domoticz-flower-power $ node FP2DOM.js 9003B7E7E2B1 Processing: 9003B7E7E2B1 module.js:339 throw err; ^ Error: Cannot find module 'requestify' at Function.Module._resolveFilename (module.js:337:15) at Function.Module._load (module.js:287:25) at Module.require (module.js:366:17) at require (module.js:385:17) at Object.<anonymous> (/home/pi/domoticz-flower-power/FP2DOM.js:12:18) at Module._compile (module.js:435:26) at Object.Module._extensions..js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Function.Module.runMain (module.js:467:10)
Can you tell me what Dummy devices i need to create? I guess i need 5 but what types and what names do they need to get.chercheursoft wrote:Hi
you have to set correctly the datas in the file "FP2DOM.js"
especially in the line: idConvertion.set("9003b7e8219c", "19-21-16-17-20");
the above is mine
Regards
I created the devices as explained but still same error.Justinb81 wrote:My forum account got deleted somehow together with all my posts. So the information above is missing the how-to.. Sorry about that.
Let me know if I can help with getting the flowerpowers in domoticz.
Basically you create 3 "General Custom" dummies and 1 "Temp" dummy, these IDX numbers you need to put in the script so that the JS can find them for the specific Bluetooth mac address.
IDX
523 FlowerPower 00082523 1 Kl Dracaena Zon General Custom Sensor 0.1 mol/m²/d - 75
522 FlowerPower 00082522 1 Kl Dracaena Vocht General Custom Sensor 47.38 % - 75
509 FlowerPower 00082509 1 Kl Dracaena EC General Custom Sensor 1292 µS/cm - 75
508 FlowerPower 1424C 1 Kl Dracaena Lucht T Temp LaCrosse TX3 21.0 C - 75