So I made it back home to give everything a test, sketch uploads and seems to be working but I'm getting hung up at the same place; reading Twitter. I'm using the Serial Monitor to debug and it just cycles between "Connecting to server..." and "Connected"; as if it is in an infinite loop.
Now I could really use a helping mind to figure out what I'm missing. Below is the most recent code.
#include <Stepper.h>
#include <ESP8266WiFi.h>
/* Christopher Cronan
Rev 0 December 4, 2015; original coding
Rev 1 December 27, 2015; changes made for twitter account read
Rev 2 January 1, 2016; Inital code cleanup
Rev 3 January 8, 2016; Final code cleanup, LED blinks added for notifications,
blink delay increased to avoid "flashing" to quick to see
BLUE LED will mean a positive event happened, number of blink dictates the event; see below:
5 blinks indicates WiFi connection established
RED LED will mean a negative event happened, number of blinks dictates the event; see below:
10 blinks indicates server connection failed
20 blinks indicated WiFi is not connected, this should cycle with a short delay until
WiFi is properly connected, at which time 5 blue blinks occur
*/
// On board LED setup
int LEDblue = 2 ; // GPIO 0 (onboard LED for good status)
int LEDred = 0 ; // GPIO 2 (onboard LED for Errors
// Blink Functions, BlinkRed for Errors; BlinkBlue for go status(s)
void blinkRed()
{
pinMode (LEDred, HIGH);
delay (250);
pinMode (LEDred, LOW);
delay (250);
}
void blinkBlue ()
{
pinMode (LEDblue, HIGH);
delay (250);
pinMode (LEDblue, LOW);
delay (250);
}
// Network Connection Settings
const char* ssid = "Work Sucks, Quit Now";
const char* password = "myPassword";
// Twitter Connection Tokens & Setup
const String kAccessToken = "myToken";
const String kAccessTokenSecret = "mySecretToken";
const String kAPI_Key = "myAPI";
const String kAPI_Secret = "mySecretAPI";
String sTweet="";
char server[] = "apkit.esy.es"; // name for apkit server using DNS
const unsigned long nRequestInterval = 100000;
boolean bRequested; // Has a request been made since connecting
unsigned long nLastAttemptTime = 0; // last time server connection established
String sCurrentLine = ""; // String to store text from server
String sLastTweet = "";
boolean bReadingTweet = false;
String sUsername = "Tipsez";
String sCountStatus = "1";
WiFiClient client;
void connectToServer()
{
// Attempt to connect, and wait a moment
Serial.println("Connecting to server...");
delay (1000);
if (client.connect(server, 80))
{
Serial.println("Connected to server");
// Make HTTP Request
client.println ("GET /twitter-oauth/status.php?username=" + sUsername + "&access_token=" + kAccessToken + "&access_token_secret=" + kAccessTokenSecret + "&count=" + sCountStatus + " HTTP/1.1");
client.println("Host: apkit.esy.es");
Serial.println ("Connected to Server");
delay (500);
client.println("Connection: close");
client.println();
}
else
{
//kill function if no response from server
Serial.println ("Connection to server failed");
for (int l_i = 0; l_i < 10; l_i++)
{
blinkRed();
}
}
nLastAttemptTime = millis();
}
//*28BYJ-48 Stepper Motor Data and setup
const int kStepsPerRev = 200; // Max steps for one revolution
const int RPM = 30; // Max RPM permitted
// GPIO Pins on Huzzah for Stepper Motor Drive output to Motor Driver Board
int STBY = 5; // GPIO 5 is Driver Standby
// initialize Stepper lib
Stepper myStepper(kStepsPerRev, 16, 14, 12, 13); // GPIO Pins for Stepper Motor Driver
// intial postion of flag; 0 is down, 1 is half, 2 is full
int nPos = 0;
// Raise
void fRaise()
{
for (int l_i = 0; l_i < 5; l_i++)
{
Serial.println ("Clockwise"); // for debugging
myStepper.step(kStepsPerRev); // one rotation clockwise
delay (50);
}
}
// Lower
void fLower ()
{
for (int l_i = 0; l_i < 5; l_i++)
{
Serial.println("Counter-Clockwise");
myStepper.step(-kStepsPerRev);
delay (50);
}
}
// Function to be input to cause Hazzah to go into sleep
// Function to be input to cause Hazzah to go into deep sleep
void setup()
{
// Set stepper speed for best ratio of speed v torque
myStepper.setSpeed(RPM);
// Initialize serial port for debugging
Serial.begin(115200);
delay(100);
// First connect WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(" ");
Serial.println("WiFi Connected! :) ");
Serial.print ("IP address: ");
Serial.print (WiFi.localIP());
Serial.println ("");
}
// Prep Onboard LED, blink if not connected otherwise stay on if connected
// will swap this for power saving once debugging complete
if (WiFi.status() == WL_CONNECTED)
{
for (int l_i = 0; l_i < 5; l_i++)
{
blinkBlue();
}
}
else
{
for (int l_i = 0; l_i < 20; l_i++)
{
blinkRed();
}
Serial.println ("WiFi is not connected");
}
}
void loop()
{
connectToServer();
WiFiClient client;
if (client.connected())
{
if (client.available())
{
// read incoming bytes
char inChar = client.read();
// add incoming byte(s) to EOL;
sCurrentLine += inChar;
// If you get a new line, clear the line
if (inChar == '\n')
{
sCurrentLine = "";
}
//if current line ends with text, it is followed by the tweet
if (sCurrentLine.endsWith ("[text] => "))
{
// tweet is beginning; clear tweet string
bReadingTweet = true;
sTweet = "";
}
//if currently reading bytes of tweet, add them to tweet string
if (bReadingTweet)
{
if (inChar != '[')
{
sTweet += inChar;
}
else
{
//if you got a [ character, you've reached the end of the tweet
bReadingTweet = false;
Serial.println ("Your Status: ");
sTweet.trim();
Serial.print (sTweet);
}
}
}
}
else if (millis() -nLastAttemptTime > nRequestInterval)
{
// if you're not connected and two minutes have passed, attempt to connect
connectToServer();
}
// Lower flag from full staff to half staff
if ( sTweet == "Half" && nPos != 0 && nPos != 1)
{
fLower();
delay (500);
nPos = 1;
}
// Raise flag from bottom of pole to half staff
if (sTweet == "Half" && nPos != 2 && nPos != 1)
{
fRaise();
delay (500);
nPos = 1;
}
// Raise flag from half staff to full staff
if (sTweet == "Full" && nPos != 0 && nPos != 2)
{
fRaise();
delay (500);
nPos = 2;
}
// Raise flag from bottom of pole to full staff
if (sTweet == "Full" && nPos != 0 && nPos != 1)
{
fRaise();
delay (500);
fRaise();
delay (500);
nPos = 2;
}
// end of void loop closing bracket
}