odroid_go:arduino:08_wifi_ap

Arduino for ODROID-GO - Wifi in AP mode

We will learn about how to make ODROID-GO as an Wifi AP with adding toggling feature on it.

ESP32 which is used on ODROID-GO supports Wifi 802.11b/g/n. So we can program Wifi features with helpful libraries on Arduino.
In this guide, we're going to use Wifi.h library.

First of all, please see the code below.

#include <WiFi.h>
 
const char *apSsid = "ODROID_GO_AP";
const char *apPasswd = "12345678";
 
WiFiServer server(80);
 
void setup() {
    IPAddress gateway(192, 168, 4, 1);
    IPAddress subnet(255, 255, 255, 0);
 
    if (WiFi.softAP(apSsid, apPasswd)) {
        server.begin();
    }
}
 
void loop() {
 
}

This is the basic code for Wifi in AP mode.
As you know what AP mode means, this code makes ODROID-GO generating its on Wifi signal and you can access to that AP on your Wifi connectable device.
To do that we've defined the AP information for SSID and password. And give gateway IP address and subnetmask address. These IP addresses should be created as an instance of IPAddress class.
So with this code, it will be on 192.168.4.x IP addresses, and you can connect to that by accessing to 192.168.4.1.
This AP activates with a WiFi.softAP() function.

And to provide a web page, define an WiFiServer instance as a global variable. This begins when the Wifi activates successfully in AP mode.

Next, add a code for setting the blue status LED up and some debugging messages shown on the serial monitor. This should be very helpful to show how the code flows.

#include <WiFi.h>
 
#define PIN_STATUS_LED  2
 
const char *apSsid = "ODROID_GO_AP";
const char *apPasswd = "12345678";
 
WiFiServer server(80);
 
void setup() {
    Serial.begin(115200);
 
    pinMode(PIN_STATUS_LED, OUTPUT);
    digitalWrite(PIN_STATUS_LED, LOW);
 
    IPAddress gateway(192, 168, 4, 1);
    IPAddress subnet(255, 255, 255, 0);
 
    if (WiFi.softAP(apSsid, apPasswd)) {
        Serial.println("Wifi AP established.");
        Serial.print("Wifi AP IP: ");
        Serial.println(WiFi.softAPIP());
        Serial.print("AP SSID: ");
        Serial.println(apSsid);
        Serial.print("AP Password: ");
        Serial.println(apPasswd);
 
        server.begin();
    } else {
        Serial.println("Wifi AP establishing failed.");
    }
}
 
void loop() {
 
}

Lastly, write down a client listener in the loop() function. This listener code loops and will response only when the client accesses.
We're not providing a description for this web code, the important thing is that you can response as a packet involving a contents you intent.

#include <WiFi.h>
 
#define PIN_STATUS_LED  2
 
const char *apSsid = "ODROID_GO_AP";
const char *apPasswd = "12345678";
 
WiFiServer server(80);
 
void setup() {
    Serial.begin(115200);
 
    pinMode(PIN_STATUS_LED, OUTPUT);
    digitalWrite(PIN_STATUS_LED, LOW);
 
    IPAddress gateway(192, 168, 4, 1);
    IPAddress subnet(255, 255, 255, 0);
 
    if (WiFi.softAP(apSsid, apPasswd)) {
        Serial.println("Wifi AP established.");
        Serial.print("Wifi AP IP: ");
        Serial.println(WiFi.softAPIP());
        Serial.print("AP SSID: ");
        Serial.println(apSsid);
        Serial.print("AP Password: ");
        Serial.println(apPasswd);
 
        server.begin();
    } else {
        Serial.println("Wifi AP establishing failed.");
    }
}
 
void loop() {
    WiFiClient client = server.available();
 
    if (client) {
        Serial.println("New Client.");
        String currentLine = "";
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                if (c == '\n') {
                    if (currentLine.length() == 0) {
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-type:text/html");
                        client.println();
 
                        client.print("Click <a href=\"/H\">here</a> to turn the blue status LED on.<br>");
                        client.print("Click <a href=\"/L\">here</a> to turn the blue status LED off.<br>");
 
                        client.println();
                        break;
                    } else {
                        currentLine = "";
                    }
                } else if (c != '\r') {
                    currentLine += c;
                }
 
                if (currentLine.endsWith("GET /H")) {
                    digitalWrite(PIN_STATUS_LED, HIGH);
                }
                if (currentLine.endsWith("GET /L")) {
                    digitalWrite(PIN_STATUS_LED, LOW);
                }
            }
        }
        client.stop();
    }
}

The code is in ready. Compile and upload to your ODROID-GO and you can see the debugging code at the Serial monitor.

You can connect to ODROID-GO on your Wifi connectable device.

Access to ODROID_GO_AP with password 12345678.

And visit 192.168.4.1 on your web browser.

If you click a text, you can see the status LED turns on or off.

The behaviors monitored on the Serial monitor.

The complete example is available as follows:

Click the Files → Examples → ODROID-GO → Wifi_AP menu to import and press CTRL-U to compile/upload.

  • odroid_go/arduino/08_wifi_ap.txt
  • Last modified: 2018/06/20 10:58
  • by joshua