//Many thanks to Tom Igo, most of this code is adapted from his :)
//Flag Waver by Tom Gerhardt
#include <SoftwareSerial.h>
#define rxPin 8
#define txPin 9
// 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 meterPin 11 // controls VU meter; has to be one of
// the PWM pins (9 – 11)
// defines for voltmeter:
#define meterMax 130 // max value on the meter
#define meterScale 150 // my meter reads 0 - 150
// variables:
int inByte= -1; // incoming byte from serial RX
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
int status = 0; // Lantronix device's connection status
long lastCompletionTime = 0; // counter for delay after last completion
int storedComment = 0; //variable of last comment number
int pulse = 500;
int count = 0;
int inc = 30;
boolean wave = true;
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
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);
pinMode(meterPin, OUTPUT);
// start serial port, 9600 8-N-1:
Serial.begin(9600);
//reset Lantronix device:
resetDevice();
// blink reset LED:
blink(3);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(9600);
mySerial.println("all systems go");
}
void loop() {
stateCheck();
setLEDs();
}
// Take the Lantronix device's reset pin low to reset it:
void resetDevice() {
digitalWrite(deviceResetPin, LOW);
delay(50);
digitalWrite(deviceResetPin, HIGH);
mySerial.println("reset");
// 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);
}
}
///////////////////////////////////////////////////////////
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();
if (inByte == 'C') { // 'C' in ASCII
status = connected;
}
else {
// if you got anything other than a C, try again:
deviceConnect();
}
}
break;
case connected:
// send HTTP GET request for CGI script:
httpRequest();
break;
case requesting:
lookForData();
break;
case reading:
readData();
break;
case requestComplete:
waitForNextRequest();
}
}
///////////////////////////////////////////////
void deviceConnect() {
// send out the server address and
// wait for a "C" byte to come back.
// fill in your server's numerical address below:
Serial.print("C128.122.253.189/80\n");
mySerial.println("connecting");
status = connecting;
}
///////////////////////////////////////////////////
void httpRequest() {
// make sure you've cleared the last byte
// from the last request:
inByte = -1;
// reset the string position counter:
stringPos = 0;
// make HTTP GET request and fill in the path to your version
// of the CGI script:
Serial.print("GET /~tjg275/Scraper2.php HTTP/1.0\n");
// fill in your server's name:
Serial.print("HOST: itp.nyu.edu\n\n");
// update the state of the program:
//mySerial.println("requesting");
status = requesting;
}
/////////////////////////////////////////////
void lookForData() {
//mySerial.println("started looking");
// wait for bytes from server:
if (Serial.available()) {
//mySerial.println("found Somthing");
inByte = Serial.read();
// If you get a "<", what follows is the air quality index.
// You need to read what follows the <.
if (inByte == '<') {
stringPos = 0;
status = reading;
//mySerial.println("found a <");
}
}
}
/////////////////////////////////////////////
void readData() {
//mySerial.println("started reading into string");
//Serial.println("readDtata");
if (Serial.available()) {
inByte = Serial.read();
// Keep reading until you get a ">":
if (inByte != '>') {
//mySerial.println("looking for numbers");
mySerial.println(inByte);
// save only ASCII numeric characters (ASCII 0 - 9):
if ((inByte >= '0') && (inByte <= '9')){
mySerial.println(inByte);
inString[stringPos] = inByte;
stringPos++;
}
}
// if you get a ">", you've reached the end of the AQI reading:
else {
//mySerial.println("finished reading string, it got:");
mySerial.println(inString);
interpretResults();
}
}
}
//////////////////////////////////////////////
void interpretResults() {
// convert the string to a numeric value:
int commentNum = atoi(inString);
//mySerial.println("changed it to a int and got:");
mySerial.println(commentNum);
// set the meter appropriately:
wave = true;
waveFlag(commentNum);
lastCompletionTime = millis();
status = requestComplete;
}
/////////////////////////////////////////////////
void waveFlag(int desiredValue) {
//Serial.println(desiredValue);
if (desiredValue > storedComment){
while (wave){
if (count < 2){
if (pulse > 2500){
inc = -30;
pulse = 2500;
}
if (pulse < 500){
inc = 30;
pulse = 560;
count++;
}
pulse += inc;
digitalWrite(meterPin, HIGH);
delayMicroseconds(pulse);
digitalWrite(meterPin, LOW);
delay(20);
}
else{
wave = !wave;
count = 0;
}
}
storedComment = desiredValue;
}
}
//////////////////////////////////////////
void waitForNextRequest() {
if (millis() - lastCompletionTime >= 5000) {
// reset Lantronix device before next request:
resetDevice();
status = disconnected;
}
}
////////////////////////////////////////////
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);
}
}
}