HomeMatic CCU2 bei ELV bestellen

Netatmo Wetterstation (incl. CO2 Messung) einbinden

Problemlösungen und Hinweise von allgemeinem Interesse zur Haussteuerung mit HomeMatic

Werbung


Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon SirDrinksAlot » 05.12.2017, 17:58

Tach

Habe den script in ne textdatei kopiert un persönliche Daten mit x ersetzt.

lg
Dateianhänge
2.jpg
Screenshot 2
1.jpg
Screenshot 1
Script.txt
Script
(11.33 KiB) 9-mal heruntergeladen
SirDrinksAlot
 
Beiträge: 5
Registriert: 05.12.2017, 17:14

Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon Indigo » 05.12.2017, 20:50

Was für Module hast du denn?
Solltest du keinen Regensensor oder Windsensor haben muss der jeweils der betreffende Teil aus dem Skript gelöscht werden.
Und wenn du die Teile hast: sind die Variablen dafür angelegt?
Indigo
 
Beiträge: 300
Registriert: 10.02.2015, 20:32

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon SirDrinksAlot » 05.12.2017, 21:08

Das war es. Habe kein Regen und Windmodul. Hatte beim ersten mal alles gelöscht aber es hat trotzdem nicht funktioniert. Habe dann den Script neu angelegt und vergessen zu löschen.
Jetzt nach dem löschen funktioniert es erstaunlicherweise.

vielen Dank für die Hilfe

lg
SirDrinksAlot
 
Beiträge: 5
Registriert: 05.12.2017, 17:14

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon timo-n » 06.12.2017, 03:30

Tag zusammen,

hab heute auch meine Netatmo bekommen. Ich versuch nun schon ne ganze weile die Netatmo in die CCU zu Integrieren aber es will einfach nicht.

Hab geräte schon 3x angelegt, SVs überprüft alles ohne erfolg. Dann irgendwann bemerkt das die Formatierung der tcl auch nicht gepasst hat.
Dann alle 50 Seiten dieses Threads durchgelesen und das gefühl bekommen es geht bei allen nur bei mir nicht :lol:

Dann hatte ich die netatmo.tcl per ssh "gestartet"? Und als fehler kam dann die Meldung:

Befehl '"./netatmo.tcl"'
fehlgeschlagen mit Beendigungscode 2 und Fehlernachricht
./netatmo.tcl: line 3: load: not found
./netatmo.tcl: line 20: syntax error: unexpected "(".


So langsam verlier ich die geduld aber das wird auch an der Uhrzeit liegen :roll:

Kann mir hier denn jemand helfen?

Danke schonmal :-)

