Article Index

Page 274 

Note change the start of setup to:

  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
to setup to force some ESP32s to change baud rate after upload and before calling loop.
#include <WiFi.h>
void setup() {
Serial.begin(9600);
delay(2000);
Serial.begin(9600);
int status = WiFi.begin("dlink3", "hawkhawk");
  delay(100);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.println(WiFi.status());
  };
}
void loop() {
  delay(1000);
}
 
 

Page 275

Note change the start of setup to:

  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
to setup to force some ESP32s to change baud rate after upload and before calling loop.
The BSSID function doesn't work on the Nano ESP32 and the lines should be removed from the program.
 
#include <WiFi.h>
void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  int status = WiFi.begin("ssid", "password");
  delay(100);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.println(WiFi.status());
  };

  String ssid = WiFi.SSID();
  Serial.printf("ssid %s \n", ssid);
  unsigned char bssid[6];
  WiFi.BSSID(bssid);
  Serial.printf("bssid  %X %X %X %X %X %X \n",
                bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
  Serial.printf("RSSI %d\n", WiFi.RSSI());
  Serial.print("Local IP ");
  Serial.println(WiFi.localIP());
  Serial.print("Subnet mask ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway IP ");
  Serial.println(WiFi.gatewayIP());
}

void loop() {
delay(1000);
}

 

  

Page 281

Note change the start of setup to:

  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
to setup to force some ESP32s to change baud rate after upload and before calling loop.
 
#include <WiFi.h>
void WiFiEvent(WiFiEvent_t event) {
  Serial.printf("[WiFi-event] event: %d\n", event);
  switch (event) {
    case ARDUINO_EVENT_WIFI_READY:
        Serial.println("WiFi interface ready"); break;
    case ARDUINO_EVENT_WIFI_SCAN_DONE:
        Serial.println("Completed scan for access points"); break;
    case ARDUINO_EVENT_WIFI_STA_START:
        Serial.println("WiFi client started"); break;
    case ARDUINO_EVENT_WIFI_STA_STOP:
        Serial.println("WiFi clients stopped"); break;
    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
      Serial.println("Connected to access point"); break;
    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:    
      Serial.println("Disconnected from WiFi access point"); break;
    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
      Serial.print("Obtained IP address: ");
      Serial.println(WiFi.localIP());
      break;
    case ARDUINO_EVENT_WIFI_STA_LOST_IP:
       Serial.println("Lost IP address"); break;
    default: break;
  }
}

void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  WiFi.onEvent(WiFiEvent);
  int status = WiFi.begin("ssid", "password");
};

void loop() {
}

 

Page 282

Note change the start of setup to:

  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
to setup to force some ESP32s to change baud rate after upload and before calling loop.
 
#include <WiFi.h>

int wifiConnect(char* ssid, char* password) {
  int status = WiFi.begin(ssid, password);
  while (status != WL_CONNECTED) {
    switch (status) {
      case WL_NO_SSID_AVAIL:
        Serial.printf("No AP with name %s can be found", ssid);
        return status;
      case WL_CONNECT_FAILED:
        Serial.printf("Connection failed");
        return status;
      case WL_CONNECTION_LOST:
        Serial.printf("Connection lost possible security problem");
        return status;
    }
    delay(100);
    status = WiFi.status();
  }  
  return status;
}
void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  int status=wifiConnect("ssid", "password");
  Serial.println(status);
};

void loop(){
  delay(1000);
}

 

Page 282

Note change the start of setup to:

  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
to setup to force some ESP32s to change baud rate after upload and before calling loop.
 
