anyway, thought I'd share this...
Code: Select all
;- dweet.pbi
;- A simple example of dweeting on dweet.io
; DWEET is free and ridiculously simple messaging (and alerts) platform for the Internet of Things.
; you can also use freeboard.io to create free dashboards for dweet.io
; learn more at http://dweet.io
EnableExplicit
InitNetwork()
; Accept a string of JSON data for specified "thing" optionally with specified "lock"
; returns 0 on connection failure or bad input, otherwise assume success and return 1
; Note, server return values are not currently checked.
; Quiet option is on by default and a 204 no content message is returned by the server, if
; Quiet = 0 then a full response is returned from the server (consuming more time).
Procedure.l dweet(Thing$, JSONData$, Lock$ = "", Quiet.b = 1)
Protected Header$ = "", Quiet$ = ""
Protected Length = 0, Timeout = 1000
If Thing$ = "" Or JSONData$ = ""
Debug "dweet(): Invalid Input"
ProcedureReturn 0
EndIf
If Quiet : Quiet$ = "quietly/" : EndIf
If Lock$ <> "" : Lock$ = "?key="+Trim(lock$) : EndIf
Protected Con = OpenNetworkConnection("dweet.io", 80)
If Con
Header$ = "POST /dweet/"+quiet$+"for/"+Trim(Thing$)+Lock$+" HTTP/1.1" + #CRLF$
Header$ + "Host: dweet.io" + #CRLF$
Header$ + "Content-Type: application/json" + #CRLF$
Header$ + "Content-Length: " + Str(StringByteLength(JSONData$, #PB_UTF8)) + #CRLF$
Header$ + #CRLF$
Protected *Buffer = AllocateMemory(10240)
If *Buffer
If SendNetworkString(Con, Header$ + JSONData$, #PB_UTF8)
Repeat
Select NetworkClientEvent(con)
Case #PB_NetworkEvent_None
Delay(10)
Timeout - 1
Case #PB_NetworkEvent_Data
Length = ReceiveNetworkData(Con, *Buffer, MemorySize(*Buffer))
If Length
Debug "dweet(): RX: "+PeekS(*Buffer, -1, #PB_UTF8)
Debug "dweet(): (END OF RX)"
Break
EndIf
Case #PB_NetworkEvent_Disconnect
Timeout = 0
EndSelect
Until Timeout = 0
EndIf
FreeMemory(*Buffer)
Else
Debug "dweet(): Out of memory"
EndIf
CloseNetworkConnection(Con)
Else
Debug "dweet(): Failed to connect to dweet.io"
ProcedureReturn 0
EndIf
EndProcedure
;-----
;----- Testing
Global DweetTestJson = CreateJSON(#PB_Any)
Global dweetio_test = SetJSONObject(JSONValue(DweetTestJson))
SetJSONString(AddJSONMember(dweetio_test, "Hostname"), Hostname())
SetJSONString(AddJSONMember(dweetio_test, "CPU"), CPUName())
SetJSONString(AddJSONMember(dweetio_test, "LocalTime"), FormatDate("%hh:%ii:%ss", Date()))
SetJSONInteger(AddJSONMember(dweetio_test, "ElappsedMS"), ElapsedMilliseconds())
; send the dweet
dweet("pb_dweet", ComposeJSON(DweetTestJson) )
; resulting dweet can be viewed on line at http://dweet.io/follow/pb_dweet
; or retrieved as raw JSON from http://dweet.io/get/latest/dweet/for/pb_dweet
FreeJSON(DweetTestJson)