Rechercher dans la communauté
Affichage des résultats pour les étiquettes 'Thingspeak'.
1 résultat trouvé
-
Suite au sujet ici http://www.domotique-fibaro.fr/index.php/topic/262-emp%C3%AAcher-une-function-de-bloquer-un-script/ j'ai mis en place un script LUA afin de faire des tests sur le comportement du "Main Loop" d'un périphérique virtuel sur mon HC2 et je le partage bien volontiers avec vous afin de croiser nos expériences. Il y a deux versions, une uniquement basée sur une variable globale et une autre utilisant en plus thingspeak afin d'historiser les résultats. Il faut tout d'abord ajouter les variables, ici : MLWatchDog pour la V1 et MLWatchDog2 pour la V2 puis ajouter un périphérique virtuel, ajouter un label nommé: lblDebug et le code suivant dans le main loop Version 1 -- Copyright © 2013 Jean-christophe Vermandé ------------------------------------------------------------------------------------------- -- Main process ------------------------------------------------------------------------------------------- selfId = fibaro:getSelfId(); function refreshUI(s) fibaro:log(s); fibaro:call(selfId, "setProperty", "ui.lblDebug.value", s); fibaro:debug(s); end function SecondsToClock(sSeconds) local nSeconds = tonumber(sSeconds) if nSeconds == 0 then return "00:00:00"; else nHours = string.format("%02.f", math.floor(nSeconds/3600)); nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours*60))); nSecs = string.format("%02.f", math.floor(nSeconds - nHours*3600 - nMins *60)); return nHours..":"..nMins..":"..nSecs end end function main() -- prepare a global counter if (_count == nil) then refreshUI("Start at "..os.date()); fibaro:setGlobal("MLWatchDog", os.time()); _count = 0; else _count = _count + 1; local elapsed = os.difftime(os.time(), tonumber(fibaro:getGlobalValue("MLWatchDog"))); local msg = "Loop #".._count.." since "..SecondsToClock(elapsed); refreshUI(msg); end fibaro:sleep(57000); end local status, err = pcall(main); if (not status) then fibaro:debug('error raised ' .. tostring(err or "unknown")); end Mise en place identique que la version pour la version 2, il faut aussi modifier le script avec votre clé thingspeak Version 2 - Thingspeak -- Copyright © 2013 Jean-christophe Vermandé ------------------------------------------------------------------------------------------- -- Main process ------------------------------------------------------------------------------------------- selfId = fibaro:getSelfId(); function refreshUI(s) fibaro:log(s); fibaro:call(selfId, "setProperty", "ui.lblDebug.value", s); fibaro:debug(s); end function SecondsToClock(sSeconds) local nSeconds = tonumber(sSeconds) if nSeconds == 0 then return "00:00:00"; else nHours = string.format("%02.f", math.floor(nSeconds/3600)); nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours*60))); nSecs = string.format("%02.f", math.floor(nSeconds - nHours*3600 - nMins *60)); return nHours..":"..nMins..":"..nSecs end end function pushThingSpeak(v) local ts = Net.FHttp("api.thingspeak.com"); local response, status, errorCode = ts:POST("/update", tostring("key=FCUXXXXXXXXXXXX&field1="..v)); -- ici c'est vite fait ! il manque la vérification de la reponse mais bon ... end function main() -- prepare a global counter if (_count == nil) then refreshUI("Start at "..os.date()); fibaro:setGlobal("MLWatchDog2", os.time()); _count = 0; else _count = _count + 1; local elapsed = os.difftime(os.time(), tonumber(fibaro:getGlobalValue("MLWatchDog2"))); local msg = "Loop #".._count.." since "..SecondsToClock(elapsed); refreshUI(msg); end pushThingSpeak(_count); fibaro:sleep(57000); end local status, err = pcall(main); if (not status) then fibaro:debug('error raised ' .. tostring(err or "unknown")); end C'est plutôt stable chez moi ... Et chez vous ?
- 8 réponses
-
- Watchdog
- Thingspeak
-
(et %d en plus)
Étiqueté avec :