Hi,
I recently bought a spark fun wifly shield (model 2.21 RN 131C).
I have had it working perfectly as a web server, to control 3 LED’s.
I am now using it along with an Adafruit motor shield (I have re routed a couple of pins on the motor shield to allow it to work with the wifly shield) and arduino uno to control a dc motor in response to tweets. It thought I had it working perfectly, but it only works for about 10 mins before it stops making http requests and the green light just stays on constantly. Sometimes the orange one flashes too but the serial monitor shows nothing, not even “association failed” as my code tells it to.
There is no obvious problem with my code or hardware as they both work perfectly for 10 mins.
Do you have any suggestions as to why it may stop working?
Here is the code i’m using:
#include "WiFly.h"
#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ);
String twitterUsername = "NickysLights"; //set the twitter user you'd like to get here.
char ssid[] = ""; //enter your ssid here.
char passphrase[] = ""; //enter your passphrase here.
const unsigned long requestInterval = 2000; // delay between requests
boolean requested; // whether you've made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet
Client client("whitespacers.com", 80);
void setup() {
currentLine.reserve(256);
tweet.reserve(150);
Serial.begin(9600);
WiFly.begin();
if (!WiFly.join(ssid,passphrase)) {
Serial.println("Association failed.");
while (1) {
// Hang on failure.
}
}
Serial.println("connecting...");
}
void loop() {
if(client.connected()){
if (client.available()) {
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
if ( currentLine.endsWith("<text>")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '<') {
tweet += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);
char tweet = Serial.read();
checkAction();
// close the connection to the server:
client.stop();
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect()) {
Serial.println("making HTTP request...");
// make HTTP GET request to twitter:
client.println("GET /train/index.php HTTP/1.0");
client.println("HOST: whitespacers.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
void checkAction() {
if (tweet == ">gofast!")
{
motor.setSpeed(150);
motor.run(BACKWARD);
delay(4000);
motor.setSpeed(50);
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
else if (tweet == ">gofast")
{
motor.setSpeed(200);
motor.run(BACKWARD);
delay(4000);
motor.setSpeed(50);
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
else if (tweet == ">faster")
{
motor.setSpeed(150);
motor.run(BACKWARD);
delay(4000);
motor.setSpeed(50);
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
else if (tweet == ">fast")
{
motor.setSpeed(150);
motor.run(BACKWARD);
delay(4000);
motor.setSpeed(50);
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
else if (tweet == ">fast!")
{
motor.setSpeed(150);
motor.run(BACKWARD);
delay(4000);
motor.setSpeed(50);
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
else if (tweet == ">go")
{
motor.setSpeed(110);
motor.run(BACKWARD);
delay(6000);
motor.setSpeed(50);
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
else if (tweet == ">go!")
{
motor.setSpeed(110);
motor.run(BACKWARD);
delay(6000);
motor.setSpeed(50);
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
else if (tweet == ">go slow")
{
motor.setSpeed(80);
motor.run(BACKWARD);
delay(6000);
motor.run(RELEASE);
}
else if (tweet == ">slow")
{
motor.setSpeed(80);
motor.run(BACKWARD);
delay(6000);
motor.run(RELEASE);
}
else if (tweet == ">slow down")
{
motor.setSpeed(80);
motor.run(BACKWARD);
delay(6000);
motor.run(RELEASE);
}
else if (tweet == ">reverse")
{
motor.setSpeed(80);
motor.run(FORWARD);
delay(4000);
motor.run(RELEASE);
}
else if (tweet == ">reverse!")
{
motor.setSpeed(80);
motor.run(FORWARD);
delay(4000);
motor.run(RELEASE);
}
else if (tweet == ">backwards")
{
motor.setSpeed(80);
motor.run(FORWARD);
delay(6000);
motor.run(RELEASE);
}
}
I tried running the factory reset command from the wifly library and later read that this may have been a bad idea.
Any help won;d be greatly appreciated.
Thanks,
Nicola