Trying to read analog pin2 on explorer kit and tweet value


#1

New electric imp user here. I’m trying to add analog reading pin2 water sensor to the sensor tweet exercise in the explorer tutorials.

I’m missing something since I don’t understand the reading versus conditions part of the function. Any help would be appreciated:

#require “HTS221.class.nut:1.0.0”
#require “LPS22HB.class.nut:1.0.0”
#require “WS2812.class.nut:3.0.0”

// Define constants
const sleepTime = 30;

// Declare Global Variables
tempSensor <- null;
pressureSensor <- null;
led <- null
waterSensor <- hardware.pin2;

// Define functions
function takeReading(){
local conditions = {};
local reading = tempSensor.read();
conditions.temp <- reading.temperature;
conditions.humid <- reading.humidity;
reading = pressureSensor.read();
conditions.press <- reading.pressure;
reading = waterSensor.read();

// Send 'conditions' to the agent
agent.send("reading.sent", conditions);

// Flash the LED
flashLed();

// Set the imp to sleep when idle, ie. program complete
imp.onidle(function() {
    server.sleepfor(sleepTime);
});

}

function flashLed() {
led.set(0, [100,0,28]).draw();
imp.sleep(2);
led.set(0, [0,0,0]).draw();
}

// Start of program

// Configure I2C bus for sensors
local i2c = hardware.i2c89;
i2c.configure(CLOCK_SPEED_400_KHZ);

tempSensor = HTS221(i2c);
tempSensor.setMode(HTS221_MODE.ONE_SHOT);

pressureSensor = LPS22HB(i2c);
pressureSensor.softReset();

// Configure SPI bus and powergate pin for RGB LED
local spi = hardware.spi257;
spi.configure(MSB_FIRST, 7500);
hardware.pin1.configure(DIGITAL_OUT, 1);
waterSensor.configure(ANALOG_IN);
led <- WS2812(spi, 1);

// Take a reading
takeReading();


#2

“conditions” is the table that you send to the agent. So you’ll want to add the water sensor reading to the table. Rather than
reading = waterSensor.read();
try
conditions.water <- waterSensor.read();

In the agent you’ll receive a container with all the previous slots, plus one called “water”.


#3

Thank you coverdriven. That worked, I am sending a value to twitter now. But, that value doesn’t seem to change - if I pull pin2 down to ground pin, or if I pull it up to 3.3V pin, I am still getting a relatively constant value of 5160 plus or minus 50.

I verified the pin should change values by using a different example program that builds a html file to watch the pins change state (ground is like 16 and 3.3 is like 65000).

Is there something I need to do to enable the ground and 3.3V pins? Are they “off” during the readings?


#4

Ah, I know what’s happening…

The WS2812 driver reconfigures SPI, which takes over pins 2, 5 and 7. It doesn’t do this until you instantiate the driver with:

led <- WS2812(spi, 1);

…which then tramples over your watersensor configuration.

Really, the driver should configure the SPI with only “MOSI” driven, which wouldn’t mess with your setup.

The fix should just be to move the waterSensor.configure() to after the led driver instantiation.


#5

That worked. Thank you. Difficult to understand these details.


#6

That one was certainly subtle… as I said, the 2812 driver needs fixing so that it doesn’t touch pins it doesn’t need.