Week 11

Networking & communications

Week 10 Week 12

Nov 17, 2021


Design, build, and connect wired or wireless node(s) with network or bus addresses

Networked devices

Using the WiFiClient example sketch and building off of modifications here, I set the ESP32 as an access point for a web server, and tested to make sure that I could read clicks on the server in serial monitor.

Than, I was able to use data from the URI header to control PWM signal to my SMA flower from last week.

Tips for working with ESP32

Some learnings from helping folks get the ESP32 server up:

  1. Make sure the ESP32 antenna is not blocked by metal.
  2. Check if GPIO pins (e.g. for LEDs) are okay to use.
  3. If the port is not detected (check with ioreg -p IOUSB on Mac), you may need to install the FTDI driver and restart your computer. The FTDI cable should be detected even before the ESP32 board is connected.
  4. When programming the board, ground the prog run pin. When done, slide the switch and press the reset button for the program to run.
  5. Troubleshooting: try connecting ESP32 to mobile hotspot.


#include <WiFi.h>

const int mosPin = 16;
const int freq = 5000;
const int mosChannel = 0;
const int resolution = 8;
const char* ssid     = "";
const char* password = "";

WiFiServer server(80);
String header;
String PWMstate = "off";
const int LEDpin = 33;

void setup() {

  while (!Serial) {

  ledcSetup(mosChannel, freq, resolution);
  ledcAttachPin(mosPin, mosChannel);
  pinMode(LEDpin, INPUT_PULLUP);

  Serial.print("Setting AP (Access Point)…");
  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");

void loop(){
  WiFiClient client = server.available();

  if (client) {
    Serial.println("New Client.");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        header += c;
        if (c == '\n') {
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Connection: close");
            if (header.indexOf("GET /pwm/on") >= 0) {
              Serial.println("PWM on");
              PWMstate = "on";
              pinMode(LEDpin, INPUT_PULLUP);
              ledcWrite(mosChannel, 155);
            } else if (header.indexOf("GET /pwm/off") >= 0) {
              Serial.println("PWM off");
              PWMstate = "off";
              pinMode(LEDpin, INPUT);
              ledcWrite(mosChannel, 0);
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #ffd1dc; border: none; color: white; padding: 16px 40px; border-radius: 100px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #555555;}</style></head>");
            client.println("<body><h1>Catkins Control</h1>");
            if (PWMstate=="off") {
              client.println("<p><a href=\"/pwm/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/pwm/off\"><button class=\"button button2\">OFF</button></a></p>");
          } else {
            currentLine = "";
        } else if (c != '\r') {
          currentLine += c;
    header = "";