the arduino wifi shield

Post on 17-Aug-2014

656 Views

Category:

Devices & Hardware

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

The nitty-gritty of how the Arduino WiFi shield goes wrong any time a network connection is dropped

TRANSCRIPT

The Arduino WiFi Shield

Katrina Ellison Geltman March 27, 2014

Arduino UNO R3 Arduino WiFi Shield

Arduino UNO R3 Arduino WiFi Shield

+

vs.

+

vs.

How it Works

Getting set up

In the beginning, you have:

WiFi ShieldComputer WiFi RouterArduino

You set up the basics.

WiFi ShieldComputer WiFi RouterArduino

WiFi driver

Serverobject

You request to connect to the network.

WiFi ShieldComputer WiFi RouterArduino

WiFi driver

Request to connectServerobject

If you’re lucky, you connect successfully.

WiFi ShieldComputer WiFi RouterArduino

WiFi driverConnection successful

Request to connectServerobject

You start your server.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Start TCP server listening onsocket/port

Now it’s listening on a particular port.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

You’re ready to go!

Receiving requests

After setup, everything happens in an event loop.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

At the beginning of the loop, a client object is created using the server’s port & socket.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

Clientobject

The client asks the WiFi shield whether it’s active or not.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

ClientobjectWhat's my state?

If there haven’t been any requests, the client stays inactive.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

ClientobjectInactive

The event loop ends and re-starts.

If there haven’t been any requests, the client stays inactive.

!!

The event loop ends and re-starts. !

I think the client is deleted, but I’m not sure how.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

If a request has been made, the client activates.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

ClientobjectClientobjectConnected!

Request!

Your code asks the client object for whatever data it’s receiving from your computer.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

ClientobjectClientobjectConnected!

Request!

Data?

If there is data, it reads it and processes it.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

ClientobjectClientobjectRead data

Request!

Read data

(This is when your Arduino actually does something)

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

ClientobjectClientobjectRead data

Request!

Read data

Then the event loop ends and the client is deleted.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

!Then the event loop ends and the client is deleted.

(I think)

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

ClientobjectInactive

That’s all good

How it goes wrong

Bad things happen if you disconnect from the network.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

The server doesn’t reconnect properly when the network reconnects.

WiFi ShieldComputer WiFi RouterArduino

Serverobject

Your code loop

We know this from our debugging output.

Socket Port Server Status

Client Status

-1 0 0 0

We know this from our debugging output.

Socket Port Server Status

Client Status

-1 0 0 0

0 80 0 0

We know this from our debugging output.

Socket Port Server Status

Client Status

-1 0 0 0

0 80 0 0Still active!

We know this from our debugging output.

Socket Port Server Status

Client Status

-1 0 0 0

0 80 0 0

No server!

How to fix it

Manually release port & socket

void WiFiServer::disconnect() { WiFiClass::_state[_sock] = -1; WiFiClass::_server_port[_sock] = 0; }

void WiFiServer::disconnect() { WiFiClass::_state[_sock] = -1; WiFiClass::_server_port[_sock] = 0; }

void WiFiServer::disconnect() { WiFiClass::_state[_sock] = -1; WiFiClass::_server_port[_sock] = 0; }

void WiFiServer::disconnect() { WiFiClass::_state[_sock] = -1; WiFiClass::_server_port[_sock] = 0; }

Socket Port Server Status

Client Status

-1 0 0 0

Socket Port Server Status

Client Status

-1 0 0 0

0 80 1 0

It works!

It works!

… trivially

It doesn’t work if a client object has ever

been created

It doesn’t work if a client object has ever

been created

This makes it useless!

It doesn’t work if a client object has ever

been created

usually

This makes it useless!

Memory management?

But available memory does not change from

loop to loop.

I don’t really know C++

Or network programming

So I would love to hear your ideas

But whether or not you can help

Your main takeaway should be

Arduino UNO R3 Arduino WiFi Shield

top related