#include <WiFi.h>
int status;
void WiFiEvent(WiFiEvent_t event) {
  Serial.printf("[WiFi-event] event: %d\n", event);
  status = event;
  switch (event) {
    case ARDUINO_EVENT_WIFI_READY:
      Serial.println("WiFi interface ready");
      break;
    case ARDUINO_EVENT_WIFI_SCAN_DONE:
      Serial.println("Completed scan for access points");
      break;
    case ARDUINO_EVENT_WIFI_STA_START:
      Serial.println("WiFi client started");
      break;
    case ARDUINO_EVENT_WIFI_STA_STOP:
      Serial.println("WiFi clients stopped");
      break;
    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
      Serial.println("Connected to access point");
      break;
    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
    Serial.println("Disconnected from WiFi access point"); break;
    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
      Serial.print("Obtained IP address: ");
      Serial.println(WiFi.localIP());
      break;
    case ARDUINO_EVENT_WIFI_STA_LOST_IP:
      Serial.println("Lost IP address");
      break;
    default: break;
  }
}
void wifiConnect(char* ssid, char* password) {
  WiFi.onEvent(WiFiEvent);
  status = WiFi.begin(ssid, password);
}
void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  WiFi.onEvent(WiFiEvent);
  wifiConnect("dlink3", "hawkhawk");
};

void loop() {
  while (status != ARDUINO_EVENT_WIFI_STA_GOT_IP) {
    delay(1000);
  };
  Serial.println(status);
  delay(5000);
}

Page 284

Note change the start of setup to:

  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
to setup to force some ESP32s to change baud rate after upload and before calling loop.
 
#include <WiFi.h>
void setup() {
  Serial.begin(9600);
  delay(2000);
  Serial.begin(9600);
  int nNet = WiFi.scanNetworks();
  Serial.println(nNet);
  for (int i = 0; i < nNet; i++) {
    Serial.printf("ssid %s  rssi %d channel %d encryption %d\n",
       WiFi.SSID(i), WiFi.RSSI(i),WiFi.channel(i),
                                    WiFi.encryptionType(i));
  }
  Serial.println("Scan complete");
};

void loop(){}

 

Page 385

#include <WiFi.h>
void setup() {
  Serial.begin(9600);
  WiFi.scanNetworks(true);
};
void loop() {
  delay(1000);
  int16_t nNet = WiFi.scanComplete();
  if (nNet  >= 0) {
    for (int i = 0; i < nNet; i++) {
      Serial.printf("ssid %s  rssi %d channel %d encryption %d\n",
             WiFi.SSID(i), WiFi.RSSI(i), WiFi.channel(i),
                                         WiFi.encryptionType(i));
    }
  }
 WiFi.scanDelete();
}

 

Page 287

#include <WiFi.h>
#include <WiFiClient.h>

int wifiConnect(char* ssid, char* password) {
  int status = WiFi.begin(ssid, password);
  while (status != WL_CONNECTED) {
    switch (status) {
      case WL_NO_SSID_AVAIL:
        Serial.printf("No AP with name %s can be found", ssid);
        return status;
      case WL_CONNECT_FAILED:
        Serial.printf("Connection failed");
        return status;
      case WL_CONNECTION_LOST:
        Serial.printf("Connection lost possible security problem");
        return status;
    }
    delay(100);
    status = WiFi.status();
  }
  return status;
}

WiFiClient client;

void setup() {
  Serial.begin(9600);
  int status = wifiConnect("dlink3", "hawkhawk");
  Serial.println(status);

  client.connect("www.example.com", 80);
  client.println("GET /index.html HTTP/1.1");
  client.println("HOST:example.com");
  client.println();
};

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

 

Page 289

#include <WiFi.h>
#include <WiFiClientSecure.h>
int wifiConnect(char* ssid, char* password) {
  int status = WiFi.begin(ssid, password);
  while (status != WL_CONNECTED) {
    switch (status) {
      case WL_NO_SSID_AVAIL:
        Serial.printf("No AP with name %s can be found", ssid);
        return status;
      case WL_CONNECT_FAILED:
        Serial.printf("Connection failed");
        return status;
      case WL_CONNECTION_LOST:
        Serial.printf("Connection lost possible security problem");
        return status;
    }
    delay(100);
    status = WiFi.status();
  }
  return status;
}
WiFiClientSecure client;

void setup() {
  Serial.begin(9600);
  int status = wifiConnect("ssid", "password");
  Serial.println(status);
  client.setInsecure();
  client.connect("www.example.com", 443);
  client.println("GET /index.html HTTP/1.1");
  client.println("HOST:example.com");
  client.println();
};


void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

 

Page 292

