World’s cheapest SCADA device: source code

This is the source code (as of May 27, 2008) for the Parallax BasicStamp Recording light meter.  I apologize for the formatting, but in the exciting new world of Office 2007, there is *literally* no way to edit the HTML that Google or Help know about, so I can’t use the  <pre></pre> tag.

' {$STAMP BS2}
' {$PBASIC 2.5}

index VAR Nib

wStrength VAR Word
wCount VAR Word

soundTime VAR Word
bCmd VAR Byte

'  generic tmp variable
iTmp VAR Word
wTmp VAR Word

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  '
' ''' Number of values to store in EEPROM
'  '
'
wMaxValuesToStore VAR Word
wMaxValuesToStore = 200
wCurrEntvalueLocation VAR Word
wStoredValuesCount VAR Word
wStoredValuescount = 0

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  '
' ''' How long to wait (milliseconds) for command input before resuming the main loop
'  '
'
wCommandDelay VAR Word
wCommandDelay = 3000

OUTH = %00000000
DIRH = %11111111

UNUSED_DATA CON 65535
'
'  Commands that can be sent to the application
'
CMD_RESET CON 0
CMD_DUMP_LAST_VALUE CON 1
CMD_DUMP_ALL_VALUES CON 2
CMD_SLEEP CON 3
'  returns a fixed length buffer with known contents
CMD_DIAG CON 4
CMD_NOOP CON 254

