Hello all,
I'm having trouble maintaining a server I'm trying to build over WiFi; here is the synopsis:
By the way I'm using:
Arduino Wifi Shield
Arduino Uno
- Turn on Arduino; WiFi boots up automatically and connect's to my home (LAN). I setup a server on port 8080 listening for incoming clients
- I then make a call (using WiFi client, which I'm not entirely sure is correct) to GET the current time (EST).
- I'm able to get the current time with this call, but after this I'm unable to handle any clients incoming to the server (even though there is currently none)
- I try connecting via (http://192.xxx.xxx.xxx:8080), but it just hangs; If I remove the GET call - which retrieves the time, I'm then able to consistently connect.
Here is my full code:
#include <SPI.h>
#include <WiFi.h>
#include <SerialLCD.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <EasyTransferI2C.h>
EasyTransferI2C master;
struct DATA_STRUCTURE {
float t; // temperature
float h; // humidity
boolean connection; // connection to wifi
boolean overridePumps;
boolean fan1;
long time;
boolean isAM;
long hr;
long mins;
long secs;
};
DATA_STRUCTURE data;
int PORT = 8080;
char ssid[] = "tynet";
char pass[] = "xxxxxxxx";
boolean toggleBacklightButton = false;
boolean toggleBacklight = true;
boolean timeReceived = false;
char toggleBox;
char buffer[500];
#define I2C_SLAVE_ADDRESS 9
#define I2C_SLAVE_M_ADDRESS 10
int status = WL_IDLE_STATUS;
WiFiServer server(PORT);
WiFiServer puller(80);
char timeapi[] = "www.timeapi.org";
// IMPORTANT NOTE: [4,7,10,11,12,13] pins are used by the WIFI adapter
SerialLCD slcd(8,9);
void setup() {
data.connection = false;
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
slcd.begin();
slcd.backlight();
wifiConnect(true);
}
void wifiConnect(boolean serverBegin) {
if(WiFi.status() == WL_NO_SHIELD) {
slcd.clear();
slcd.setCursor(0, 0);
delay(250);
slcd.print("No WIFI!");
while(true);
}
while(status != WL_CONNECTED) {
slcd.setCursor(0, 0);
slcd.print("Connecting to: ");
slcd.setCursor(0, 1);
slcd.print(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}
if(serverBegin) {
server.begin();
}
wifiSuccess();
}
void loop() {
/*toggleBox = digitalRead(2);
if(toggleBox == LOW && toggleBacklightButton == true) {
toggleBacklightButton = false;
toggleBacklight = !toggleBacklight;
if(toggleBacklight) {
slcd.backlight();
slcd.display();
} else {
slcd.noBacklight();
slcd.noDisplay();
}
} else if(toggleBox == HIGH) {
toggleBacklightButton = true;
// the button was just toggled; once we release
// the button will update.
}*/
WiFiClient client = server.available();
if(client) {
int bufferIndex = 0;
boolean isBlank = true;
Serial.println("Client connected.");
while(client.connected()) {
while(client.available()) {
char c = client.read();
delay(1);
//if(!timeReceived) {
//} else {
if(c == '\n' && isBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("</html>");
break;
}
if(c == '\n') {
isBlank = true;
}
else if(c != '\r') {
buffer[bufferIndex++] = c;
isBlank = false;
}
//}
Serial.write(c);
}
}
/*if(strstr(buffer, "!TIME")) {
Serial.println("FOUND time.");
String time = strrchr(buffer, '!TIME');
time = time.substring(1, -1);
data.time = time.toInt();
master.sendData(I2C_SLAVE_ADDRESS);
}// else {
/*if(strstr(buffer, "GET /DATA")) {
}*/
//}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.flush();
client.stop();
Serial.println("client disonnected");
}
//if(master.receiveData()) {}
}
void wifiSuccess() {
Wire.begin(I2C_SLAVE_M_ADDRESS);
master.begin(details(data), &Wire);
Wire.onReceive(receive);
data.connection = true; // tell the slave we are connected to the internet
master.sendData(I2C_SLAVE_ADDRESS);
IPAddress ip = WiFi.localIP();
char lcdMessage[] = "Connected: ";
char ipMessage[] = "IP: .";
char portColon[] = ":8080";
char buffer[5];
slcd.clear();
slcd.setCursor(0, 0);
strcat(lcdMessage, ssid);
slcd.print(lcdMessage);
delay(50);
slcd.setCursor(0, 1);
slcd.print(ipMessage);
for(int i = 0; i < 4; i++) {
sprintf (buffer, "%d", ip[i]);
}
slcd.print(buffer);
slcd.print(portColon);
delay(250);
/*WiFiClient curlRequest;
if(curlRequest.connect(timeapi, 80)) {
curlRequest.println("GET /est/now?format=!TIME\\s HTTP/1.1");
curlRequest.println("Host:www.timeapi.org");
curlRequest.println("Connection: close");
curlRequest.println();
}*/
timeReceived = true;
}
void receive(int numBytes) {}
I'm unsure exactly how to set this up correctly; some guidance would be greatly appreciated!
Thanks all,
Tyler B