Code: Alles auswählen
#!/bin/tclsh

 load tclrega.so

  #---------------------------------------------------------------------------------------------------------------#
  #                         CONFIG                           #
  #---------------------------------------------------------------------------------------------------------------#
  # to obtain your own client ID and API key please register a new app here: http://dev.netatmo.com/dev/listapps
  set clientId "meine-clientid"         
  set clientSecret "mein-client secret"
  # the following are your normal netatmo credentials (the ones you used to setup your netatmo weather station)
  set username "meine email"
  set password "mein passwort"
  # the following are MAC addresses of your indoor station and the outside module and rain module
  set deviceid "70:ee:50:2a:e8:f2"
  set moduleid "02:00:00:2c:ad:b4"
  #set rainid "XX:XX:XX:XX:XX:XX"
  #set windid "XX:XX:XX:XX:XX:XX"

  set ::env(LD_LIBRARY_PATH) "/usr/local/addons/cuxd"
  set cfgfile "/tmp/netatmo.dat"
  set logtag "netatmo.tcl"
  set logfacility "local1"
  # 0=panic, 1=alert 2=crit 3=err 4=warn 5=notice 6=info 7=debug
  set loglevel 6
  #---------------------------------------------------------------------------------------------------------------#

  #---------------------------------------------------------------------------------------------------------------#
  #                       GLOBAL VARIABLES                                #
  #---------------------------------------------------------------------------------------------------------------#
  set accesstoken ""
  set refreshtoken ""
  set tokenexpires 0
  #---------------------------------------------------------------------------------------------------------------#


  set loglevels {panic alert crit err warn notice info debug}

  proc log {lvl msg} {
    global logtag
    global logfacility
    global loglevel
    global loglevels
   
    set lvlnum [lsearch $loglevels $lvl]
   
    if {$lvlnum <= $loglevel} {
      if {$lvlnum <= 3} {
        catch {exec logger -s -t $logtag -p $logfacility.$lvl $msg}
      } else {
        puts "$lvl: $msg"
        catch {exec logger -t $logtag -p $logfacility.$lvl $msg}
      }
     
    }
   
  }

  proc refreshToken {rt ci cs} {
    log debug "refreshing token"
    set url "https://api.netatmo.net/oauth2/token"
    set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
    set parameter "grant_type=refresh_token&refresh_token=$rt&client_id=$ci&client_secret=$cs"

    catch {exec /usr/local/addons/cuxd/curl -k -i -H $header -X POST -d $parameter $url} response
    log debug "response was $response"
   
    return $response

  }

  proc requestToken {ci cs un pw} {
    log "debug" "requesting new token"
    set url "https://api.netatmo.net/oauth2/token"
    set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
    set parameter "grant_type=password&client_id=$ci&client_secret=$cs&username=$un&password=$pw"

    catch {exec /usr/local/addons/cuxd/curl -k -i -H $header -X POST -d $parameter $url} response
    log debug "response was $response"
   
    return $response

  }

  proc parseOAuthResponse {input} {
    log debug "parsing authentication result"
    global accesstoken
    global refreshtoken
   
    regexp {HTTP/1.1\s(\d*)} $input dummy returncode
    regexp {\"access_token\":\"(.*?)\"} $input dummy accesstoken
    regexp {\"refresh_token\":\"(.*?)\"} $input dummy refreshtoken
    regexp {\"expires_in\":(.*?)\,} $input dummy expiresin

    log debug "returncode is $returncode"
    log debug "access token is $accesstoken"
    log debug "refresh token is $refreshtoken"
    log debug "expires in $expiresin"
   
    if {[expr $returncode]!=200} {
        log error "Authentication failed with code $returncode and response $input"
        exit 1
      }
   
    return $expiresin
  }

  proc saveAccessToken {expin} {
    global accesstoken
    global refreshtoken
    global tokenexpires
    global cfgfile
     
    log debug "saving new access token to $cfgfile"
   
    set fileId [open $cfgfile "w"]
   
    set now [clock seconds]
    set tokenexpires [expr $now + $expin]
   
    puts $fileId $accesstoken
    puts $fileId $refreshtoken
    puts $fileId $tokenexpires
    close $fileId
  }

  proc loadAccessToken {} {
    global accesstoken
    global refreshtoken
    global tokenexpires
    global cfgfile
   
    log debug "loading stored credentials from $cfgfile"
   
    set fp [open $cfgfile r]
    set file_data [read $fp]
    close $fp
   
    log debug "file data is: $file_data"
   
    set data [split $file_data "\n"]
   
    set accesstoken [lindex $data 0]
    set refreshtoken [lindex $data 1]
    set tokenexpires [lindex $data 2]
  }

  log debug "script has started"

  if { [file exists $cfgfile] == 1} {
    log info "found stored credentials"
      loadAccessToken
      set now [clock seconds]
      log debug "current time is [clock format $now -format "%Y-%m-%dT%H:%M:%S"], token is valid until [clock format $tokenexpires -format "%Y-%m-%dT%H:%M:%S"]"
      if {[expr $now >= $tokenexpires] == 1} {
        log notice "token has already expired"
        saveAccessToken [parseOAuthResponse [refreshToken $refreshtoken $clientId $clientSecret]]
        log notice "oauth token successfully refreshed"
      } else {
        log info "token is still valid"
      }
    } else {
      log warn "no stored credentials found"
      saveAccessToken [parseOAuthResponse [requestToken $clientId $clientSecret $username $password]]
      log notice "oauth token successfully initialized"
    }

  log debug "polling outdoor module..."
  set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$moduleid&scale=max&type=Temperature,Humidity&date_end=last"
  log debug "querying $url"
  catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
  log debug "response is: $response"

  regexp {\"value\":\[\[(.*?),(.*?)\]} $response dummy otemp ohum

  log info "Outside temperature is $otemp"
  log info "Outside humidity is $ohum"

  log debug "polling main module..."
  set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&scale=max&type=Temperature,Humidity,CO2,Pressure,Noise&date_end=last"
  log debug "querying $url"
  catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
  log debug "response is: $response"

  regexp {\"value\":\[\[(.*?),(.*?),(.*?),(.*?),(.*?)\]} $response dummy itemp ihum ico2 ipressure inoise
 
  log info "LogI is $response"       
  log info "Inside temperature is $itemp"
  log info "Inside humidity is $ihum"
  log info "Inside CO2 level $ico2"
  log info "Inside pressure is $ipressure"
  log info "Inside noise level is $inoise"

  log debug "polling regensensor module...1d"
  set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$rainid&scale=1day&type=sum_rain&date_end=last"
  log debug "querying $url"
  catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
  log debug "response is: $response"

  regexp {\"value\":\[\[(.*?)\]} $response dummy rain1d
 
  log info "LogR is $response"       
  log info "Outside Regen1d is $rain1d"

  log debug "polling regensensor module...30min"
  set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$rainid&scale=30min&type=Rain,sum_rain&date_end=last"
  log debug "querying $url"
  catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
  log debug "response is: $response"

  regexp {\"value\":\[\[(.*?),(.*?)\]} $response dummy rain2 rain30min
 
  log info "LogR is $response"       
  log info "Outside rain2 is $rain2"
  log info "Outside Regen30min is $rain30min"

  log debug "polling wind module..."
  set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$windid&scale=max&type=WindAngle,WindStrength,GustAngle,GustStrength&date_end=last"
 log debug "quering $url"
 catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
 log debug "respnose is $response"

 regexp {\"value\":\[\[(.*?),(.*?),(.*?),(.*?)\]} $response dummy windangle windstrength gustangle guststrength
 log info "WindAngle is $windangle"
 log info "Windstrength is $windstrength"
 log info "GustAngle is $gustangle"
 log info "Guststrength is $guststrength"


  #
  # set ReGaHss variables
  #
  set rega_cmd ""
  append rega_cmd "var ITemp = dom.GetObject('CUxD.CUX9002001:1.SET_TEMPERATURE');"
  append rega_cmd "var IHumi = dom.GetObject('CUxD.CUX9002001:1.SET_HUMIDITY');"
  append rega_cmd "var OTemp = dom.GetObject('CUxD.CUX9002002:1.SET_TEMPERATURE');"
  append rega_cmd "var OHumi = dom.GetObject('CUxD.CUX9002002:1.SET_HUMIDITY');"
  append rega_cmd "var IPress = dom.GetObject('Luftdruck');"
  append rega_cmd "var ICO2 = dom.GetObject('CO2');"
  append rega_cmd "var INoise = dom.GetObject('Sonometer');"
  append rega_cmd "OTemp.State('$otemp');"
  append rega_cmd "OHumi.State('$ohum');"
  append rega_cmd "ITemp.State('$itemp');"
  append rega_cmd "IHumi.State('$ihum');"
  append rega_cmd "IPress.State('$ipressure');"
  append rega_cmd "ICO2.State('$ico2');"
  append rega_cmd "INoise.State('$inoise');"
  append rega_cmd "var sdatetime = system.Date('%d.%m.%Y %H:%M:%S');"
  append rega_cmd "var netatmosynctime = dom.GetObject('SyncTime');"
  append rega_cmd "netatmosynctime.Variable(sdatetime.ToString());"
  rega_script $rega_cmd
timo-n
 
Beiträge: 21
Registriert: 23.10.2015, 19:13
Wohnort: Stuttgart

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon Bulli » 06.12.2017, 12:11

Hallo
ich tippe mal auf Schmutzzeichen in deinen Script. Mach mal dso2unix auf die Datei.
Welchen Editor benutz du?

Folgende Zeilen kannst du noch alle löschen wenn du kein Wind+Regenmodul hast.
Code: Alles auswählen
        log debug "polling regensensor module...1d"
        set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$rainid&scale=1day&type=sum_rain&date_end=last"
        log debug "querying $url"
        catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
        log debug "response is: $response"

        regexp {\"value\":\[\[(.*?)\]} $response dummy rain1d

        log info "LogR is $response"
        log info "Outside Regen1d is $rain1d"

        log debug "polling regensensor module...30min"
        set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$rainid&scale=30min&type=Rain,sum_rain&date_end=last"
        log debug "querying $url"
        catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
        log debug "response is: $response"

        regexp {\"value\":\[\[(.*?),(.*?)\]} $response dummy rain2 rain30min

        log info "LogR is $response"
        log info "Outside rain2 is $rain2"
        log info "Outside Regen30min is $rain30min"

        log debug "polling wind module..."
        set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$windid&scale=max&type=WindAngle,WindStrength,GustAngle,GustStrength&date_end=last"
       log debug "quering $url"
       catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
       log debug "respnose is $response"

       regexp {\"value\":\[\[(.*?),(.*?),(.*?),(.*?)\]} $response dummy windangle windstrength gustangle guststrength
       log info "WindAngle is $windangle"
       log info "Windstrength is $windstrength"
       log info "GustAngle is $gustangle"
       log info "Guststrength is $guststrength"


dos2unix z.B so
viewtopic.php?f=31&t=12876&start=470#p386773 und 2 Beiträge darunter...

Gruß
Bulli
Bulli
 
Beiträge: 364
Registriert: 29.04.2014, 17:38

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon timo-n » 06.12.2017, 15:27

Ich nutz Notepad++ aber egal wie ich es mach es kommt immer ne andere Fehlermeldung. Nun hatte ich die tcl neu gemacht nun hab ich wieder diese komischen As die wohl mit der Formatierung zusammenhängen.
Inwzischen UltraEdit auch probiert aber die Meldungen werden immer komischer...

Denke die Netatmo kann bald die Temperatur in der Mülltonne messen... :oops:

Im Terminal kommt:

Befehl '"./netatmo.tcl"'
fehlgeschlagen mit Beendigungscode 1 und Fehlernachricht
invalid command name " "
while executing
"       "
(procedure "log" line 6)
invoked from within
"log debug "script has started""
(file "./netatmo.tcl" line 138).



Code: Alles auswählen
#!/bin/tclsh

load tclrega.so
#---------------------------------------------------------------------------------------------------------------#
#                                                   CONFIG                                                      #
#---------------------------------------------------------------------------------------------------------------#
#  to obtain your own client ID and API key please register a new app here: http://dev.netatmo.com/dev/listapps
set clientId meineclientid
set clientSecret meinclientsecret
#  the following are your normal netatmo credentials (the ones you used to setup your netatmo weather station)
set username "email"
set password "passwort"
#  the following are MAC addresses of your indoor station and the outside module and rain module
set deviceid "70:ee:50:2a:e8:f2"
set moduleid "02:00:00:2c:ad:b4"
#set rainid "XX:XX:XX:XX:XX:XX"
#set windid "XX:XX:XX:XX:XX:XX"

set ::env(LD_LIBRARY_PATH) "/usr/local/addons/cuxd"
set cfgfile "/tmp/netatmo.dat"
set logtag "netatmo.tcl"
set logfacility "local1"
#0=panic, 1=alert 2=crit 3=err 4=warn 5=notice 6=info 7=debug
set loglevel 6
#---------------------------------------------------------------------------------------------------------------#

#---------------------------------------------------------------------------------------------------------------#
#                                              GLOBAL VARIABLES                                                                #
#---------------------------------------------------------------------------------------------------------------#
set accesstoken ""
set refreshtoken ""
set tokenexpires 0
#---------------------------------------------------------------------------------------------------------------#


set loglevels {panic alert crit err warn notice info debug}

proc log {lvl msg} {
global logtag
global logfacility
global loglevel
global loglevels
       
set lvlnum [lsearch $loglevels $lvl]
       
if {$lvlnum <= $loglevel} {
if {$lvlnum <= 3} {
catch {exec logger -s -t $logtag -p $logfacility.$lvl $msg}
} else {
puts "$lvl: $msg"
catch {exec logger -t $logtag -p $logfacility.$lvl $msg}
}
}
}
proc refreshToken {rt ci cs} {
log debug "refreshing token"
set url "https://api.netatmo.net/oauth2/token"
set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
set parameter "grant_type=refresh_token&refresh_token=$rt&client_id=$ci&client_secret=$cs"
catch {exec /usr/local/addons/cuxd/curl -k -i -H $header -X POST -d $parameter $url} response
log debug "response was $response"
return $response
}
proc requestToken {ci cs un pw} {
log "debug" "requesting new token"
set url "https://api.netatmo.net/oauth2/token"
set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
set parameter "grant_type=password&client_id=$ci&client_secret=$cs&username=$un&password=$pw"

catch {exec /usr/local/addons/cuxd/curl -k -i -H $header -X POST -d $parameter $url} response
log debug "response was $response"

return $response
}

proc parseOAuthResponse {input} {
log debug "parsing authentication result"
global accesstoken
global refreshtoken

regexp {HTTP/1.1\s(\d*)} $input dummy returncode
regexp {\"access_token\":\"(.*?)\"} $input dummy accesstoken
regexp {\"refresh_token\":\"(.*?)\"} $input dummy refreshtoken
regexp {\"expires_in\":(.*?)\,} $input dummy expiresin

log debug "returncode is $returncode"
log debug "access token is $accesstoken"
log debug "refresh token is $refreshtoken"
log debug "expires in $expiresin"

if {[expr $returncode]!=200} {
log error "Authentication failed with code $returncode and response $input"
exit 1
}

return $expiresin
}

proc saveAccessToken {expin} {
global accesstoken
global refreshtoken
global tokenexpires
global cfgfile

log debug "saving new access token to $cfgfile"

set fileId [open $cfgfile "w"]
set now [clock seconds]
set tokenexpires [expr $now + $expin]

puts $fileId $accesstoken
puts $fileId $refreshtoken
puts $fileId $tokenexpires
close $fileId
}

proc loadAccessToken {} {
global accesstoken
global refreshtoken
global tokenexpires
global cfgfile

log debug "loading stored credentials from $cfgfile"

set fp [open $cfgfile r]
set file_data [read $fp]
close $fp

log debug "file data is: $file_data"

set data [split $file_data "\n"]   
       
set accesstoken [lindex $data 0]
set refreshtoken [lindex $data 1]
set tokenexpires [lindex $data 2]
}

log debug "script has started"

if { [file exists $cfgfile] == 1} {
log info "found stored credentials"
loadAccessToken
set now [clock seconds]
log debug "current time is [clock format $now -format "%Y-%m-%dT%H:%M:%S"], token is valid until [clock format $tokenexpires -format "%Y-%m-%dT%H:%M:%S"]"
if {[expr $now >= $tokenexpires] == 1} {
log notice "token has already expired"
saveAccessToken [parseOAuthResponse [refreshToken $refreshtoken $clientId $clientSecret]]
log notice "oauth token successfully refreshed"
} else {
log info "token is still valid"
}
} else {
log warn "no stored credentials found"
saveAccessToken [parseOAuthResponse [requestToken $clientId $clientSecret $username $password]]
log notice "oauth token successfully initialized"
}

log debug "polling outdoor module..."
set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$moduleid&scale=max&type=Temperature,Humidity&date_end=last"
log debug "querying $url"
catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
log debug "response is: $response"

regexp {\"value\":\[\[(.*?),(.*?)\]} $response dummy otemp ohum

log info "Outside temperature is $otemp"
log info "Outside humidity is $ohum"

log debug "polling main module..."
set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&scale=max&type=Temperature,Humidity,CO2,Pressure,Noise&date_end=last"
log debug "querying $url"
catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
log debug "response is: $response"

regexp {\"value\":\[\[(.*?),(.*?),(.*?),(.*?),(.*?)\]} $response dummy itemp ihum ico2 ipressure inoise
log info "LogI is $response"             
log info "Inside temperature is $itemp"
log info "Inside humidity is $ihum"
log info "Inside CO2 level $ico2"
log info "Inside pressure is $ipressure"
log info "Inside noise level is $inoise"

#
# set ReGaHss variables
#
set rega_cmd ""
append rega_cmd "var ITemp = dom.GetObject('CUxD.CUX9002001:1.SET_TEMPERATURE');"
append rega_cmd "var IHumi = dom.GetObject('CUxD.CUX9002001:1.SET_HUMIDITY');"
append rega_cmd "var OTemp = dom.GetObject('CUxD.CUX9002002:1.SET_TEMPERATURE');"
append rega_cmd "var OHumi = dom.GetObject('CUxD.CUX9002002:1.SET_HUMIDITY');"
append rega_cmd "var Rain1 = dom.GetObject('Regenmenge_30min');"
append rega_cmd "var Rain2 = dom.GetObject('Regenmenge_1d');"
append rega_cmd "var Rain3 = dom.GetObject('Regen_aktuell');"
append rega_cmd "var IPress = dom.GetObject('Luftdruck');"
append rega_cmd "var ICO2 = dom.GetObject('CO2');"
append rega_cmd "var INoise = dom.GetObject('Sonometer');"
append rega_cmd "OTemp.State('$otemp');"
append rega_cmd "OHumi.State('$ohum');"
append rega_cmd "ITemp.State('$itemp');"
append rega_cmd "IHumi.State('$ihum');"
append rega_cmd "IPress.State('$ipressure');"
append rega_cmd "ICO2.State('$ico2');"
append rega_cmd "INoise.State('$inoise');"
append rega_cmd "var sdatetime = system.Date('%d.%m.%Y %H:%M:%S');"
append rega_cmd "var netatmosynctime = dom.GetObject('SyncTime');"
append rega_cmd "netatmosynctime.Variable(sdatetime.ToString());"
rega_script $rega_cmd
timo-n
 
Beiträge: 21
Registriert: 23.10.2015, 19:13
Wohnort: Stuttgart

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon SirDrinksAlot » 06.12.2017, 16:48

Hatte anfangs auch solche fehler. Hab dann das Script direkt aus der Anleitung in den Editor von Winscp eingefügt statt notepad und dann funktionierte es.

lg
SirDrinksAlot
 
Beiträge: 5
Registriert: 05.12.2017, 17:14

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon timo-n » 06.12.2017, 20:26

Ja es muss irgendwie an der Formatierung liegen. Habs jetzt mal direkt über WinSCP eingefügt aber wenn ich nun die tcl ausführe kommt gar keine Meldung mehr. Aber ich seh bei den Geräten auch keine Werte sondern nur Nullen. Mich verlässt langsam die Lust nach zig versuchen... Wollte eigentlich nur in 2 Räumen die CO2 Werte sehen :roll:

Nun kommt:

Befehl '"./netatmo.tcl"'
fehlgeschlagen mit Beendigungscode 1 und Fehlernachricht
invalid command name " "
while executing
"    global logtag"
(procedure "log" line 2)
invoked from within
"log debug "script has started""
(file "./netatmo.tcl" line 144).


Code: Alles auswählen
#!/bin/tclsh

load tclrega.so

#---------------------------------------------------------------------------------------------------------------#
#                                                   CONFIG                                                      #
#---------------------------------------------------------------------------------------------------------------#
#  to obtain your own client ID and API key please register a new app here: http://dev.netatmo.com/dev/listapps
set clientId "MeineID"
set clientSecret "MeinSecret"
#  the following are your normal netatmo credentials (the ones you used to setup your netatmo weather station)
set username "MeineEmail"
set password "NetatmoPasswort"
#  the following are MAC addresses of your indoor station and the outside module
set deviceid "70:ee:50:2a:e8:f2"
set moduleid "02:00:00:2c:ad:b4"
set cfgfile "/tmp/netatmo.dat"
set logtag "netatmo.tcl"
set logfacility "local1"
# 0=panic, 1=alert 2=crit 3=err 4=warn 5=notice 6=info 7=debug
set loglevel 6
#---------------------------------------------------------------------------------------------------------------#

#---------------------------------------------------------------------------------------------------------------#
#                                              GLOBAL VARIABLES                                                                #
#---------------------------------------------------------------------------------------------------------------#
set accesstoken ""
set refreshtoken ""
set tokenexpires 0
#---------------------------------------------------------------------------------------------------------------#


set loglevels {panic alert crit err warn notice info debug}

proc log {lvl msg} {
    global logtag
    global logfacility
    global loglevel
    global loglevels
   
    set lvlnum [lsearch $loglevels $lvl]
   
    if {$lvlnum <= $loglevel} {
        if {$lvlnum <= 3} {
            catch {exec logger -s -t $logtag -p $logfacility.$lvl $msg}
        } else {
            puts "$lvl: $msg"
            catch {exec logger -t $logtag -p $logfacility.$lvl $msg}
        }
       
    }
   
}

proc refreshToken {rt ci cs} {
    log debug "refreshing token"
    set url "https://api.netatmo.net/oauth2/token"
    set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
    set parameter "grant_type=refresh_token&refresh_token=$rt&client_id=$ci&client_secret=$cs"

    catch {exec curl -k -i -H $header -X POST -d $parameter $url} response
    log debug "response was $response"
   
    return $response

}

proc requestToken {ci cs un pw} {
    log "debug" "requesting new token"
    set url "https://api.netatmo.net/oauth2/token"
    set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
    set parameter "grant_type=password&client_id=$ci&client_secret=$cs&username=$un&password=$pw"

    catch {exec curl -k -i -H $header -X POST -d $parameter $url} response
    log debug "response was $response"
   
    return $response

}

proc parseOAuthResponse {input} {
    log debug "parsing authentication result"
    global accesstoken
    global refreshtoken
   
    regexp {HTTP/1.1\s(\d*)} $input dummy returncode
    regexp {\"access_token\":\"(.*?)\"} $input dummy accesstoken
    regexp {\"refresh_token\":\"(.*?)\"} $input dummy refreshtoken
    regexp {\"expires_in\":(.*?)\,} $input dummy expiresin

    log debug "returncode is $returncode"
    log debug "access token is $accesstoken"
    log debug "refresh token is $refreshtoken"
    log debug "expires in $expiresin"
   
    if {[expr $returncode]!=200} {
            log error "Authentication failed with code $returncode and response $input"
            exit 1
        }
   
    return $expiresin
}

proc saveAccessToken {expin} {
    global accesstoken
    global refreshtoken
    global tokenexpires
    global cfgfile
       
    log debug "saving new access token to $cfgfile"
   
    set fileId [open $cfgfile "w"]
   
    set now [clock seconds]
    set tokenexpires [expr $now + $expin]
   
    puts $fileId $accesstoken
    puts $fileId $refreshtoken
    puts $fileId $tokenexpires
    close $fileId
}

proc loadAccessToken {} {
    global accesstoken
    global refreshtoken
    global tokenexpires
    global cfgfile
   
    log debug "loading stored credentials from $cfgfile"
   
    set fp [open $cfgfile r]
    set file_data [read $fp]
    close $fp
   
    log debug "file data is: $file_data"
   
    set data [split $file_data "\n"]   
   
    set accesstoken [lindex $data 0]
    set refreshtoken [lindex $data 1]
    set tokenexpires [lindex $data 2]
}

log debug "script has started"

if { [file exists $cfgfile] == 1} {
    log info "found stored credentials"
        loadAccessToken
        set now [clock seconds]
        log debug "current time is [clock format $now -format "%Y-%m-%dT%H:%M:%S"], token is valid until [clock format $tokenexpires -format "%Y-%m-%dT%H:%M:%S"]"
        if {[expr $now >= $tokenexpires] == 1} {
            log notice "token has already expired"
            saveAccessToken [parseOAuthResponse [refreshToken $refreshtoken $clientId $clientSecret]]
            log notice "oauth token successfully refreshed"
        } else {
            log info "token is still valid"
        }
    } else {
        log warn "no stored credentials found"
        saveAccessToken [parseOAuthResponse [requestToken $clientId $clientSecret $username $password]]
        log notice "oauth token successfully initialized"
    }

log debug "polling outdoor module..."
set url "http://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$moduleid&scale=max&type=Temperature,Humidity&date_end=last"
log debug "querying $url"
catch {exec curl -# $url} response
log debug "response is: $response"

regexp {\"value\":\[\[(.*?),(.*?)\]} $response dummy otemp ohum
                   
log info "Outside temperature is $otemp"
log info "Outside humidity is $ohum"

log debug "polling main module..."
set url "http://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&scale=max&type=Temperature,Humidity,CO2,Pressure,Noise&date_end=last"
log debug "querying $url"
catch {exec curl -# $url} response
log debug "response is: $response"

regexp {\"value\":\[\[(.*?),(.*?),(.*?),(.*?),(.*?)\]} $response dummy itemp ihum ico2 ipressure inoise
                   
log info "Inside temperature is $itemp"
log info "Inside humidity is $ihum"
log info "Inside CO2 level $ico2"
log info "Inside pressure is $ipressure"
log info "Inside noise level is $inoise"

#
# set ReGaHss variables
#
set rega_cmd ""
append rega_cmd "var ITemp = dom.GetObject('CUxD.CUX9002001:1.SET_TEMPERATURE');"
append rega_cmd "var IHumi = dom.GetObject('CUxD.CUX9002001:1.SET_HUMIDITY');"
append rega_cmd "var OTemp = dom.GetObject('CUxD.CUX9002002:1.SET_TEMPERATURE');"
append rega_cmd "var OHumi = dom.GetObject('CUxD.CUX9002002:1.SET_HUMIDITY');"
append rega_cmd "var IPress = dom.GetObject('Luftdruck');"
append rega_cmd "var ICO2 = dom.GetObject('CO2');"
append rega_cmd "var INoise = dom.GetObject('Sonometer');"
append rega_cmd "OTemp.State('$otemp');"
append rega_cmd "OHumi.State('$ohum');"
append rega_cmd "ITemp.State('$itemp');"
append rega_cmd "IHumi.State('$ihum');"
append rega_cmd "IPress.State('$ipressure');"
append rega_cmd "ICO2.State('$ico2');"
append rega_cmd "INoise.State('$inoise');"
append rega_cmd "var sdatetime = system.Date('%d.%m.%Y %H:%M:%S');"
append rega_cmd "var netatmosynctime = dom.GetObject('SyncTime');"
append rega_cmd "netatmosynctime.Variable(sdatetime.ToString());"
rega_script $rega_cmd
timo-n
 
Beiträge: 21
Registriert: 23.10.2015, 19:13
Wohnort: Stuttgart

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon SirDrinksAlot » 06.12.2017, 21:57

Weiß nicht obs hilft aber kopier mal den script so in den WinSCP editor. Funktioniert bei mir einwandfrei. Und gib deine Daten ohne " ein.

lg



Code: Alles auswählen
            #!/bin/tclsh

            load tclrega.so

            #---------------------------------------------------------------------------------------------------------------#
            #                                                   CONFIG                                                      #
            #---------------------------------------------------------------------------------------------------------------#
            #  to obtain your own client ID and API key please register a new app here: http://dev.netatmo.com/dev/listapps
            set clientId                     
            set clientSecret
            #  the following are your normal netatmo credentials (the ones you used to setup your netatmo weather station)
            set username
            set password
#           the following are MAC addresses of your indoor station and the outside module and rain module
            set deviceid
            set moduleid
           
            set ::env(LD_LIBRARY_PATH) "/usr/local/addons/cuxd"
            set cfgfile "/tmp/netatmo.dat"
            set logtag "netatmo.tcl"
            set logfacility "local1"
            # 0=panic, 1=alert 2=crit 3=err 4=warn 5=notice 6=info 7=debug
            set loglevel 7
            #---------------------------------------------------------------------------------------------------------------#

            #---------------------------------------------------------------------------------------------------------------#
            #                                              GLOBAL VARIABLES                                                                #
            #---------------------------------------------------------------------------------------------------------------#
            set accesstoken ""
            set refreshtoken ""
            set tokenexpires 0
            #---------------------------------------------------------------------------------------------------------------#


           
    set loglevels {panic alert crit err warn notice info debug}

    proc log {lvl msg} {
        global logtag
        global logfacility
        global loglevel
        global loglevels
       
        set lvlnum [lsearch $loglevels $lvl]
       
        if {$lvlnum <= $loglevel} {
            if {$lvlnum <= 3} {
                catch {exec logger -s -t $logtag -p $logfacility.$lvl $msg}
            } else {
                puts "$lvl: $msg"
                catch {exec logger -t $logtag -p $logfacility.$lvl $msg}
            }
           
        }
       
    }

    proc refreshToken {rt ci cs} {
        log debug "refreshing token"
        set url "https://api.netatmo.net/oauth2/token"
        set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        set parameter "grant_type=refresh_token&refresh_token=$rt&client_id=$ci&client_secret=$cs"

        catch {exec /usr/local/addons/cuxd/curl -k -i -H $header -X POST -d $parameter $url} response
        log debug "response was $response"
       
        return $response

    }

    proc requestToken {ci cs un pw} {
        log "debug" "requesting new token"
        set url "https://api.netatmo.net/oauth2/token"
        set header "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        set parameter "grant_type=password&client_id=$ci&client_secret=$cs&username=$un&password=$pw"

        catch {exec /usr/local/addons/cuxd/curl -k -i -H $header -X POST -d $parameter $url} response
        log debug "response was $response"
       
        return $response

    }

    proc parseOAuthResponse {input} {
        log debug "parsing authentication result"
        global accesstoken
        global refreshtoken
       
        regexp {HTTP/1.1\s(\d*)} $input dummy returncode
        regexp {\"access_token\":\"(.*?)\"} $input dummy accesstoken
        regexp {\"refresh_token\":\"(.*?)\"} $input dummy refreshtoken
        regexp {\"expires_in\":(.*?)\,} $input dummy expiresin

        log debug "returncode is $returncode"
        log debug "access token is $accesstoken"
        log debug "refresh token is $refreshtoken"
        log debug "expires in $expiresin"
       
        if {[expr $returncode]!=200} {
                log error "Authentication failed with code $returncode and response $input"
                exit 1
            }
       
        return $expiresin
    }

    proc saveAccessToken {expin} {
        global accesstoken
        global refreshtoken
        global tokenexpires
        global cfgfile
           
        log debug "saving new access token to $cfgfile"
       
        set fileId [open $cfgfile "w"]
       
        set now [clock seconds]
        set tokenexpires [expr $now + $expin]
       
        puts $fileId $accesstoken
        puts $fileId $refreshtoken
        puts $fileId $tokenexpires
        close $fileId
    }

    proc loadAccessToken {} {
        global accesstoken
        global refreshtoken
        global tokenexpires
        global cfgfile
       
        log debug "loading stored credentials from $cfgfile"
       
        set fp [open $cfgfile r]
        set file_data [read $fp]
        close $fp
       
        log debug "file data is: $file_data"
       
        set data [split $file_data "\n"]   
       
        set accesstoken [lindex $data 0]
        set refreshtoken [lindex $data 1]
        set tokenexpires [lindex $data 2]
    }

    log debug "script has started"

    if { [file exists $cfgfile] == 1} {
        log info "found stored credentials"
            loadAccessToken
            set now [clock seconds]
            log debug "current time is [clock format $now -format "%Y-%m-%dT%H:%M:%S"], token is valid until [clock format $tokenexpires -format "%Y-%m-%dT%H:%M:%S"]"
            if {[expr $now >= $tokenexpires] == 1} {
                log notice "token has already expired"
                saveAccessToken [parseOAuthResponse [refreshToken $refreshtoken $clientId $clientSecret]]
                log notice "oauth token successfully refreshed"
            } else {
                log info "token is still valid"
            }
        } else {
            log warn "no stored credentials found"
            saveAccessToken [parseOAuthResponse [requestToken $clientId $clientSecret $username $password]]
            log notice "oauth token successfully initialized"
        }

    log debug "polling outdoor module..."
    set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&module_id=$moduleid&scale=max&type=Temperature,Humidity&date_end=last"
    log debug "querying $url"
    catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
    log debug "response is: $response"

    regexp {\"value\":\[\[(.*?),(.*?)\]} $response dummy otemp ohum

    log info "Outside temperature is $otemp"
    log info "Outside humidity is $ohum"

    log debug "polling main module..."
    set url "https://api.netatmo.net/api/getmeasure?access_token=$accesstoken&device_id=$deviceid&scale=max&type=Temperature,Humidity,CO2,Pressure,Noise&date_end=last"
    log debug "querying $url"
    catch {exec /usr/local/addons/cuxd/curl -k -# $url} response
    log debug "response is: $response"

    regexp {\"value\":\[\[(.*?),(.*?),(.*?),(.*?),(.*?)\]} $response dummy itemp ihum ico2 ipressure inoise
   
    log info "LogI is $response"             
    log info "Inside temperature is $itemp"
    log info "Inside humidity is $ihum"
    log info "Inside CO2 level $ico2"
    log info "Inside pressure is $ipressure"
    log info "Inside noise level is $inoise"

   

   


    #
    # set ReGaHss variables
    #
    set rega_cmd ""
    append rega_cmd "var ITemp = dom.GetObject('CUxD.CUX9002001:1.SET_TEMPERATURE');"
    append rega_cmd "var IHumi = dom.GetObject('CUxD.CUX9002001:1.SET_HUMIDITY');"
    append rega_cmd "var OTemp = dom.GetObject('CUxD.CUX9002002:1.SET_TEMPERATURE');"
    append rega_cmd "var OHumi = dom.GetObject('CUxD.CUX9002002:1.SET_HUMIDITY');"
    append rega_cmd "var IPress = dom.GetObject('Luftdruck');"
    append rega_cmd "var ICO2 = dom.GetObject('CO2');"
    append rega_cmd "var INoise = dom.GetObject('Sonometer');"
    append rega_cmd "OTemp.State('$otemp');"
    append rega_cmd "OHumi.State('$ohum');"
    append rega_cmd "ITemp.State('$itemp');"
    append rega_cmd "IHumi.State('$ihum');"
    append rega_cmd "IPress.State('$ipressure');"
    append rega_cmd "ICO2.State('$ico2');"
    append rega_cmd "INoise.State('$inoise');"
    append rega_cmd "var sdatetime = system.Date('%d.%m.%Y %H:%M:%S');"
    append rega_cmd "var netatmosynctime = dom.GetObject('SyncTime');"
    append rega_cmd "netatmosynctime.Variable(sdatetime.ToString());"
    rega_script $rega_cmd
SirDrinksAlot
 
Beiträge: 5
Registriert: 05.12.2017, 17:14

Re: Netatmo Wetterstation (incl. CO2 Messung) einbinden

Beitragvon timo-n » 07.12.2017, 11:45

Es scheint zu gehen :-)
Lag wohl tatsächlich an der Formatierung :roll:

aber irgendwie aktualisiert sich der Status aber nicht automatisch.

Ich hab wie beschrieben nen Virtuellen Tasterkanal genommen und bei Key Short und Long dann
Code: Alles auswählen
tclsh /usr/local/addons/netatmo/netatmo.tcl
eingetragen.

Dann ein Programm mit 15minuten Zeitmodul mit diesem Script
Code: Alles auswählen
dom.GetObject("CUxD.CUX2801001:2.CMD_QUERY_RET").State(2);
dom.GetObject("CUxD.CUX2801001:2.CMD_SETS").State("tclsh /usr/local/addons/netatmo/netatmo.tcl");
var v = dom.GetObject("CUxD.CUX2801001:2.CMD_RETS").State();
WriteLine(v);


Hab ich da was falsch?

Und wenn ich noch ein Zusatzmodul bekomme und aus einem 2ten Raum die Werte haben will was muss da noch in die tcl?

Danke für eure hilfe :D
timo-n
 
Beiträge: 21
Registriert: 23.10.2015, 19:13
Wohnort: Stuttgart

VorherigeNächste

Zurück zu HomeMatic Tipps & Tricks - keine Fragen!

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 9 Gäste







© homematic-forum.de & Lizenzgebern. Alle Rechte vorbehalten. Alle Bilder & Texte auf dieser Seite sind Eigentum
der jeweiligen Besitzer und dürfen ohne deren Einwilligung weder kopiert noch sonstwie weiter verwendet werden.