Shop OBEX P1 Docs P2 Docs Learn Events
WizFi Orb Article Code — Parallax Forums

WizFi Orb Article Code

Tom CantrellTom Cantrell Posts: 4
edited 2012-11-06 09:31 in Robotics
Hello Parallaxians:

I wrote an article recently about using WizNET WizFi210 Wi-Fi module with Prop BOE...

http://www.digikey.com/us/en/techzone/microcontroller/resources/articles/wi-fi-orb-sees-all.html?WT.z_sm_link=G+_microtzart_1010

...so I thought I'd post the code if anyone wants to play around.

CON                              
  _clkmode = xtal1 + pll16x      
  _xinfreq = 5_000_000
OBJ                              
  debug : "Parallax Serial Terminal"                               
  WizFi : "FullDuplexSerial"
CON
  lenipaddr = 18 'max length ip address string incl. cr/lf
  lenxmls = 100 'max length xml field data
DAT
  ssid byte    "your_ssid",$d,0       'replace with your wlan ssid
  website byte "[URL="http://www.google.com",$d,0"]www.google.com",$d,0[/URL]    'using Google
  get byte     "GET /ig/api?stock="     'finance data api              '
  symbol byte  ".dji",$d,$a,0              'stock symbol ex:.dji,aapl,ibm
  numkeys byte 3
  keys byte    "perc",0,"trade_date",0,"trade_time",0 'xml keys
VAR
  byte i,j,k,rxbyte,ipaddr[lenipaddr],xmls[lenxmls]
PUB WizFiOrb                            'gets xml data items from a webpage   
Debug.start(115_200)                    'start debug terminal
WizFi.start(6,7,0,57600)                'start uart connected to WizFi
dira[12..15]~~                          'use two red and two green leds on BOE
waitcnt((clkfreq*3) + cnt)              'some delay to enable/view PST 
Debug.str(string($d,"WizFi Orb",$d))
REPEAT
   REPEAT
 '     Debug.str(string($d,"trying to wakeup..."))
      WizFi.str(string("at",$d))        'dummy command to wakeup WizFi
      REPEAT
      UNTIL WizFi.rx == $5B
   UNTIL WizFi.rx=="O"
  ' Debug.str(string("awake"))
   REPEAT                           
  '    Debug.Str(string($d,"trying to associate..."))
      WizFi.str(string("at+wa="))       
      WizFi.str(@ssid)                  '(re)try to associate
      REPEAT
      UNTIL WizFi.rx == $5B             'look for "["
   UNTIL WizFi.rx == "O"                'retry until WizFi says "[OK]"
 '  Debug.Str(string("associated",$d))
   WizFi.str(string("at+dnslookup="))   'lookup website ip address
   WizFi.str(@website)
  ' Debug.str(string("looking up IP..."))
   REPEAT
   UNTIL WizFi.rx == ":"                ': marks start of returned ip address
   REPEAT i from 0 to lenipaddr-1 
      ipaddr[i] := WizFi.rx             'save ip address digits
    '  Debug.char(ipaddr[i])
      IF ipaddr[i] == $d                'and terminating carriage return 
         quit
 '  Debug.str(string("creating socket...")) 
   REPEAT
      WizFi.str(string("at+nauto=0,1,"))'create tcp client connection
      i:=0
      REPEAT UNTIL ipaddr[i]==$d
         WizFi.tx(ipaddr[i])            'send IP address digits
         i++
      WizFi.str(string(",80",$d))       'port 80
      REPEAT
      UNTIL WizFi.rx == $5B             'look for "["
   UNTIL WizFi.rx=="O"                  'retry until WizFi says "[OK]"
 '  Debug.str(string("created",$d))
  ' Debug.str(string("connecting..."))
   REPEAT
      WizFi.str(string("ata2",$d))      'connect to website server
      REPEAT
      UNTIL WizFi.rx == $5B
   UNTIL WizFi.rx=="O"
  ' Debug.str(string("connected",$d))
   WizFi.str(@get)                      'send http GET request
   REPEAT                               'skip HTTP response header
     i:=WizFi.rx
   UNTIL (i==$0a)AND(WizFi.rx==$0d) 
   k:=0                                 'k is offset for each key in keys                                                
   j:=0                                 'j is pointer to build xml string
   REPEAT numkeys                       'for each key in keys
       i:=0                             'i counts consecutive key char matches
       REPEAT                           'search incoming webpage for key
          CASE WizFi.rx == keys[k+i]    'count consecutive key char matches
             TRUE : i++                 'increment if match
             FALSE : i := 0             'clear otherwise
       UNTIL keys[k+i+1] == 0           'until key matched
       k:=k+i+2                         'point to next key
       REPEAT                           'find opening doublequote
       UNTIL WizFi.rx == $22
       REPEAT                           'gather desired data
          xmls[j] := WizFi.rx
          j++
       UNTIL xmls[j-1] == $22           'until closing doublequote
       xmls[j-1] := $d                  'put carriage return after data
   k:=0                                 'k steps through keys                                                
   j:=0                                 'j steps through xmls
   Debug.str(string($d,"Symbol = "))
   Debug.str(@symbol)
   REPEAT numkeys                       'for each key
      Debug.str(string("  "))
      REPEAT                            'display key
         Debug.char(keys[k])
         k++
      UNTIL keys[k]==0
      Debug.str(string(" = "))
      REPEAT                            'display value
         Debug.char(xmls[j])
         j++
      UNTIL xmls[j-1]==$d
   IF xmls[0]=="-"                      'show on leds
      IF xmls[1]=="0"
         OUTA[12..15]:=%0010            '..down < 1% = one red led
      ELSE
         OUTA[12..15]:=%0011            '..down >= 1% = two red leds
   ELSE
      IF xmls[0]=="0"                   
         OUTA[12..15]:=%0100            '..up < 1% = one green led
      ELSE
         OUTA[12..15]:=%1100            '..up >= 1% = two green leds
 '  Debug.str(string("command mode",$d))
   REPEAT
      WizFi.str(string("+++"))          'request WizFi to command mode
      waitcnt(clkfreq + cnt)            'wait for WizFi to command mode
      WizFi.str(string("at",$d))        'confirm WizFi to command mode
      REPEAT
      UNTIL WizFi.rx == $5B             'look for [
   UNTIL WizFi.rx=="O"                  'retry until WizFi says [OK]
 '  Debug.str(string("hang up",$d))
   REPEAT
      WizFi.str(string("ath",$d))       'hang-up i.e. disassociate
      REPEAT
      UNTIL WizFi.rx == $5B             'look for "["
   UNTIL WizFi.rx=="O"                  'retry until WizFi says [OK]
 '  Debug.str(string("sleep",$d))
   WizFi.str(string("at+psdpsleep",$d)) 'put WizFi to sleep    
   waitcnt((clkfreq*45)+cnt)            'put MCU to sleep
   waitcnt((clkfreq*45)+cnt)            'put MCU to sleep


