Go Down

Topic: Changing ethernet code to wifi  (Read 2581 times) previous topic - next topic


I've found a very similar project to mine, but it uses Arduino Uno with the official ethernet shield. I use Arduino Uno with official  WIFI shield. The code for the ethernet is below. What do I have to change to make it compatible with my version?
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 0, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

String HTTP_req;            // stores the HTTP request

void setup()
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for diagnostics
    pinMode(7, INPUT);        // switch is attached to Arduino pin 7
    pinMode(8, INPUT);        // switch is attached to Arduino pin 8

void loop()
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: keep-alive");
                    // AJAX request for switch state
                    if (HTTP_req.indexOf("ajax_switch") > -1) {
                        // read switch state and analog input
                    else {  // HTTP request for web page
                        // send web page - contains JavaScript with AJAX calls
                        client.println("<!DOCTYPE html>");
                        client.println("<title>Arduino Web Page</title>");
                        client.println("function GetSwitchAnalogData() {");
                            "nocache = \"&nocache=\" + Math.random() * 1000000;");
                        client.println("var request = new XMLHttpRequest();");
                        client.println("request.onreadystatechange = function() {");
                        client.println("if (this.readyState == 4) {");
                        client.println("if (this.status == 200) {");
                        client.println("if (this.responseText != null) {");
.innerHTML = this.responseText;");
                        "request.open(\"GET\", \"ajax_switch\" + nocache, true);");
                        client.println("setTimeout('GetSwitchAnalogData()', 1000);");
                        client.println("<body onload=\"GetSwitchAnalogData()\">");
                        client.println("<h1>Arduino AJAX Input</h1>");
                        client.println("<div id=\"sw_an_data\">");
                    // display received HTTP request on serial port
                    HTTP_req = "";            // finished with request, empty string
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)

// send the state of the switch to the web browser
void GetAjaxData(EthernetClient cl)
    int analog_val;
    if (digitalRead(7)) {
        cl.println("<p>Switch 7 state: ON</p>");
    else {
        cl.println("<p>Switch 7 state: OFF</p>");
    if (digitalRead(8)) {
        cl.println("<p>Switch 8 state: ON</p>");
    else {
        cl.println("<p>Switch 8 state: OFF</p>");
    // read analog pin A2
    analog_val = analogRead(2);
    cl.print("<p>Analog A2: ");


The difference between the Ethernet code and the WiFi code is in how the Wifi shield connects to the local area network. Once connected, acting as client or acting as server are the same.


So the only thing I have to change is the way I connect to the server? Basically just the the first bunch of lines?

Go Up