Skip to content

Commit 4f73977

Browse files
committed
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.
1 parent 09de344 commit 4f73977

File tree

3 files changed

+235
-49
lines changed

3 files changed

+235
-49
lines changed

apps-conf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
//
6060
// Commonly accessed uIP stack settings
6161
//
62-
#define MAX_TCP_CONNS 1 // Max TCP connections desired
63-
#define MAX_TCP_LISTENPORTS 1 // Max TCP listening ports
64-
#define MAX_UDP_CONNS 1 // Max UDP connections desired
62+
#define MAX_TCP_CONNS 2 // Max TCP connections desired
63+
#define MAX_TCP_LISTENPORTS 2 // Max TCP listening ports
64+
#define MAX_UDP_CONNS 2 // Max UDP connections desired
6565
// Don't play with UIP_CLOCK_DIV unless you know what you are doing!
6666
#define UIP_CLOCK_DIV 2 // Referenced in stack.c; default 2
6767

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
* Socket App
3+
*
4+
* A simple socket application / DHCP example sketch for the WiShield
5+
* Sample python server script for this sketch to connect with at bottom of sketch
6+
*
7+
*/
8+
9+
// Requires APP_SOCKAPP, APP_UDPAPP and UIP_DHCP to be defined in apps-conf.h
10+
// APP_SOCKAPP - for the TCP sockets components of the sketch
11+
// APP_UDPAPP - for the UDP/DNS components of the sketch
12+
// UIP_DHCP - for the DHCP components of the sketch
13+
14+
#include <WiShield.h>
15+
extern "C" {
16+
#include "uip.h"
17+
}
18+
19+
// Wireless configuration parameters ----------------------------------------
20+
unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield
21+
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
22+
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
23+
char ssid[] = {"ASYNCLABS"}; // max 32 bytes
24+
unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
25+
unsigned char wireless_mode = 1; // 1==Infrastructure, 2==Ad-hoc
26+
unsigned char ssid_len;
27+
unsigned char security_passphrase_len;
28+
29+
// WPA/WPA2 passphrase
30+
const prog_char security_passphrase[] PROGMEM = {"12345678"}; // max 64 characters
31+
32+
// WEP 128-bit keys
33+
prog_uchar wep_keys[] PROGMEM = {
34+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
35+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
36+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
37+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
38+
};
39+
// End of wireless configuration parameters ----------------------------------------
40+
41+
42+
// global data
43+
boolean connectAndSendTCP = false;
44+
uip_ipaddr_t srvaddr;
45+
46+
void setup()
47+
{
48+
// Enable Serial output
49+
Serial.begin(57600);
50+
51+
WiFi.init();
52+
53+
Serial.println("Start the DHCP query...");
54+
uip_dhcp_request();
55+
}
56+
57+
void loop()
58+
{
59+
if(true == connectAndSendTCP) {
60+
connectAndSendTCP = false;
61+
// Address of server to connect to
62+
uip_ipaddr(&srvaddr, 192,168,1,100);
63+
uip_connect(&srvaddr, HTONS(3333));
64+
}
65+
66+
WiFi.run();
67+
}
68+
69+
extern "C" {
70+
// Process UDP UIP_APPCALL events
71+
void udpapp_appcall(void)
72+
{
73+
uip_dhcp_run();
74+
}
75+
76+
// DHCP query complete callback
77+
void uip_dhcp_callback(const struct dhcp_state *s)
78+
{
79+
if(NULL != s) {
80+
// Set the received IP addr data into the uIP stack
81+
uip_sethostaddr(s->ipaddr);
82+
uip_setdraddr(s->default_router);
83+
uip_setnetmask(s->netmask);
84+
85+
// Print the received data - its quick and dirty but informative
86+
Serial.print("DHCP IP : ");
87+
Serial.print(uip_ipaddr1(s->ipaddr), DEC);
88+
Serial.print(".");
89+
Serial.print(uip_ipaddr2(s->ipaddr), DEC);
90+
Serial.print(".");
91+
Serial.print(uip_ipaddr3(s->ipaddr), DEC);
92+
Serial.print(".");
93+
Serial.println(uip_ipaddr4(s->ipaddr), DEC);
94+
95+
Serial.print("DHCP GATEWAY: ");
96+
Serial.print(uip_ipaddr1(s->default_router), DEC);
97+
Serial.print(".");
98+
Serial.print(uip_ipaddr2(s->default_router), DEC);
99+
Serial.print(".");
100+
Serial.print(uip_ipaddr3(s->default_router), DEC);
101+
Serial.print(".");
102+
Serial.println(uip_ipaddr4(s->default_router), DEC);
103+
104+
Serial.print("DHCP NETMASK: ");
105+
Serial.print(uip_ipaddr1(s->netmask), DEC);
106+
Serial.print(".");
107+
Serial.print(uip_ipaddr2(s->netmask), DEC);
108+
Serial.print(".");
109+
Serial.print(uip_ipaddr3(s->netmask), DEC);
110+
Serial.print(".");
111+
Serial.println(uip_ipaddr4(s->netmask), DEC);
112+
113+
Serial.print("DHCP DNS : ");
114+
Serial.print(uip_ipaddr1(s->dnsaddr), DEC);
115+
Serial.print(".");
116+
Serial.print(uip_ipaddr2(s->dnsaddr), DEC);
117+
Serial.print(".");
118+
Serial.print(uip_ipaddr3(s->dnsaddr), DEC);
119+
Serial.print(".");
120+
Serial.println(uip_ipaddr4(s->dnsaddr), DEC);
121+
}
122+
else {
123+
Serial.println("DHCP NULL FALLBACK");
124+
}
125+
126+
connectAndSendTCP = true;
127+
}
128+
129+
130+
char packet[] = "SocketAppDHCP";
131+
132+
void socket_app_appcall(void)
133+
{
134+
if(uip_closed() || uip_timedout()) {
135+
Serial.println("SA: closed / timedout");
136+
uip_close();
137+
return;
138+
}
139+
if(uip_poll()) {
140+
Serial.println("SA: poll");
141+
}
142+
if(uip_aborted()) {
143+
Serial.println("SA: aborted");
144+
}
145+
if(uip_connected()) {
146+
Serial.println("SA: connected / send");
147+
uip_send(packet, strlen(packet));
148+
}
149+
if(uip_acked()) {
150+
Serial.println("SA: acked");
151+
uip_close();
152+
}
153+
if(uip_newdata()) {
154+
Serial.println("SA: newdata");
155+
}
156+
if(uip_rexmit()) {
157+
Serial.println("SA: rexmit");
158+
uip_send(packet, strlen(packet));
159+
}
160+
}
161+
162+
// These uIP callbacks are unused for the purposes of this simple DHCP example
163+
// but they must exist.
164+
void socket_app_init(void)
165+
{
166+
}
167+
168+
void udpapp_init(void)
169+
{
170+
}
171+
172+
void dummy_app_appcall(void)
173+
{
174+
}
175+
}
176+
177+
/*
178+
179+
# -- Beginning of python server script
180+
181+
import socket
182+
183+
HOST = '' # Symbolic name meaning all available interfaces
184+
PORT = 3333 # Arbitrary non-privileged port
185+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
186+
s.bind((HOST, PORT))
187+
s.listen(1)
188+
189+
try:
190+
while 1:
191+
conn, addr = s.accept()
192+
print 'Connected by', addr
193+
data = conn.recv(1024)
194+
if not data:
195+
continue
196+
print data
197+
conn.close()
198+
except:
199+
conn.close()
200+
201+
# -- End of python script
202+
203+
*/
204+