#include <WiFi.h>
#include <WiFiClient.h>
int wifiConnect(char* ssid, char* password) {
  int status = WiFi.begin(ssid, password);
  while (status != WL_CONNECTED) {
    switch (status) {
      case WL_NO_SSID_AVAIL:
        Serial.printf("No AP with name %s can be found", ssid);
        return status;
      case WL_CONNECT_FAILED:
        Serial.printf("Connection failed");
        return status;
      case WL_CONNECTION_LOST:
        Serial.printf("Connection lost possible security problem");
        return status;
    }
    delay(100);
    status = WiFi.status();
  }
  return status;
}

WiFiClient client;

void setup() {
  Serial.begin(9600);
  int status = wifiConnect("ssid", "password");
  Serial.println(status);

  Serial.print(client.connect("192.168.253.75", 8080));
  float temp = 20.5;

  int len = snprintf(NULL, 0, "%f", temp);
  char *text = (char*)malloc(len + 1);
  snprintf(text, len + 1, "%f", temp);

  client.println("PUT /index.html HTTP/1.1");
  client.println("Content-Type:text/plain");
  client.print("Content-Length: ");
  client.println(len);
  client.println();
  client.println(text);
};

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

Page 293   Python

from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
     
    def log_message(self,*args, **kwargs):
        pass

    def do_PUT(self):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        bodyString= body.decode(encoding="utf-8")
        temp=float(bodyString)
        print(temp)
        self.send_response(200)
        self.end_headers()

httpd = HTTPServer(('', 8080), SimpleHTTPRequestHandler)
httpd.serve_forever()

 

 Page 295

#include <WiFi.h>
#include <WiFiClient.h>
int wifiConnect(char* ssid, char* password) {
  int status = WiFi.begin(ssid, password);
  while (status != WL_CONNECTED) {
    switch (status) {
      case WL_NO_SSID_AVAIL:
        Serial.printf("No AP with name %s can be found", ssid);
        return status;
      case WL_CONNECT_FAILED:
        Serial.printf("Connection failed");
        return status;
      case WL_CONNECTION_LOST:
        Serial.printf("Connection lost possible security problem");
        return status;
    }
    delay(100);
    status = WiFi.status();
  }
  return status;
}

WiFiServer server(80);

void setup() {
  Serial.begin(9600);
  int status = wifiConnect("ssid", "password");
  Serial.println(status);

  server.begin();
};

void loop() {
  int buflen = 100;
  char buffer[100];

  WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {
      Serial.println("connected");
      while (client.available()) {
        char c = client.read();
        Serial.print(c);
      }

      char page[] = "<html><Body>Temperature =20.5C</Br></body></html>";
      int len = strlen(page);
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html; charset=UTF-8");
      client.println("Content-Length: ");
      client.println(len);
      client.println();
      client.println(page);
    }
  }
  client.stop();
}
 

Page 371  Python

with open("iopress.key", 'rb') as f:
    lines = f.readlines()
lines=b'"'.join(lines)
lines=lines.decode("ascii")
lines=lines.replace("\n",'\\n"\n')
print("static const unsigned char key[]="+'"', lines+";")

with open("iopress.crt", 'rb') as f:
    lines = f.readlines()
lines=b'"'.join(lines)
lines=lines.decode("ascii")
lines=lines.replace("\n",'\\n"\n')
print("static const unsigned char cert[]="+'"', lines+";")

  

Page 298 HTTPS Server  - the certificates listed will eventually timeout and they need to be replaced

#include "esp_https_server.h"
#include <WiFi.h>

int wifiConnect(char* ssid, char* password) {
  int status = WiFi.begin(ssid, password);
  while (status != WL_CONNECTED) {
    switch (status) {
      case WL_NO_SSID_AVAIL:
        Serial.printf("No AP with name %s can be found", ssid);
        return status;
      case WL_CONNECT_FAILED:
        Serial.printf("Connection failed");
        return status;
      case WL_CONNECTION_LOST:
        Serial.printf("Connection lost possible security problem");
        return status;
    }
    delay(100);
    status = WiFi.status();
  }
  return status;
}


