Permalink
Browse files

DHCP now without extra reset! Added SocketAppDHCP example.

Also increased number of available connections in apps-conf.h in an attempt for more stability.  WiFiScan example updated to remove extra reconnect between DHCP and DNS.
  • Loading branch information...
1 parent 09de344 commit 4f73977e9bf45f60550186553a6532826351ce55 @GregEigsti GregEigsti committed Jul 13, 2010
Showing with 235 additions and 49 deletions.
  1. +3 −3 apps-conf.h
  2. +204 −0 examples/SocketAppDHCP/SocketAppDHCP.pde
  3. +28 −46 examples/WiFiScan/WiFiScan.pde
View
@@ -59,9 +59,9 @@
//
// Commonly accessed uIP stack settings
//
-#define MAX_TCP_CONNS 1 // Max TCP connections desired
-#define MAX_TCP_LISTENPORTS 1 // Max TCP listening ports
-#define MAX_UDP_CONNS 1 // Max UDP connections desired
+#define MAX_TCP_CONNS 2 // Max TCP connections desired
+#define MAX_TCP_LISTENPORTS 2 // Max TCP listening ports
+#define MAX_UDP_CONNS 2 // Max UDP connections desired
// Don't play with UIP_CLOCK_DIV unless you know what you are doing!
#define UIP_CLOCK_DIV 2 // Referenced in stack.c; default 2
@@ -0,0 +1,204 @@
+/*
+ * Socket App
+ *
+ * A simple socket application / DHCP example sketch for the WiShield
+ * Sample python server script for this sketch to connect with at bottom of sketch
+ *
+ */
+
+// Requires APP_SOCKAPP, APP_UDPAPP and UIP_DHCP to be defined in apps-conf.h
+// APP_SOCKAPP - for the TCP sockets components of the sketch
+// APP_UDPAPP - for the UDP/DNS components of the sketch
+// UIP_DHCP - for the DHCP components of the sketch
+
+#include <WiShield.h>
+extern "C" {
+ #include "uip.h"
+}
+
+// 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[] = {"ASYNCLABS"}; // max 32 bytes
+unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
+unsigned char wireless_mode = 1; // 1==Infrastructure, 2==Ad-hoc
+unsigned char ssid_len;
+unsigned char security_passphrase_len;
+
+// WPA/WPA2 passphrase
+const prog_char security_passphrase[] PROGMEM = {"12345678"}; // 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
+};
+// End of wireless configuration parameters ----------------------------------------
+
+
+// global data
+boolean connectAndSendTCP = false;
+uip_ipaddr_t srvaddr;
+
+void setup()
+{
+ // Enable Serial output
+ Serial.begin(57600);
+
+ WiFi.init();
+
+ Serial.println("Start the DHCP query...");
+ uip_dhcp_request();
+}
+
+void loop()
+{
+ if(true == connectAndSendTCP) {
+ connectAndSendTCP = false;
+ // Address of server to connect to
+ uip_ipaddr(&srvaddr, 192,168,1,100);
+ uip_connect(&srvaddr, HTONS(3333));
+ }
+
+ WiFi.run();
+}
+
+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");
+ }
+
+ 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
+
+*/
+
@@ -14,12 +14,6 @@
// ----------------------------------------------------------------------------
// -- WiFiScan state of affairs notes
// --
-// -- The stack and this app are currently tweaky; things seem to work every other
-// -- time. Using SimpleClient I found the stack tweak to be present in the latest
-// -- WiShield master (1.3.0). Try configuring SimpleClient 1.3.0 and running it over
-// -- and over and just play with it. I am unsure if DHCP and DNS work together but
-// -- they do work separately.
-// --
// -- WiFiScan is a conglomeration of APP_TYPES and UIP features; it is both a
// -- TCP/socket app and a UPD app (at the same time). It utilizes the new
// -- UIP_DNS, UIP_DHCP and UIP_SCAN features to do some fun stuff.
@@ -30,12 +24,8 @@
// -- the strongest RSSI.
// -- 3. If suitable OPEN AP is returned connect with it
// -- 4. If connection is made use UIP_DHCP to get DHCP addr info from gateway.
-// -- I am not happy with this as I am unable to make the sketch work without
-// -- a software reset of the WiShield. Removing the reset needs to be done
-// -- to make DHCP viable.
-// -- 5. Set the returned DHCP data into memory and reset the WiShield (not Arduino) :(
-// -- 6. Reconnect to the OPEN AP using DHCP data then use UIP_DNS to lookup the
-// -- IP address of my server.
+// -- 5. Set the returned DHCP data into the uIP stack.
+// -- 6. Use UIP_DNS to lookup the IP address of my server.
// -- 7. Phone home to my server with TCP data describing the open AP.
// -- I want to add GPS shield to enable sending lat/lon in the packet
// -- 8. Disconnect
@@ -47,8 +37,7 @@
// -- and does its job unnoticed. I have not looked recently but I believe
// -- Skyhook could be used to geographically locate the open APs as well.
// --
-// -- The code is a bloody mess - but it works in a 'labratory setting'. I
-// -- want to work out the extra reset during DHCP acquisition and then
+// -- The code is a bloody mess - but it works in a 'labratory setting'.
// -- I'll refactor the code. Its still an experiment!
// --
// -- If you are going to play with this contact me for a user ID and you can
@@ -67,7 +56,6 @@
//#include <dataflash.h>
-//#include <WiServer.h>
#include <WiShield.h>
extern "C" {
#include "uip.h"
@@ -82,8 +70,8 @@ extern "C" {
#define PHASESEARCH 1
#define PHASECONNECT 2
#define PHASEDHCP 3
-#define PHASERECONNECT 4
-#define PHASEDNS 5
+#define PHASEDNS 4
+#define PHASEDNSWAIT 5
#define PHASERUN 6
#define PHASECLEANUP 7
@@ -235,22 +223,16 @@ void loop()
uip_dhcp_request();
}
}
- if(PHASERECONNECT == phase) {
- Serial.println("PHASERECONNECT");
- if(false == WiFi.init(15)) {
- phase = PHASEINIT;
- Serial.println("R2");
- }
- else {
- cleanupCount = 0;
- udpRetry = 0;
- //set up the DNS resolver
- uip_dns_conf(dns_ip);
- uip_dns_query("slacklab.org");
- phase = PHASEDNS;
- }
+ if(PHASEDNS == phase) {
+ Serial.println("PHASEDNS");
+ cleanupCount = 0;
+ udpRetry = 0;
+ //set up the DNS resolver
+ uip_dns_conf(dns_ip);
+ uip_dns_query("slacklab.org");
+ phase = PHASEDNSWAIT;
}
- if(PHASEDNS == phase || PHASEDHCP == phase || PHASERECONNECT == phase || PHASERUN == phase || PHASECLEANUP == phase) {
+ if(PHASEDNSWAIT == phase || PHASEDNS == phase || PHASEDHCP == phase || PHASERUN == phase || PHASECLEANUP == phase) {
if(PHASECLEANUP == phase && cleanupCount++ > 1000) {
phase = PHASEINIT;
@@ -314,22 +296,21 @@ extern "C" {
if(20 < udpRetry++) {
Serial.println("DHCP TIMEOUT FALLBACK");
- phase = PHASERECONNECT;
+ phase = PHASEDNS;
}
}
- if(PHASEDNS == phase) {
+
+ if(PHASEDNSWAIT == phase) {
Serial.println("PHASEDNS");
- if(uip_udp_conn->rport == HTONS(53)) {
- if(uip_poll()) {
- uip_dns_run();
- }
- if(uip_newdata()) {
- uip_dns_newdata();
- }
+ if(uip_poll()) {
+ uip_dns_run();
}
+ if(uip_newdata()) {
+ uip_dns_newdata();
+ }
+
if(20 < udpRetry++) {
Serial.println("DNS TIMEOUT FALLBACK");
- //phase = PHASECLEANUP;
uip_ipaddr(srvaddr, 71,231,196,153);
uip_connect(&srvaddr, HTONS(7995));
tcpRetry = 0;
@@ -421,9 +402,10 @@ extern "C" {
void uip_dhcp_callback(const struct dhcp_state *s)
{
if(NULL != s) {
- //uip_sethostaddr(s->ipaddr);
- //uip_setdraddr(s->default_router);
- //uip_setnetmask(s->netmask);
+
+ uip_sethostaddr(s->ipaddr);
+ uip_setdraddr(s->default_router);
+ uip_setnetmask(s->netmask);
local_ip[0] = uip_ipaddr1(s->ipaddr);
local_ip[1] = uip_ipaddr2(s->ipaddr);
@@ -482,7 +464,7 @@ extern "C" {
Serial.println("DHCP NULL FALLBACK");
}
- phase = PHASERECONNECT;
+ phase = PHASEDNS;
}
void uip_dns_callback(char *name, u16_t *ipaddr)

0 comments on commit 4f73977

Please sign in to comment.