loading

I need help for my electric imp/arduino project....

Hey, I have been working on a project centered around what chris nafis has already done with the grove dust sensor. So far, I have managed to get the dust sensor hooked up to an arduino and I have been getting it to display data, but that's about it. My next step was to get an elecrric im on sparkfun's imp shield to read serial data from the arduino in the form of numbers and then output it to COSM. However, for some reason, this does not seem to want to work on the leval of the imp. I followed most of jimb0's instructions on sparkfun for imps and arduino, but this does not seem to work. I have an arduino leonardo outputting serial data with serial1 at 19200 through pins 1 and 0 to the shield, which it then supposed to connect an imp running a program to patch this to a cosm node. I know nothing about squirrel, just using this code to do things:
// Transmit data between UART and Input/OutputPorts on the impee
// by: Jim Lindblom
//     SparkFun Electronics
// date: September 26, 2012
// license: BeerWare
//          Please use, reuse, and modify this code as you need.
//          We hope it saves you some time, or helps you learn something!
//          If you find it handy, and we meet some day, you can buy me a beer or iced tea in return.

local rxLEDToggle = 1;  // These variables keep track of rx/tx LED toggling status
local txLEDToggle = 1;

// impeeIn will override the InputPort class.
// Whenever data is received to the impee, we'll jump into the set(c) function defined within
class impeeIn extends InputPort
{
    name = "UART Out";
    type = "number";
   
    // This function takes whatever character was sent to the impee
    // and sends it out over the UART5/7. We'll also toggle the txLed
    function set(c)
    {
        hardware.uart57.write(c);
        toggleRxLED();
    }
}

local impeeInput = impeeIn();  // assign impeeIn class to the impeeInput
local impeeOutput = OutputPort("UART In", "number");  // set impeeOutput as a string

function initUart()
{
    hardware.configure(UART_57);    // Using UART on pins 5 and 7
    hardware.uart57.configure(19200, 8, PARITY_NONE, 1, NO_CTSRTS); // 19200 baud worked well, no parity, 1 stop bit, 8 data bits
}

function initLEDs()
{
    // LEDs are on pins 8 and 9 on the imp Shield
    // They're both active low, so writing the pin a 1 will turn the LED off
    hardware.pin8.configure(DIGITAL_OUT_OD_PULLUP);
    hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
    hardware.pin8.write(1);
    hardware.pin9.write(1);
}

// This function turns an LED on/off quickly on pin 9.
// It first turns the LED on, then calls itself again in 50ms to turn the LED off
function toggleTxLED()
{
    txLEDToggle = txLEDToggle?0:1;    // toggle the txLEDtoggle variable
    if (!txLEDToggle)
    {
        imp.wakeup(0.05, toggleTxLED.bindenv(this)); // if we're turning the LED on, set a timer to call this function again (to turn the LED off)
    }
    hardware.pin9.write(txLEDToggle);  // TX LED is on pin 8 (active-low)
}

// This function turns an LED on/off quickly on pin 8.
// It first turns the LED on, then calls itself again in 50ms to turn the LED off
function toggleRxLED()
{
    rxLEDToggle = rxLEDToggle?0:1;    // toggle the rxLEDtoggle variable
    if (!rxLEDToggle)
    {
        imp.wakeup(0.05, toggleRxLED.bindenv(this)); // if we're turning the LED on, set a timer to call this function again (to turn the LED off)
    }
    hardware.pin8.write(rxLEDToggle);   // RX LED is on pin 8 (active-low)
}

// This is our UART polling function. We'll call it once at the beginning of the program,
// then it calls itself every 10us. If there is data in the UART57 buffer, this will read
// as much of it as it can, and send it out of the impee's outputPort.
function pollUart()
{
    imp.wakeup(0.00001, pollUart.bindenv(this));    // schedule the next poll in 10us
   
    local byte = hardware.uart57.read();    // read the UART buffer
    // This will return -1 if there is no data to be read.
    while (byte != -1)  // otherwise, we keep reading until there is no data to be read.
    {
        //  server.log(format("%c", byte)); // send the character out to the server log. Optional, great for debugging
        impeeOutput.set(byte);  // send the valid character out the impee's outputPort
        byte = hardware.uart57.read();  // read from the UART buffer again (not sure if it's a valid character yet)
        toggleTxLED();  // Toggle the TX LED
    }
}

// This is where our program actually starts! Previous stuff was all function and variable declaration.
// This'll configure our impee. It's name is "UartCrossAir", and it has both an input and output to be connected:
imp.configure("UartCrossAir", [impeeInput], [impeeOutput]);
initUart(); // Initialize the UART, called just once
initLEDs(); // Initialize the LEDs, called just once
pollUart(); // start the UART polling, this function continues to call itself
// From here, two main functions are at play:
//      1. We'll be calling pollUart every 10us. If data is sent from the UART, we'll send out out of the impee.
//      2. If data is sent into the impee, we'll jump into the set function in the InputPort.
//
// The end
It's not mine, but I modified it slightly t work with numbers instead of strings. I an then using this arduino code on a leonardo:
;int pin = 10;
unsigned long final;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

void setup() {
  Serial1.begin(19200);
   while (!Serial) {
    ;
   }

  pinMode(10,INPUT);
  starttime = millis();
}

void loop() {
  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;

  if ((millis()-starttime) > sampletime_ms)
  {
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
    final = concentration*35

    ;Serial.println(concentration);

    lowpulseoccupancy = 0;
    starttime = millis();
  }
};int pin = 10;
unsigned long final;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

void setup() {
  Serial1.begin(19200);
   while (!Serial) {
    ;
   }

  pinMode(10,INPUT);
  starttime = millis();
}

void loop() {
  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;

  if ((millis()-starttime) > sampletime_ms)
  {
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
    final = concentration*35

    ;Serial.println(concentration);

    lowpulseoccupancy = 0;
    starttime = millis();
  }
}
I would really like some help, as this is my science fair project and I am the only person that I know that knows about Imp, and sadly we don't have any hackerspaces around where I live in alaska.


tockclock4 years ago
The Electric Imp Shield form Sparkfun "connects one of the imp's UARTs (Imp pins 5 and 7) to either the Arduino hardware UART or a software serial on pins 8 and 9. It defaults to the software serial."

Essentially, you are trying to talk to the Imp on Arduino pins 0 and 1, but the Imp is actually connected to Arduino pins 8 and 9; hence nothing happens.

You have two options:

a) solder the "jumpers on the bottom side to set it to the hardware serial", so that your code works.

b) modify your Arduino code to use Software Serial on pins 8 and 9. Software Serial is pretty easy to use and setup, take a look in the Examples folder on the Arduino.

I would try the code below and see if it works: 

#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX for the Electric Imp shield

int pin = 10;
unsigned long final;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

void setup() {
  mySerial.begin(19200);
  //software serial setup

  pinMode(10,INPUT);
  starttime = millis();
}

void loop() {
  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy+duration;

  if ((millis()-starttime) > sampletime_ms)
  {
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=>100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
    final = concentration*35

      ;
    mySerial.println(concentration);

    lowpulseoccupancy = 0;
    starttime = millis();
  }
}
qquuiinn (author)  tockclock3 years ago
thanks dude this really helped