examples/WiFiScan/WiFiScan.pde

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
// ----------------------------------------------------------------------------
1515
// -- WiFiScan state of affairs notes
1616
// --
17-
// -- The stack and this app are currently tweaky; things seem to work every other
18-
// -- time. Using SimpleClient I found the stack tweak to be present in the latest
19-
// -- WiShield master (1.3.0). Try configuring SimpleClient 1.3.0 and running it over
20-
// -- and over and just play with it. I am unsure if DHCP and DNS work together but
21-
// -- they do work separately.
22-
// --
2317
// -- WiFiScan is a conglomeration of APP_TYPES and UIP features; it is both a
2418
// -- TCP/socket app and a UPD app (at the same time). It utilizes the new
2519
// -- UIP_DNS, UIP_DHCP and UIP_SCAN features to do some fun stuff.
@@ -30,12 +24,8 @@
3024
// -- the strongest RSSI.
3125
// -- 3. If suitable OPEN AP is returned connect with it
3226
// -- 4. If connection is made use UIP_DHCP to get DHCP addr info from gateway.
33-
// -- I am not happy with this as I am unable to make the sketch work without
34-
// -- a software reset of the WiShield. Removing the reset needs to be done
35-
// -- to make DHCP viable.
36-
// -- 5. Set the returned DHCP data into memory and reset the WiShield (not Arduino) :(
37-
// -- 6. Reconnect to the OPEN AP using DHCP data then use UIP_DNS to lookup the
38-
// -- IP address of my server.
27+
// -- 5. Set the returned DHCP data into the uIP stack.
28+
// -- 6. Use UIP_DNS to lookup the IP address of my server.
3929
// -- 7. Phone home to my server with TCP data describing the open AP.
4030
// -- I want to add GPS shield to enable sending lat/lon in the packet
4131
// -- 8. Disconnect
@@ -47,8 +37,7 @@
4737
// -- and does its job unnoticed. I have not looked recently but I believe
4838
// -- Skyhook could be used to geographically locate the open APs as well.
4939
// --
50-
// -- The code is a bloody mess - but it works in a 'labratory setting'. I
51-
// -- want to work out the extra reset during DHCP acquisition and then
40+
// -- The code is a bloody mess - but it works in a 'labratory setting'.
5241
// -- I'll refactor the code. Its still an experiment!
5342
// --
5443
// -- If you are going to play with this contact me for a user ID and you can
@@ -67,7 +56,6 @@
6756

