I'm having a problem with some code.
The project is to wirelessly turn on and off a sprinkler system. A bit of background is that it will be used to show off new wireless connectivity on site for the bigwigs.
The arduino is to connect to a server via wireless that will retrieve the on/off state via a URL that it is meant to be in.
Ok, on to the problem.
The wireless connects, gets DHCP, retrieves the URL I want. That's great. It becomes a problem, when the wireless drops out (after about an hour and a half). That is ok, because it reconnects, but ... it reconnects to the wireless, but doesn't re-request DHCP. So my specific problem I need to solve is, how do I code it so that it re-requests DHCP when it reconnects to the wireless. The reasoning behind it, is that the wireless system I use (on campus) is cisco deployment of wlc5508's and 1250 AP's. The wlc requires the device to request DHCP or else it doesn't authorise the device on the WLAN (I've done a fair bit of digging and can't seem to get a static IP to work - so this is what I'm still currently working on trying to fix, but re-requesting DHCP would be good).
I would greatly appreciate anyone to point me in the right direction.
I'm using Arduino 021, TwentyTen http://toysdownunder.com/freetronics-twenty-ten.html, WiShield 2.0, Greg's WiShield user contrib libraries http://github.com/asynclabs/WiShield_user_contrib and added MsTimer2 http://www.arduino.cc/playground/Main/MsTimer2 but not in use.
- Code: Select all
/*
* A simple sketch that uses WiServer to get the hourly weather data from LAX and prints
* it via the Serial API
*/
#include <WiServer.h>
#include <MsTimer2.h>
extern "C" {
#include "uip.h"
}
#define DEBUG 1
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"sprinkler"}; // max 32 bytes
unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"sprinkler1234"}; // max 64 characters
// WEP 128-bit 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
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
// 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.
while (len-- > 0) {
Serial.print(*(data++));
}
}
// global data
boolean connectAndSendTCP = false;
uip_ipaddr_t srvaddr;
// IP Address for *omitted*
uint8 ip[] = {192,168,0,30};
// A request that gets the latest METAR weather data for LAX
GETrequest getWeather(ip, 80, "sprinkler.*omitted*", "/wishield.php");
void getPage() {
// getWeather.submit();
}
void setup() {
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
WiServer.init(NULL);
Serial.println("Start the DHCP query...");
uip_dhcp_request();
// Enable Serial output and ask WiServer to generate log messages (optional)
Serial.begin(57600);
WiServer.enableVerboseMode(true);
// Have the processData function called when data is returned by the server
getWeather.setReturnFunc(printData);
//MsTimer2::set(1000 * 30, getPage); // 30s period for testing
//MsTimer2::start();
}
// Time (in millis) when the data should be retrieved
long updateTime = 0;
void loop(){
// Check if it's time to get an update
if (millis() >= updateTime) {
getWeather.submit();
// Get another update one hour from now
updateTime += 1000 * 30;
}
if(true == connectAndSendTCP) {
connectAndSendTCP = false;
// Address of server to connect to
uip_ipaddr(&srvaddr, 10,3,3,175);
uip_connect(&srvaddr, HTONS(3333));
}
// Run WiServer
WiServer.server_task();
delay(10);
}
extern "C" {
// Process UDP UIP_APPCALL events
void udpapp_appcall(void)
{
uip_dhcp_run();
}
// DHCP query complete callback
void uip_dhcp_callback(const struct dhcp_state *s)
{
if(NULL != s) {
// Set the received IP addr data into the uIP stack
uip_sethostaddr(s->ipaddr);
uip_setdraddr(s->default_router);
uip_setnetmask(s->netmask);
// Print the received data - its quick and dirty but informative
Serial.print("DHCP IP : ");
Serial.print(uip_ipaddr1(s->ipaddr), DEC);
Serial.print(".");
Serial.print(uip_ipaddr2(s->ipaddr), DEC);
Serial.print(".");
Serial.print(uip_ipaddr3(s->ipaddr), DEC);
Serial.print(".");
Serial.println(uip_ipaddr4(s->ipaddr), DEC);
Serial.print("DHCP GATEWAY: ");
Serial.print(uip_ipaddr1(s->default_router), DEC);
Serial.print(".");
Serial.print(uip_ipaddr2(s->default_router), DEC);
Serial.print(".");
Serial.print(uip_ipaddr3(s->default_router), DEC);
Serial.print(".");
Serial.println(uip_ipaddr4(s->default_router), DEC);
Serial.print("DHCP NETMASK: ");
Serial.print(uip_ipaddr1(s->netmask), DEC);
Serial.print(".");
Serial.print(uip_ipaddr2(s->netmask), DEC);
Serial.print(".");
Serial.print(uip_ipaddr3(s->netmask), DEC);
Serial.print(".");
Serial.println(uip_ipaddr4(s->netmask), DEC);
Serial.print("DHCP DNS : ");
Serial.print(uip_ipaddr1(s->dnsaddr), DEC);
Serial.print(".");
Serial.print(uip_ipaddr2(s->dnsaddr), DEC);
Serial.print(".");
Serial.print(uip_ipaddr3(s->dnsaddr), DEC);
Serial.print(".");
Serial.println(uip_ipaddr4(s->dnsaddr), DEC);
}
else {
Serial.println("DHCP NULL FALLBACK");
}
// Shut down DHCP
uip_dhcp_shutdown();
connectAndSendTCP = true;
}
char packet[] = "SocketAppDHCP";
void socket_app_appcall(void)
{
if(uip_closed() || uip_timedout()) {
Serial.println("SA: closed / timedout");
uip_close();
return;
}
if(uip_poll()) {
Serial.println("SA: poll");
}
if(uip_aborted()) {
Serial.println("SA: aborted");
}
if(uip_connected()) {
Serial.println("SA: connected / send");
uip_send(packet, strlen(packet));
}
if(uip_acked()) {
Serial.println("SA: acked");
uip_close();
}
if(uip_newdata()) {
Serial.println("SA: newdata");
}
if(uip_rexmit()) {
Serial.println("SA: rexmit");
uip_send(packet, strlen(packet));
}
}
// These uIP callbacks are unused for the purposes of this simple DHCP example
// but they must exist.
void socket_app_init(void)
{
}
void udpapp_init(void)
{
}
void dummy_app_appcall(void)
{
}
}
/*
# -- Beginning of python server script
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 3333 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
try:
while 1:
conn, addr = s.accept()
print 'Connected by', addr
data = conn.recv(1024)
if not data:
continue
print data
conn.close()
except:
conn.close()
# -- End of python script
*/
Thanks in advance ...