Hello. I'm a newbie in arduino.
I'm trying to make the wifly shield read from a local database every ten seconds. I managed to do this by using a conditional if in the millis() and adding the value I wanted to wait until it loaded again to a variable like this:
if (millis() >= updateTime) {
updateTime += 10000;
}
the problem I'm having is that the arduino reads from the database and "updates" 2 times and then it stops.
this is my code
#include <_Spi.h>
#include <Client.h>
#include <Configuration.h>
#include <Debug.h>
#include <ParsedStream.h>
#include <Server.h>
#include <SpiUart.h>
#include <WiFly.h>
#include <WiFlyDevice.h>
// (Based on Ethernet's WebClient Example)
#include "WiFly.h"
#include "Credentials.h"
//byte server[] = { 194,80,29,91 }; // Google
//byte server[] = { 66, 249, 89, 104 }; // Google
byte server[] = {169,254,195,202 }; // Google
Client client(server, 80);
char c ;
int updateTime = 0;
int redPin = 3; // LED connected to digital pin 13
int greenPin = 5; // LED connected to digital pin 13
int bluePin = 6; // LED connected to digital pin 13
//Client client("169.254.97.89/emotize", 80);
//Client client("http://www.google.com", 80);
//Client client("http://www.sandrobrt..com", 80);
void setup() {
//------- for LED
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// ---- for Wifi
Serial.begin(115200);
Serial.println("WebClient example at 38400 baud.");
WiFly.begin();
if (!WiFly.join(ssid)) {
Serial.println("Association failed.");
while (1) {
// Hang on failure.
}
}
WiFly.configure(WIFLY_BAUD, 38400);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /get2.php?id_reciever=2 HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
int count = 0;
int plus = 0;
void loop() {
if (millis() >= updateTime) {
client.flush();
client.stop();
WiFly.begin();
if (!WiFly.join(ssid)) {
Serial.println("Association failed.");
}
WiFly.configure(WIFLY_BAUD, 38400);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /get2.php?id_reciever=2 HTTP/1.0");
client.println();
}else {
Serial.println("connection failed");
}
updateTime += 10000;
}
current_state();
}
void current_state(){
if (client.available()) {
c = client.read();
Serial.print(c);
count++;
}
if(isDigit(c)) {
// Serial.println(c);
int number = int(c);
// Serial.println(number);
if(number == 49){//1
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
analogWrite(redPin, 255);
analogWrite(bluePin, 100);
}
if(number == 50){ //2
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
analogWrite(redPin, 255);
analogWrite(greenPin, 75);
}
if(number == 51){//3
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
analogWrite(bluePin, 255);
}
if(number == 52){//4
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
analogWrite(redPin, 255);
}
if(number == 53){//5
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
analogWrite(greenPin, 255);
}
if(number == 54){//6
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
analogWrite(redPin, 255);
analogWrite(bluePin, 255);
}
// plus = 0;
// }
}
//
// if (!client.connected()) {
// Serial.println();
// Serial.println("disconnecting.");
// client.stop();
// for(;;)
// ;
// }
}
If someone knows what the problem is let me know please.
I tried to use the client.flush() because I guess that it's just saving unnecessary data but with it the program doesn't run properly.
thanks in advanced
Sandro Brito