AsyncLabs WiShield 2.0 two way communication
My garage door project is starting to expand a bit.
The controller for it will now also monitor my mail box, and send a mail directly to my mobile phone running Android. But to do that best, and in real time, I had to make the WiShield 2.0 and Arduino be able to both recieve the commands from the webpage, telling it to turn the stuff on and off, plus also send a notification to my NAS to tell it that it should send me an email.
After looking a bit in both the server and client example, I noticed that it wasn't that big a difference between them.
I did a bit copy and pasting and edited the code a bit, and here is my result.
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {10,0,0,15}; // IP address of WiShield
unsigned char gateway_ip[] = {10,0,0,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,0,0,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"dd-wrt"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}; // max 64 characters
// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
unsigned long timeDebounce = 30000;
unsigned long inputDebounce = 0;
// Function that prints data from the server
void printData(char* data, int len)
{
// Print the data returned by the server
// Note that the data is not null-terminated, may be broken up into smaller packets, and
// includes the HTTP header.
Serial.print(data);
//while (len-- > 0)
//{
//Serial.print(*(data++));
//}
//delay(100);
}
//IP of my NAS
uint8 ip[] = {10,0,0,117};
//Define the request
GETrequest pullServer(ip, 80, "10.0.0.117", "");
// This is our page serving function that generates web pages
boolean sendMyPage(char* URL)
{
if (strcmp(URL, "/toggle5") == 0)
{
}
WiServer.print("OK");
return true;
}
void setup()
{
Serial.begin(9600);
pullServer.setReturnFunc(printData);
WiServer.init(sendMyPage);
}
void loop()
{
if (millis() - inputDebounce >= timeDebounce && analogRead(5) > 0) //One minute debounce
{
inputDebounce = millis();
//char newURL[32];
//sprintf(newURL, url, var);
pullServer.setURL("/arduino/control/call.php?status=mail");
pullServer.submit();
}
delay(500);
// Run WiServer
WiServer.server_task();
}
It will then call http://10.0.0.117/arduino/control/call.php?status=gotmail when it is more than 60 seconds since last time (to allow the postman to close the lid again without spamming me) and when the digital pin 2 is HIGH (5V input)
The call.php is very simple too
<?php
if (isset($_GET["status"]))
{
if($_GET["status"] == "gotmail")
{
mail("user@provider.com", "You got mail!", "Wohoo, go check it!", "from:noreply@your-arduino.home");
}
}
?>