6857

6958
//#include <dataflash.h>
70-
//#include <WiServer.h>
7159
#include <WiShield.h>
7260
extern "C" {
7361
#include "uip.h"
@@ -82,8 +70,8 @@ extern "C" {
8270
#define PHASESEARCH 1
8371
#define PHASECONNECT 2
8472
#define PHASEDHCP 3
85-
#define PHASERECONNECT 4
86-
#define PHASEDNS 5
73+
#define PHASEDNS 4
74+
#define PHASEDNSWAIT 5
8775
#define PHASERUN 6
8876
#define PHASECLEANUP 7
8977

@@ -235,22 +223,16 @@ void loop()
235223
uip_dhcp_request();
236224
}
237225
}
238-
if(PHASERECONNECT == phase) {
239-
Serial.println("PHASERECONNECT");
240-
if(false == WiFi.init(15)) {
241-
phase = PHASEINIT;
242-
Serial.println("R2");
243-
}
244-
else {
245-
cleanupCount = 0;
246-
udpRetry = 0;
247-
//set up the DNS resolver
248-
uip_dns_conf(dns_ip);
249-
uip_dns_query("slacklab.org");
250-
phase = PHASEDNS;
251-
}
226+
if(PHASEDNS == phase) {
227+
Serial.println("PHASEDNS");
228+
cleanupCount = 0;
229+
udpRetry = 0;
230+
//set up the DNS resolver
231+
uip_dns_conf(dns_ip);
232+
uip_dns_query("slacklab.org");
233+
phase = PHASEDNSWAIT;
252234
}
253-
if(PHASEDNS == phase || PHASEDHCP == phase || PHASERECONNECT == phase || PHASERUN == phase || PHASECLEANUP == phase) {
235+
if(PHASEDNSWAIT == phase || PHASEDNS == phase || PHASEDHCP == phase || PHASERUN == phase || PHASECLEANUP == phase) {
254236

255237
if(PHASECLEANUP == phase && cleanupCount++ > 1000) {
256238
phase = PHASEINIT;
@@ -314,22 +296,21 @@ extern "C" {
314296

315297
if(20 < udpRetry++) {
316298
Serial.println("DHCP TIMEOUT FALLBACK");
317-
phase = PHASERECONNECT;
299+
phase = PHASEDNS;
318300
}
319301
}
320-
if(PHASEDNS == phase) {
302+
303+
if(PHASEDNSWAIT == phase) {
321304
Serial.println("PHASEDNS");
322-
if(uip_udp_conn->rport == HTONS(53)) {
323-
if(uip_poll()) {
324-
uip_dns_run();
325-
}
326-
if(uip_newdata()) {
327-
uip_dns_newdata();
328-
}
305+
if(uip_poll()) {
306+
uip_dns_run();
329307
}
308+
if(uip_newdata()) {
309+
uip_dns_newdata();
310+
}
311+
330312
if(20 < udpRetry++) {
331313
Serial.println("DNS TIMEOUT FALLBACK");
332-
//phase = PHASECLEANUP;
333314
uip_ipaddr(srvaddr, 71,231,196,153);
334315
uip_connect(&srvaddr, HTONS(7995));
335316
tcpRetry = 0;
@@ -421,9 +402,10 @@ extern "C" {
421402
void uip_dhcp_callback(const struct dhcp_state *s)
422403
{
423404
if(NULL != s) {
424-
//uip_sethostaddr(s->ipaddr);
425-
//uip_setdraddr(s->default_router);
426-
//uip_setnetmask(s->netmask);
405+
406+
uip_sethostaddr(s->ipaddr);
407+
uip_setdraddr(s->default_router);
408+
uip_setnetmask(s->netmask);
427409

428410
local_ip[0] = uip_ipaddr1(s->ipaddr);
429411
local_ip[1] = uip_ipaddr2(s->ipaddr);
@@ -482,7 +464,7 @@ extern "C" {
482464
Serial.println("DHCP NULL FALLBACK");
483465
}
484466

485-
phase = PHASERECONNECT;
467+
phase = PHASEDNS;
486468
}
487469

488470
void uip_dns_callback(char *name, u16_t *ipaddr)

0 commit comments

Comments
 (0)