esp_err_t get_handlertemp(httpd_req_t* req)
{
    /* Send a simple response */
    const char resp[] = "Temperature is 20.3";
    httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
}
esp_err_t get_handlerhum(httpd_req_t* req)
{
    /* Send a simple response */
    const char resp[] = "Humidity is 80%";
    httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
}

void setup() {
  Serial.begin(9600);
  int status = wifiConnect("dlink3", "hawkhawk");
  Serial.println(status);

static const unsigned char cert[] =  " -----BEGIN CERTIFICATE-----\n"
        "MIIDazCCAlOgAwIBAgIUA+lvUf9wMrNvaz9DuKnfx4TCoeQwDQYJKoZIhvcNAQEL\n"
        "BQAwRTELMAkGA1UEBhMCR0IxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM\n"
        "GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yNDA4MTYxMzQwMTNaFw0yNTA4\n"
        "MTYxMzQwMTNaMEUxCzAJBgNVBAYTAkdCMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw\n"
        "HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB\n"
        "AQUAA4IBDwAwggEKAoIBAQC5zxoZHid/tAtRY+V/Y1rRue4yMiHVLBTmh0kqGM/h\n"
        "NvOuxJUnXKP2qn9cbM1OhZvF7NIIcqNTfHaNwDG+tF8p7YlQGcSBdk5+v3HTIAFI\n"
        "gg3nwHEWdhNhfNnyHrjJG4YDkLGR9KZwMFfBYpsQJHwegUEpYG+5HnaMncjsJu2Q\n"
        "bSO7fQ9dSBC7tIidfv6DhWdz/dHGjqpWYRwHhPACgwS1kKjWiOSrUMWUm3T3px7p\n"
        "UfND7Ypz4/1ObTNZJs8zV8bnWp68YxS0rAeD0QIX3yTgvAGV56Dqdl2V4V5D/dpR\n"
        "No99W3oUu99YeAK/t5pL+Xu5aXDdeg2e1OhPW7o9fZnlAgMBAAGjUzBRMB0GA1Ud\n"
        "DgQWBBQQ4grGsqpNnsjZhWuWOg/Cey7AtTAfBgNVHSMEGDAWgBQQ4grGsqpNnsjZ\n"
        "hWuWOg/Cey7AtTAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAj\n"
        "lw6xqyLBB86XIpW1YAINVHEw9x5ewMSbYTN7pDj01tRlaLfr8S4Qvo4FBA2Mq5fn\n"
        "fstJzucb18yX15ZNo6xD1fAPYRf6BK6UwSeo/U4Hjewkk7gOyKEW8IjAMtWB5Svr\n"
        "bZn4wxcSZEX/EHtGWe0kCZ4bDlWn9GuSjtAIZcrKo+jr0Cos2O4t19MWgxrbOwMx\n"
        "AJYepL5+YcMd0NPuERBfTHE7mIG1heH8DAXAHtnFw26835aEyseZIR6EYPVxESYI\n"
        "xmszSRgHFWDEqPpvaFpdOT1fT4KsloSP+wkE8DcmvjYRbPeabc4z8vTHRWgyLsHJ\n"
        "mjqAoUl1y8um2Iw5ko0N\n"
        "-----END CERTIFICATE-----\n";
static const unsigned char key[] = " -----BEGIN PRIVATE KEY-----\n"
        "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC5zxoZHid/tAtR\n"
        "Y+V/Y1rRue4yMiHVLBTmh0kqGM/hNvOuxJUnXKP2qn9cbM1OhZvF7NIIcqNTfHaN\n"
        "wDG+tF8p7YlQGcSBdk5+v3HTIAFIgg3nwHEWdhNhfNnyHrjJG4YDkLGR9KZwMFfB\n"
        "YpsQJHwegUEpYG+5HnaMncjsJu2QbSO7fQ9dSBC7tIidfv6DhWdz/dHGjqpWYRwH\n"
        "hPACgwS1kKjWiOSrUMWUm3T3px7pUfND7Ypz4/1ObTNZJs8zV8bnWp68YxS0rAeD\n"
        "0QIX3yTgvAGV56Dqdl2V4V5D/dpRNo99W3oUu99YeAK/t5pL+Xu5aXDdeg2e1OhP\n"
        "W7o9fZnlAgMBAAECggEABezu3iIyDEaHnd7bsMZQXSPazsr+fTfcqsVhte/4oSwJ\n"
        "dWdbglfX+sPRL/dgTMLCBvvYbuCJCN6NQVQBwh0qc8HZgS5xL9fABRbB4IPCxrcv\n"
        "Dlb6xEabs54xrSEBr5grG+3/W7I7pJRGGCq22zruomJo25LxvSuViEJ35+AN728d\n"
        "rQW4qce1w0837O15u5EnWVrtEtFbvZHytuywvrGKDkB5mmoCE+31ASPnVH3iQFjw\n"
        "s0HPmMufHfKRKnlucfCl5jJToHxIHf4JBd1LlvjpJfi0EtjLNbxg9vhCr4roKdqq\n"
        "BvM+btu31X7Q/R0Ne3p7V5J3FRNwA3Bce7P6TVsqAQKBgQDdi+5e3FR8QY/qJ9xk\n"
        "4MnxycBhcpd2Wtk7oqCs314fk0ifxtQHUh3QKPmTnqJiwxGRdYje05JHHCo2bgMb\n"
        "FN7AHw1NsYSfsxqYxpgkovc1HJxKNbcqfI/bC1q1lT3Vt9gGk7DCD73EauVIAwOe\n"
        "ybfpqte0Ej95zzTAWqRzw3JmQQKBgQDWtGiC/82qJ7X7xwpjnP0++RauF+TC71Hy\n"
        "alqgTbwy0SEaGcxSdGMaxHKK0p94BIs3ZrSsSXLLUSrYC2c0UYnzFLT2i/uJD4NY\n"
        "NrD4Xwq1Wo6vWLvY2EU618nTFmzDGOaC1enA030puRGRWEH+35iud7AIyuuPyLhr\n"
        "Ek0zNIkypQKBgG9UUwu+QoJSW+R59WmIAFMNZCxT7kLeck1icsWMVXsegx8vRfsL\n"
        "y8l/3bLNw6JHjjt/SbFXtikfwSKq88qXGTyIHiJNs2yhDxt4qJm4futknjE4fvvN\n"
        "rmiPcxzOi00rXlYnv2o1iNH8OY2PXjFcApxcapqllNo8QrDqm7tEmudBAoGAcNua\n"
        "CCoQYH3JQhSJGH1//OcQDekPXYxQ5f0TsCnMYGXfYYnoBfuZ0Issrl4yZvL0fuWk\n"
        "2N8u0ULUI4Yy9KRbwAPFb8d7K7uUzfzJn3TN+zAjynX5H+3mzhx5wVSLTS48lM9+\n"
        "tNY2d4UJf/4FisTby/Gr/aM0mXrnvZh8LgtShuUCgYEAw+3K2PalVraWHGbg18pz\n"
        "fL212YObHDdbHM+SaBoopuiJed9Yz5DRbhVSSfuJMNu9WMjk9aR3Tr7s41l5L/+M\n"
        "YGJGNJCE7I4mIfvgXwezxgd5P39+2Ei/qwR9nwsX/y6Mp3EuLKuPJUUaZERjrkIl\n"
        "EVzn7XZ781QWSSBer5/vcQM=\n"
        "-----END PRIVATE KEY-----\n";

    httpd_ssl_config_t config = HTTPD_SSL_CONFIG_DEFAULT();
    config.servercert = cert;
    config.servercert_len = sizeof(cert);
    config.prvtkey_pem = key;
    config.prvtkey_len = sizeof(key);

    httpd_handle_t server = NULL;
    httpd_uri_t uri_get = {
    .uri = "/temp",
    .method = HTTP_GET,
    .handler = get_handlertemp,
    .user_ctx = NULL
    };
 

    if (httpd_ssl_start(&server, &config) == ESP_OK) {
        httpd_register_uri_handler(server, &uri_get);
        uri_get.uri = "/hum";
        uri_get.handler = get_handlerhum;
        httpd_register_uri_handler(server, &uri_get);
    }
};

void loop() {
}