Here's what you'll see on the terminal...

Symbol = .dji
perc = -0.35
trade_date = 20121105
trade_time = 151100

Symbol = .dji
perc = -0.29
trade_date = 20121105
trade_time = 151301


As mentioned in the article, this code assumes the WizFi210 has been provisioned with your network parameters (SSID, security, etc.) and also the WizFi210 baud rate should match the setting in the code (WizFi.start), 57.6K in this example. Thanks to the eifficiency of SPIN and high-level functions of the WizFi module (ex: DNS) it only takes about 3KB. I'm using two red and two green leds on the PropBOE to 'ambientize' the DJIA data, would like to do something more creative if I find any spare time. .

-- Tom Cantrell

Comments

  • Ken GraceyKen Gracey Posts: 7,392
    edited 2012-11-06 06:04
    Hey Tom!

    We are most honored to have you on the forums. You're proof that there's a bunch of talent out there that doesn't frequent our forums.

    The article was very clear and I hope to use the code myself at some point in the future.
  • Mike GMike G Posts: 2,702
    edited 2012-11-06 07:46
    Nice Article Tom. I'll be checking out the code, hopefully, later today.
  • davejamesdavejames Posts: 4,047
    edited 2012-11-06 08:44
    I was smitten with the elegance!
  • Tom CantrellTom Cantrell Posts: 4
    edited 2012-11-06 09:31
    ...wow Ken, thanks for your kind words!

    I'm realizing it was 20+ years ago that this kid aptly named Chip showed up on my doorstep with his PIC-programmer gadget. And I've been a big fan of Parallax ever since! And how fortunate he had a brother Ken to help run the business with class & passion. I've tried a zillion SBCs over the years, but when I have a brainstorm and want to get something working right now I reach for Parallax. PS: Don't take my non-presence on the forums personally, I'm just kind of anti-social (media :=).

    Mike G: The WizNet folks pointed me to the work you're doing with their wired Ethernet chip and it's quite impressive! So impressive I implore you not to look at my toy program or comment thereupon :=) If there's meaningful takeaway from the article it's a) 'Embedded Wi-Fi' taking off, b) 'Big Data' - where is it, who owns/guarantees it, how to use it?, c) idea of 'ambience' vs. staring at a screen.

    I'm sure I'll be doing some more projects using Parallax gear Ken, I'll definitely come back and keep you posted.

    -- Tom
Sign In or Register to comment.