Need simple voltage input example

I know I’m not even close with this, but my coding skills are pretty lacking with Squirrel.  All I need to do is take a voltage reading ( 0 - 1v ) on Pin1, and then send it to the server for display.  I’m eventually going to convert this to an amperage / power reading and then send it via a HTTP POST to my energy monitoring software.  I just can’t seem to grasp how to get the voltage reading.  


I’ve got it down on an arduino, but this is nothing like that.  Anyhow, here’s the wrong version that I’ve been able to come up with so far : 

// First test at voltage monitoring to convert to power reading

// Register with the server
imp.configure(“Voltage Reading”, [], []);

// Variable to represent Pin 1 voltage reading
local readingPin1 = 0;

hardware.pin2.configure(ANALOG_IN);

hardware.pin2.read(readingPin1);

// Show the voltage reading on the server
server.show(readingPin1);

Try  “readingPin1 = hardware.pin2.read();”  for reading the voltage, instead of "
hardware.pin2.read(readingPin1);"


Not sure if it matters, but that’s the syntax I’ve been using successfully.

That worked well, thanks a ton!


I get a voltage reading now.  Not sure what it’s actually referring to though, the reading I’m getting is 27000 when the voltage is about 1.2 volts on the pin.  I’m sure there’s a conversion factor I have to discover to make the reading make sense.

I also have to make it repeatedly sample the voltage and report the reading, but I think there’s enough examples in the temperature logger code that I can pull from to get me going there.

Thanks again!

Well, got it working.  And added in a small averaging array.  It’s working good now.


// First test at voltage monitoring to convert to power reading

// Variable to represent Pin 1 voltage reading
local readingPin1 = 0;

// Output port to send temperature readings
local output = OutputPort(“Voltage”, “number”);

hardware.pin2.configure(ANALOG_IN);

local averageWatts = array(10, 0);

// Update voltage reading on server

function updateNode()
{
  local v = 0;
  local cV = 0;
  local w = 0;
  local wattsTotal = 0;

  v = hardware.pin2.read();

  cV = 3.3 * v / 65535;
  
  w = calculateWatts(cV);
  
  server.show(w);
  
  for(local a=0;a<9;a+=1) {
      
      averageWatts[a]=averageWatts[a+1];
  }
  
  averageWatts[9] = w;
    
  for(local b=0;b<10;b+=1) {
    
    wattsTotal = wattsTotal + averageWatts[b];
  }    
  
  output.set(wattsTotal/10);
    
  imp.wakeup(1.0,updateNode);

}

function calculateWatts(cV) {
    
    local ctRange = 20;
    local ctVoltsMax = 2.5;
    local amps = 0;
    local powerFactor = 0.98;
    local voltage = 113.6;
    local watts = 0;
    
    amps = ctRange * cV / ctVoltsMax;
    watts = voltage * powerFactor * amps;
    
    return watts;
}

// Register with the server
imp.configure(“Voltage Reading”, [], [output]);

updateNode();