dweet.io

Share your advanced PureBasic knowledge/code with the community.
hippy
User
User
Posts: 28
Joined: Tue Mar 05, 2013 3:11 pm

dweet.io

Post by hippy »

dweet.io is a free online service that is like twitter for machines....

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)
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: dweet.io

Post by infratec »

Interesting :!:

But I think: not really useful.

We host our own 'smarthome' server which stores the data of our 'heat controls'.
You can look at temperarture diagrams for years.
Only so you can see the differences between heating periods.
30 days which are offered for 2$ per month is to less.
For 10$ you can get a webserver with database behind.

Anyway: thanks for the code to make it usable from PB side of life. :mrgreen:

Bernd
Post Reply