bCmd = CMD_NOOP

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   ''
' ''''''
' ''''''
'   ''
'
GOSUB Reset

DO
  GOSUB getRCTime
  GOSUB getCommand
  GOSUB Delay
  GOSUB updateDisplay
LOOP

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Reset:
  DEBUG "ResetStart",CR

  '  Make sure history memory ring buffer is set to a value that lets us later distinguish it from zero (0)
  DEBUG "Starting",CR
  DEBUG "Will save ", DEC wMaxValuesToStore, " values in EEPROM",CR

  DEBUG "Initializing history ring buffer",CR
  FOR iTmp = 0 TO ((wMaxValuesToStore -1) * 2) STEP 2
    WRITE iTmp, Word UNUSED_DATA
    'DEBUG DEC iTmp, "."
  NEXT
  DEBUG "Finished initializing history ring buffer",CR
  'DEBUG CLS, HOME
  wCount = 0
  wStrength = 0
  FOR iTmp = 0 TO ((wMaxValuesToStore -1) * 2) STEP 2
    WRITE iTmp, Word 0
  NEXT
  wStoredValuesCount = 0
  DEBUG "ResetFinish",CR
  RETURN

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
getCommand:

  'DEBUG "cmd> "
'  SERIN 16,  84,getCommandBadData, 5000,getCommandTimeout, [WAITSTR cmdBuffer9]
  SERIN 16,  84, wCommandDelay, getCommandTimeout, [DEC1 bCmd]
  'SERIN 16,  84, [DEC bCmd]
  'DEBUG DEC bCmd
  GOSUB processCommand
  RETURN

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
processCommand:
  'DEBUG "Got command: ", DEC bCmd, CR

  SELECT bCmd

    '  reset
    CASE CMD_RESET
      GOSUB Reset

    '  send current value
    CASE CMD_DUMP_LAST_VALUE
      DEBUG "", DEC5 wStrength, "", CR

    '  send last N values
    CASE CMD_DUMP_ALL_VALUES
'      DEBUG CLS, HOME
      DEBUG "" ,CR
      DEBUG TAB, "", DEC wMaxValuesToStore, "",CR
      DEBUG TAB, "", DEC wStoredValuesCount, "", CR
      DEBUG TAB, "", DEC (wCurrentValueLocation / 2), "",CR
      DEBUG TAB, "", DEC wCommandDelay, "", CR

'      FOR iTmp = 0 TO ((wMaxValuesToStore -1) * 2) STEP 2
      FOR iTmp = 0 TO ((wStoredValuesCount *2 ) + 1) STEP 2
        READ iTmp, Word wTmp
        IF wTmp = UNUSED_DATA THEN EXIT
        DEBUG TAB,"", CR
        DEBUG TAB, TAB,"", DEC iTmp, "", CR
        DEBUG TAB, "", CR
        wtmp = 0

      NEXT
      DEBUG "", CR

    CASE CMD_SLEEP
      DEBUG "Sleeping", CR
      SLEEP 1

    CASE CMD_DIAG
      DEBUG "deadbeef0",CR

    CASE CMD_NOOP
      'DEBUG DEC wCurrentValueLocation,"->",DEC wStrength,CR

      'DEBUG "Received NOOP command", CR
'      NAP 0
      DEBUG 0, CR

    CASE ELSE
      'DEBUG "Received unknown command", CR

    ENDSELECT

  RETURN

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
getCommandBadData:
  'DEBUG "Got bad command", CR
  DEBUG "CommandError", CR
  RETURN

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
getCommandTimeout:
  'DEBUG "No command received" ,CR
'     DEBUG DEC wCurrentValueLocation,"->",DEC wStrength,CR
      'DEBUG DEC wStrength,CR

  RETURN

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
getRCTime:
  '  These next 3 lines are the actual application.  How long is it taking for a charged cap to drop to 0?
  HIGH 2   '  arbitrary short time to charge the cap (from the training example)
  PAUSE 3  '  arbitrary short time to let it "leak" down to zero (from the training example)
  RCTIME 2, 1, wStrength

  soundTime = wStrength * 3

  'DEBUG  "Raw strength: ", DEC wStrength ,CR
  '22wStrength = wStrength * wStrength

  wCurrentValueLocation = (wcount // wMaxValuesToStore) * 2
  WRITE wCurrentValueLocation, Word wStrength
  wStoredValuesCount = (wStoredValuesCount + 1) MAX wMaxValuesToStore

  wCount = wCount + 1

'  DEBUG "", DEC5 wStrength, "", CR

' DEBUG CLS, DEC5 sStrength, " soundTime: ", DEC5 soundTime
' FREQOUT 5,  soundTime MAX 250 , ABS (soundTime - 2640)

  RETURN

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Delay:
  PAUSE  wStrength MAX 250
  RETURN

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
updateDisplay:
  IF index = 6 THEN index = 0
  LOOKUP index, [ %01000000,
    %10000000,
    %00000100,
    %00000010,
    %00000001,
    %00100000 ], OUTH

  index = index + 1
  RETURN

The world’s cheapest SCADA device?

As part of my learning about CygNet, SCADA, and the exciting world of Real World Data, I decided to build a SCADA-enabled device from scratch.

I wanted something that could be reconfigured to support any sort of sensor, was very small, and that didn’t require soldering.  A little bit of research (and by research I mean shopping) turned up the venerable BasicStamp: http://www.parallax.com/Default.aspx?tabid=295

It’s just about the simplest embedded system possible.  A processor with a built-in Basic (the language) interpreter, 16 I/O lines, a serial port, and a small breadboard for components.

For $80, you can pick up a version at Radio Shack that includes a complete training program.   (http://www.radioshack.com/product/index.jsp?productId=2117994&cp=&sr=1&origkw=parallax&kw=parallax&parentPage=search), or you can buy just the boards for anywhere from $10-$40 on eBay or the Parallax store.

I spent a little over a week going through the training and ended up with a photocell-based light meter that output readings on the serial port, and stored a small number of recent readings in its onboard EEPROM.  I hadn’t read a circuit diagram in about 30 years, so that was fun.

Useful tidbit:  the web has lots of resistor value calculators

ParallaxBasicStampSCADA002

Doug had already created a serial-device EIE for the Davis Vantage weather station, so it was a matter of hours for him to add a new “general” message that would allow communications with arbitrary devices

Protocol

The protocol is blindingly simple. Send a number (really a single byte with the ASCII code for a numeral) from 0-n, and the device sends back data and/or takes some action. The current command set:

  • 0 (48/0x30) – reset the device. Clear the ring buffer and all counters and restart the program
  • 1 (49/0x31) – return the most recent reading
  • 2 (50/0x32) – return all readings
  • 3 (51/0x33) – sleep (turn off power to the device for N seconds, where N is currently hardcoded)
  • 4 (52/0x34 – return a known diagnostic string (“deadbeef”). Why that word? It’s the default register & memory settings for the RS/6000 plus it’s all in hex. I didn’t say the commands made sense.
  • 254 – used in early testing

Example: Send “1” -> Receive “NNNNN” where N is a numeral

There is no way I could have gotten this running without help from everyone, but especially Doug, Pete, Sheri, and Derek.

NoiseTube – cell phones for ubiquitous noise pollution monitoring

Hey, world, we told you so, although we were a little more advanced in our design and concepts. 🙂

-Lynne

 

http://noisetube.net/

 

Turn your mobile phone into an environmental sensor and participate to the monitoring of noise pollution

Noise pollution is a serious problem in many cities. NoiseTube is a research project about a new participative approach for monitoring noise pollution involving the general public. Our goal is to extend the current usage of mobile phones by turning them into noise sensors enabling each citizen to measure his own exposure in his everyday environment. Furthermore each user could also participate to the creation of a collective map of noise pollution by sharing automatically his geolocalized measures with the community.

By installing our free application on your GPS equipped phone, you will be able to measure the level of noise in dB(A) (with a precision a bit lower than a sound level meter), and contribute to the collective noise mapping by annotating it (tagging, subjective level of annoyance) and sending this geolocalized information automatically to the NoiseTube server by internet (GPRS).

IPv6 SCADA system for energy use monitoring

generic sensor node – http://www.archrock.com/products/ipsensor_node.php

http://www.archrock.com

Arch Rock products and technology enable new wireless sensor applications to be developed in familiar environments, delivered on a wide range of embedded hardware platforms, and integrated with the enterprise. Drawing together three major advancements: industry standard IP-based, multi-hop mesh networking, powerful embedded systems and flexible web services computing architectures, Arch Rock has not only created the foundation to network the physical world, but has now delivered a comprehensive product suite based upon that foundation – Arch Rock Energy Optimizer™ – to help meet the challenge of Green House Gas reduction.

I can’t find anything that says they’re actually producing and selling this technology, which seems odd for a company that’s gotten $15M in venture funding.

They’re doing IPv6 over Zigbee, basically, which is what 802.15.4 is

.NET SCADA

Tired of toy and/or antique languages when dealing with SCADA?  Weep no more, cousins, for TinyCLR has your number.  It’s available with/as two kinds of boards, one compatible with Arduino pin-outs, and the other with Parallax BasicStamps.  They’re dirt-cheap, too, on the order of $100-$150.

Sample I/O devices:  Wii controller, metal detector, reflective sensor, variable potentiometer, Bluetooth (cool – we could talk to the Blackberry this way), MP3 player (as a shield board), accelerometer, IR remote emitter/detector, motor controllers

If we do another round of Cygnipede this year, I’d suggest we build it around these devices.

Welcome to TinyCLR.

Your guide to embedded systems.

Jump start your first .NET Micro Framework project with
the C# Language in minutes!

FEZ Domino and FEZ Mini are very small (open source) boards targeted for beginners. They are based on the USBizi chipset and all it’s features. FEZ offers many peripherals, such as USB host and SD interface, not available with hobbyist-targeted boards. Even though FEZ is targeted for beginners, it’s also a low-cost starting point for professionals wanting to explore NETMF (.NET Micro Framework).
FEZ stands for "Freakin’ Easy!"
FEZ offers many features not found in Arduino, BASIC STAMP and others:

  1. Based on Microsoft’s .NET Micro Framework.

 

Generic Serial Driver for Windows 7 Sensor and Location Platform

http://win7sensorserial.codeplex.com/

The Generic Serial Driver lets you use Arduino and similar microcontroller boards with the Windows 7 Sensor and Location Platform. You’ll be able to create your own combination of firmware and physical sensors. It’s developed in C++ and Arduino.

Their sensor protocol:

image

Invention: Inexpensive absolute rotary (or positional) encoder

Traditional rotary encoders are expensive and too good for my hobbyist/research needs.  The cheapest I’ve found are about $300, plus the several hundred dollars it costs us to set up an account and buy one.  For many applications it would be very, very helpful to not have to give the system hints about where its “arms” and “legs” are via a generally tedious and time-consuming zeroing procedure (I’m looking at you, Meade Telescopes.)

Using an encoding ring similar to my example (below), use a Parallax ColorPAL and pre-calibrated tables (E.g. R:0 G:0 B:0 = 0 Degrees) you could drop the cost signficantly, especially when you consider that a Parallax BasicStamp could also be used for other control/sensing activities at the same time.  To make this simpler to program, although more expensive, you could print separate Red, Green, and Blue rings where the differentiating factor is the value (black<->white).

The sensed area doesn’t have to be a ring if you use colors, but could be any shape, which might be interesting for sensing X-Y positions.

 

image

The ColorPAL is a miniature color and light sensor which, through its RGB LED, doubles as a color generator. For sensing color, the ColorPAL uses its LED to illuminate a sample, one color component at a time, along with a broad-spectrum light-to-voltage converter to measure the light reflected back. The amount of light reflected from the sample under illumination from each red, green, and blue LED can be used to determine the sample’s color.

 

 

image

Invention: pseudo 3-D visual control cues on accelerometer-equipped display devices

Which is a fancy way of saying tablet or mobile phone.  This is so simple that it’s probably been done.

Image an on-screen control like a button object.  Typically I’d use a bit of a false drop-shadow to give the illusion of 3D.  With a phone that can sense movement, though, you could vary the position of the drop shadow in response to changes in position.  With devices that support ambient light sensors, you could also vary the strength of the shadow.

Imagine a UI with a single, “3D” button in the middle of the screen, with the device being held level.  The in-UI light source is coming from directly above the button, which is to say roughly where the user’s head is IRL.  As the device it tilted right (right side down, left side up), a simulated shadow is displayed on the right side of the button, growing longer as the tilt angle increases.  If you wanted to get gnarly about it, you could also begin to foreshorten the button image itself.

I whipped up a demo of how this works, deliberately exaggerating the side-to-side movement of the light source.  Take it from me, even a fast laptop makes a sucky render farm.  If, for some reason, you want the source for this, it’s a Blender file: http://gutenberg.vsi.dom/wp-content/uploads/2010/04/3d-button-shadow.blend.

Note: Depending on your version of Quicktime, your settings therefore, and the phase of Jupiter’s odd-numbered moons, you may have to single/double/context click the movie to get it to play.  You may have to wait for a month with two blue moons and then sacrifice an insurance sales rep to get it to play.  In case it’s not playing, you can download/watch it from here: http://gutenberg.vsi.dom/wp-content/uploads/2010/04/3d-button-demo.mov

http://gutenberg.vsi.dom/wp-content/uploads/2010/04/3d-button-demo.mov

…later…

Huh.  I’ve been Googling for something on this but am not having any luck.  This seems incredibly obvious.  What am I missing?