Search  

    Sensorbase Arduino Code

    #include <SoftwareSerial.h>

    /*
    SensorBase.org poster
    Language: Wiring/Arduino (pin numbers defined for Arduino)

    Microcontroller is connected to a Lantronix serial-to-ethernet device.
    This program connects to a HTTP server through the Lantronix module,
    makes a HTTP GET request for a PHP script, and parses the returned string.
    Lantronix device communicates at 9600-8-n-1 non-inverted (true) serial.

    This program is designed to contact a PHP script that returns a null byte
    (\0) as its last byte.

    Analog sensor on analog pin 0

    Digital I/O connections as defined in the defines, below

    Lantronix serial settings:
    Baudrate 9600, I/F Mode 4C, Flow 00
    Port 10001
    Remote IP Addr: — none —, Port 00000
    Connect Mode : D4
    Disconn Mode : 00
    Flush   Mode : 00

    created  11 Feb 2008
    modified 22 Sep 2008
    by Tom Igoe
    */

    // Defines for the  program’s status (used for status variable):
    #define disconnected 0
    #define connecting 1
    #define connected 2
    #define requesting 3
    #define reading 4
    #define requestComplete 5

    // Defines for I/O pins:

    #define connectedLED 2        // indicates when there’s a TCP connection
    #define requestingLED 3       // indicates a HTTP request has been made
    #define readingLED 4          // indicates device is reading HTTP results
    #define requestCompleteLED 5  // indicates a successful read
    #define programResetLED 6     // indicates reset of Arduino
    #define deviceResetPin 7      // resets Lantronix Device
    #define softwareTX 8          // softwareSerial TX to Xport
    #define softwareRX 10          // softwareSerial RX, not used
    // variables:
    int inByte= -1;              // incoming byte from serial RX

    int status = 0;              // Lantronix device’s connection status
    long lastCompletionTime = 0; // counter for delay after last completion
    int moduleID = 1;            // ID of the sensor module (assumes several sensors
    // are sending code to the PHP script)
    int voltage = 0;             // the analog voltage from the sensor that you’re sending

    // set up softwareSerial to talk to the Xport:
    SoftwareSerial xport =  SoftwareSerial(softwareRX,softwareTX);

    void setup() {
    // set all status LED pins and Lantronix device reset pin:
    pinMode(connectedLED, OUTPUT);
    pinMode(requestingLED, OUTPUT);
    pinMode(requestCompleteLED, OUTPUT);
    pinMode(programResetLED, OUTPUT);
    pinMode(deviceResetPin, OUTPUT);

    // define pin modes for SoftwareSerial tx, rx pins:
    pinMode(softwareRX, INPUT);
    pinMode(softwareTX, OUTPUT);
    // set the data rate for the SoftwareSerial port
    xport.begin(9600);

    // start serial port, 9600 8-N-1:
    Serial.begin(9600);

    //reset Lantronix device:
    resetDevice();
    // blink reset LED:
    blink(3);
    }

    void loop() {
    // read the data from the sensor:
    voltage = analogRead(5);
    delay(10);
    // Serial.println(voltage);
    // check the state of the HTTP request:
    stateCheck();
    // set the LEDs to inform the user about the state of the request:
    setLEDs();
    }

    /*
    Check the status of the connection and take appropriate action:
    */

    void stateCheck() {
    switch (status) {
    case disconnected:
    // attempt to connect to the server:
    deviceConnect();
    break;
    case connecting:
    // until you get a C, keep trying to connect:
    // read the serial port:
    if (Serial.available()) {
    inByte = Serial.read();
    Serial.print(inByte, BYTE);
    if (inByte == ‘C’) {  // ‘C’ in ascii
    status = connected;
    }
    else {
    // if you got anything other than a C, disconnect:
    status = disconnected;
    }
    }
    break;
    case connected:
    // send HTTP GET request for CGI script:
    httpRequest();
    break;
    case requesting:
    waitForResponse();
    break;
    case requestComplete:
    waitForNextRequest();
    }
    }

    /*
    Set the indicator LEDs according to the state of the program
    */
    void setLEDs() {
    /*  Except for the disconnected and connecting states,
    all the states of the program have corresponding LEDS.
    so you can use a for-next loop to set them  by
    turning them all off except for the one that has
    the same number as the current program state:
    */

    for (int thisLED = 2; thisLED <= 5; thisLED++) {
    if (thisLED == status) {
    digitalWrite(thisLED, HIGH);
    }
    else {
    digitalWrite(thisLED, LOW);
    }
    }
    }

    /*
    Command the Lantronix device to connect to the server
    */
    void deviceConnect() {
    //   fill in your server’s numerical address below:
    xport.print(”C128.122.253.189/80\n”);
    status = connecting;
    //  Serial.println(”connecting”);
    }

    /*
    Send a HTTP GET request
    */
    void httpRequest() {
    // make sure you’ve cleared the last byte
    // from the last request:
    inByte = -1;
    //  Make HTTP GET request. Fill in the path to your version
    //  of the CGI script:
    xport.print(”GET /~amd471/netobj/sensorbase/sensorbase_xml_slog.php?”);

    xport.print(”sensor_ID”);
    xport.print(moduleID, DEC);

    xport.print(”&steps=”);
    xport.print(voltage, DEC);

    xport.print(” HTTP/1.1\n”);

    //  Fill in your server’s name:
    xport.print(”HOST:itp.nyu.edu\n\n”);
    // update the state of the program:
    status = requesting;
    Serial.print(”requesting”);
    }

    /*
    Read the results sent by the server until you get a < character.
    */
    void waitForResponse() {
    // wait for bytes from server:
    if (Serial.available()) {
    inByte = Serial.read();
    Serial.print(inByte, BYTE);
    // when the PHP script sends a 0, the script is done,
    // and the request is complete:
    if (inByte == 0) {
    status = requestComplete;
    lastCompletionTime = millis();
    }
    }
    }
    /*
    Wait two minutes before initiating a new request.
    */
    void waitForNextRequest() {
    if (millis() – lastCompletionTime >= 120000) {
    // reset Lantronix device before next request:
    resetDevice();
    //stateCheck();
    status = disconnected;
    }
    }

    /*
    Take the Lantronix device’s reset pin low to reset it
    */
    void resetDevice() {
    digitalWrite(deviceResetPin, LOW);
    delay(50);
    digitalWrite(deviceResetPin, HIGH);
    // pause to let Lantronix device boot up:
    delay(2000);
    }
    /*
    Blink the reset LED.
    */
    void blink(int howManyTimes) {
    int i;
    for (i=0; i< howManyTimes; i++) {
    digitalWrite(programResetLED, HIGH);
    delay(200);
    digitalWrite(programResetLED, LOW);
    delay(200);
    }
    }

    Share on Facebook!