Go Down

Topic: Host site and send email alerts (Read 680 times) previous topic - next topic

Hello all, and thanks for taking the time to read and hopefully reply.

I just got the Arduino Mega 2560 Rev. 3 and Officially WiFi Shield today as a gift and was playing around with the code and ran into a problem.  I can have the Arduino serve up a web page just fine, and with another sketch I can send a GET request to a website (pushingbox) which interprets the unique url code and responds accordingly (in my case it sends me an email and text message.)  However when I try to do both things in one sketch (as seen below) it does not work.  At first it will host the web page, and then it will send the GET request (done via button push as a test), however after that it will not do either one again.  If you send the GET request first it will work but will not host the webpage.  If you send the GET request and do not try to connect to the Arduino's hosted web page it will continue to send the GET requests just fine.  I have done some google searches and it appears this might be a known issue (unable to mix client and server activity.)  I am hoping however that it is just my code, or someone here knows how to fix this issue.

This is a basic example (a good bit of code is from some examples online) If I made any typo's here I am sorry.

Code: [Select]
char ssid[] = "username"; //  your network SSID (name)
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
WiFiClient client;
WiFiServer server(80);

int pin = 22;

void setup () {

Serial.begin(9600); //use this to see how much RAM is free, then delete it

if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true); // don't continue
  }

while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid); 
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  Serial.println("Connected to wifi");
       
printWifiStatus();

server.begin();
pinMode(pin, INPUT);
//set everything up
}

void loop () {

if (digitalRead(pin) == HIGH) { //here we check the power for the float switches
    email();
delay(2000) //no need to stop if we lost power, just send email warning
}


        webPage();

}

void email() { //send email warning


char server[] = "api.pushingbox.com";

if (client.connect(server, 80)) {   
client.print("GET /pushingbox?devid=**************"); //change as needed to unique devid       
client.println(" HTTP/1.1");   
client.print("Host: ");   
client.println(server);   
client.println("User-Agent: Arduino");   
client.println(); 
}

while (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
client.stop();
}

}

void webPage(); {

WiFiClient client = server.available();

if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;

while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
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>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");

client.print("test page");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
  // give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Nick Gammon

First, this is confusing:

Code: [Select]

WiFiServer server(80);

...

  char server[] = "api.pushingbox.com";


Try using different names for totally different things.




You also have two "client" variables:

Code: [Select]

WiFiClient client;

...

  WiFiClient client = server.available();


Give them different names too.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

I think there is a potential problem mixing client/server activity. It probably can be done, but depending on how the underlying libraries work, perhaps not in this case.

A server listens, a client initiates a session. Typically you don't do both at the same time. Maybe the underlying library doesn't support it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

alright, I made a
Code: [Select]
WiFiClient client;
WiFiClient emailclient;


and changed the this
Code: [Select]
char pushserver[] = "api.pushingbox.com";

Still not working together (again they work fine alone.)  I suppose you are correct.  I was just hoping to kill two birds at once (be alerted to an event, and then go to a website to take actions based on the event and have the Arduino follow the actions.)  I was hoping to do this with only the Arduino but gave in to the simplicity of pushingbox. I suppose I may need to find another solution all together.  Thanks for your reply.

Ill watch this to see if others have more ideas.

Go Up