Switch to desktop version  
eWON and EMONCMS - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: BASIC Script (https://techforum.ewon.biz/forum-52.html)
+--- Thread: eWON and EMONCMS (/thread-137.html)



eWON and EMONCMS - simon - 12-10-2016

Hi everybody,

Here is an example of BASIC script for publishing the eWON Tag values to the EMONCMS IoT Web Platform (http://emoncms.org).


  2016-10-12_10-01-16.png (Size: 89,87 KB / Downloads: 25)

Code:
ONDATE 1,"*/5 * * * *", "@weather_push()"
END

Function weather_push()
 
  ONSTATUS "GOTO EMONCMS_Callback"  
  @EMONCMS_push("outTemp",outTemp@)
 
EndFn

Function EMONCMS_init()
  EMONCMS_apikey$ = "4263b1bf8f7ecd4c6ecb1ea373974b4b"
  apikey$ = "&apikey=" + EMONCMS_apikey$
EndFn

Function EMONCMS_push($dataName$, $value)  
  @EMONCMS_init()
 
  //REQUESTHTTPX "https://emoncms.org/input/post.json?json={temperature:20.5}&apikey=4263b1bf8f7ecd4c6ecb1ea373974b4b","GET"
  $url$ = "https://emoncms.org"
  $cmd$ = "/input/post.json"
  $param$ = 'json={'+ $dataName$ + ":" + STR$ $value +'}'+ apikey$
  REQUESTHTTPX $url$+$cmd$+'?'+$param$,'GET'
 
  ActionID% = GETSYS PRG,"ACTIONID"
 
EndFn

Function Log($message$, $error%)

  IF $error% = 0 THEN
    LogEvent $message$,102
  ELSE
    LogEvent $message$,99
  ENDIF
    
  Print $message$
EndFn

EMONCMS_Callback:
  EventId%=Getsys PRG,"EVTINFO"

  IF EventId%=ActionID% THEN
    SETSYS PRG,"ACTIONID",EventId%
    CurrentStatus%=GETSYS PRG,"ACTIONSTAT"
    IF (CurrentStatus%=0) THEN
     StatusCode$ = RESPONSEHTTPX "STATUSCODE"
     StatusCode$ = RTRIM statusCode$
     Body$ = RESPONSEHTTPX "RESPONSE-BODY"
     Body$ = RTRIM Body$
     IF StatusCode$ = "200" AND Body$ = "ok" THEN
       @Log("EMONCMS Updated successfully", 0)
     ELSE
       @Log("Error when sending data to EMONCMS :" + Body$, 1)
     ENDIF
    ELSE
       @Log("Error when sending data to EMONCMS", 1)
    ENDIF
  ENDIF
END