esp32 programmer - tamlablinz/learn-esp32 GitHub Wiki

ESP 32 VIA WROOOM 32 DOWNLOADER

We will program ESP32 chips directly:

alt text

with a programmer like this:

alt text

  1. download usb to uart drivers for CP2104 from https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

  2. in Arduino select board Firebeetle-ESP32

  3. upload a test sketch

PINOUT I2C

When using the ESP32 with the Arduino IDE, you should use the ESP32 I2C default pins (supported by the Wire library):

GPIO 21 (SDA)
GPIO 22 (SCL)

Example of complete code for WROOM with Wifi, OSC, I2C and webserver from the embodied gestures project (pins can be differently labeled to other kits!):


#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>

#include <Wire.h>

#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <Adafruit_Sensor.h>

/* Set the delay between fresh samples */
#define BNO055_SAMPLERATE_DELAY_MS (5)

Adafruit_BNO055 bno = Adafruit_BNO055();

// WiFi network name and password:
const char * networkName = "embodiedgestures";
const char * networkPswd = "goodman1";

//IP address to send UDP data to:
// either use the ip address of the server or 
// a network broadcast address
const IPAddress udpAddress(192, 168, 0, 127);
const int udpPort = 44444;

//Are we currently connected?
boolean connected = false;

//The udp library class

WiFiUDP udp;

int touchValue = 0;
int fsr1 =0;
int fsr2 = 0;


OSCMessage msg_start("/esp/millis/start");
OSCMessage msg_touch("/esp/touch");
OSCMessage msg_orientation("/esp/orientation");
OSCMessage msg_accel("/esp/accel");
OSCMessage msg_fsr("/esp/fsr");
OSCMessage msg_end("/esp/millis/end");

imu::Vector<3> euler;
imu::Vector<3> accel;

bool registered = false;

WiFiServer server(80);
bool bno_error = false;

void setup(){
  // Initialize hardware serial:
  Serial.begin(115200);
  //Wire.begin(4, 15); //Wire.begin(SDA, SCL);
    /* Initialise the BNO055 orientation sensor */
  if(!bno.begin())
  {
    /* There was a problem detecting the BNO055 ... check your connections */
    Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
    bno_error = true;
    //while(1);
  }
  /* Display the current temperature */
  if(!bno_error){
    int8_t temp = bno.getTemp();
    Serial.print("Current Temperature: ");
    Serial.print(temp);
    Serial.println(" C");
    Serial.println("");
    bno.setExtCrystalUse(true);
    Serial.println("Calibration status values: 0=uncalibrated, 3=fully calibrated");
  }
  
  
  //Connect to the WiFi network
  connectToWiFi(networkName, networkPswd);
  
}

void loop(){
  
  wifiserver();


  if(connected){
   
    //Send a packet

    fsr1 = analogRead(34);  // read the input on analog pin 15:
    //Serial.print("fsr1: ");
    //Serial.println(fsr1);      // print out the value you read:

    fsr2 = analogRead(35);  // read the input on analog pin 15:
    //Serial.print("fsr2: ");
    //Serial.println(fsr2);      // print out the value you read:
    
    //touchValue = touchRead(T2);  //GPIO 15
    //Serial.print("touch: ");
    //Serial.print(touchValue);
    ///Serial.println("");
    if(!bno_error){
      euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);
      accel = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
    }
    
    /* Display the floating point data */
    //Serial.print("X: ");
    //Serial.print(euler.x());
    //Serial.print(" Y: ");
    //Serial.print(euler.y());
    //Serial.print(" Z: ");
    //Serial.print(euler.z());
    //Serial.print("\t\t");
    
   
  
    
    //OSC send
    
    msg_start.add((unsigned int) millis());  
    udp.beginPacket(udpAddress, udpPort);
    msg_start.send(udp);
    udp.endPacket();
    msg_start.empty();
    
    /*
    msg_touch.add((float) touchValue);
    udp.beginPacket(udpAddress, udpPort);
    msg_touch.send(udp);
    udp.endPacket();
    msg_touch.empty();
    */

    msg_fsr.add((float) fsr1);
    msg_fsr.add((float) fsr2);
    udp.beginPacket(udpAddress, udpPort);
    msg_fsr.send(udp);
    udp.endPacket();
    msg_fsr.empty();

    if(!bno_error){
      msg_orientation.add((float) euler.x());
      msg_orientation.add((float) euler.y());
      msg_orientation.add((float) euler.z());
      udp.beginPacket(udpAddress, udpPort);
      msg_orientation.send(udp);
      udp.endPacket();
      msg_orientation.empty();

      msg_accel.add((float) accel.x());
      msg_accel.add((float) accel.y());
      msg_accel.add((float) accel.z());
      udp.beginPacket(udpAddress, udpPort);
      msg_accel.send(udp);
      udp.endPacket();
      msg_accel.empty();
    }
    

    
    msg_end.add((unsigned int) millis());
    udp.beginPacket(udpAddress, udpPort);
    msg_end.send(udp);
    udp.endPacket();
    msg_end.empty();
    
  }
  //Wait for 25ms
  delay(25);
}


void connectToWiFi(const char * ssid, const char * pwd){
  Serial.println("Connecting to WiFi network: " + String(ssid));

  // delete old config
  WiFi.disconnect(true);
  //register event handler
  
  if(!registered){
    registered = true;
    WiFi.onEvent(WiFiEvent);
  }
  
  
  //Initiate connection
  WiFi.begin(ssid, pwd);
  Serial.println("Waiting for WIFI connection...");

  //initiate server
  server.begin();
}

//wifi event handler
void WiFiEvent(WiFiEvent_t event){
    switch(event) {
      case SYSTEM_EVENT_STA_GOT_IP:
          //When connected set 
          Serial.print("WiFi connected! IP address: ");
          Serial.println(WiFi.localIP());  
          //initializes the UDP state
          //This initializes the transfer buffer
          udp.begin(WiFi.localIP(),udpPort);
          connected = true;
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          Serial.println("WiFi lost connection");
          connected = false;
          Serial.println("trying to reconnect");
          WiFi.disconnect(true);
          Serial.println("IP after disconnect");
          Serial.println(WiFi.localIP());
          //Initiate connection
          Serial.println("connect....");
          connectToWiFi(networkName, networkPswd);
          delay(1000);    
          break;
    }
}

void wifiserver(){
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            //information first
            client.println("Interface C1<br>");

            client.println("<br>");
            if(bno_error){
              client.println("Error in BNO055<br>");
            }
            
            client.println("<br>");
            
            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 2 off.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 2 on.<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(2, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(2, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

⚠️ **GitHub.com Fallback** ⚠️