MicroMod WiFi Function Board - ESP32 Hookup Guide

Pages
Contributors: bboyho, Elias The Sparkiest
Favorited Favorite 0

Arduino Examples

Example 1: Connecting to WiFi

This example shows you how to send AT commands from your Processor Board to scan and connect to a WiFi Router. Note that this example runs once in the setup() function.

If you have not already, select your Board (in this case the MicroMod Artemis), and associated COM port. Copy and paste the code below in your Arduino IDE. Make sure to modify the SSID (YOUR_NETWORK_HERE) and password (YOUR_PASSWORD_HERE) for your wireless router. Hit the upload button and set the serial monitor to 115200 baud.

language:c
const char* newLineCarriageReturn = "\r\n";                                                     // Used at beginning of code to clear out anything in buffer
const char* toSend = "AT+GMR\r\n";                                                              // Check version information.
const char* enableSys = "AT+SYSLOG=1\r\n";                                                      // Enable AT error code prompt
const char* wifiMode = "AT+CWMODE=3\r\n";                                                       // Set the WiFi mode of ESP devices
const char* whatWifi = "AT+CWLAP\r\n";                                                          // List available APs
const char* connectTo = "AT+CWJAP=\"YOUR_NETWORK_HERE\",\"YOUR_PASSWORD_HERE\"\r\n";            // Connect an ESP station to a targeted AP, where YOUR_NETWORK_HERE is your network SSID, and YOUR_PASSWORD_HERE.
const char* wifiInfo = "AT+CWSTATE?\r\n";                                                       // Query the Wi-Fi state and Wi-Fi information
const char* atReset = "AT+RST\r\n";                                                             // Restart module
const char* whatStandard = "AT+CWAPPROTO?\r\n";                                                 // Sets the 802.11 b/g/n protocol standard of SoftAP mode
//const char* sendLight = "AT+HTTPCLIENT=1,3,192.168.1.116/TEMP86";                             // Send HTTP Client Request

//String composedMess = "";
//uint8_t powerEnableZero = A1;
//uint8_t powerEnableOne = 34;

void setup() {

  //  pinMode(powerEnableOne, OUTPUT);
  // pinMode(powerEnableZero, OUTPUT);

  Serial.begin(115200);  //Arduino Serial Monitor
  Serial1.begin(115200); //Hardware Serial Port connected to ESP32


  //Let user know that we are ready to begin sending AT commands
  Serial.println("We up.");

  Serial1.write(newLineCarriageReturn);
  delay(1000);//wait for ESP32

  Serial1.write(toSend);
  delay(1000);//wait for ESP32

  //check on ESP32 response
  while (Serial1.available()) {
    Serial.print(char(Serial1.read()));
  }

  Serial1.write(enableSys);
  delay(1000);//wait for ESP32

  //check on ESP32 response
  while (Serial1.available()) {
    Serial.print(char(Serial1.read()));
  }

  Serial1.write(wifiMode);
  delay(2000);//wait for ESP32

  //check on ESP32 response
  while (Serial1.available()) {
    Serial.print(char(Serial1.read()));
  }

  Serial1.write(whatWifi);
  delay(5000);//wait for ESP32

  //check on ESP32 response
  while (Serial1.available()) {
    Serial.print(char(Serial1.read()));
  }

  Serial1.write(connectTo);
  delay(5000);//wait for ESP32

  //check on ESP32 response
  while (Serial1.available()) {
    Serial.print(char(Serial1.read()));
  }

  Serial1.write(wifiInfo);
  delay(5000);//wait for ESP32

  //check on ESP32 response
  while (Serial1.available()) {
    Serial.print(char(Serial1.read()));
  }

  Serial.println("Done.");
  while (1);
  delay(2000);
}

void loop()
{
  //  digitalWrite(powerEnableZero, LOW);
  //  digitalWrite(powerEnableOne, LOW);
  while (1);

}

If all goes well, your ESP32 be configured for each AT command. At one point, the ESP32 will see what other wireless routers (if there are any) are in range before connecting to your WiFi router and providing a status on the connection to your network.

Example 2: Serial Passthrough

This example allows you to use your Processor Board as a serial passthrough to send characters to and from the ESP32 and the USB-to-serial converter. This is useful for testing different AT commands from the Arduino Serial Monitor or terminal window.

If you have not already, select your Board (in this case the MicroMod Artemis), and associated COM port. Copy and paste the code below in your Arduino IDE. Hit the upload button and set the serial monitor to 115200 baud.

language:c
char val; //init global var for serial characters being sent from ESP32

void setup()
{

  Serial.begin(115200); //Set up Serial Monitor
  Serial1.begin(115200); //Set up hardware UART to pipe data from the ESP32

}

void loop()
{

  if (Serial.available())
  {
    //If data comes in from Serial Monitor:
    //1.) echo the character back to the Serial Monitor
    //2.) send it to Hardware UART.

    val  = Serial.read(); //save character from Arduino's buffer to a variable

    //Serial.print(val); //display serial data back on the Arduino's Serial Monitor, disabled this line if using a Terminal Window
    Serial1.write(val); //send serial data to Processor Board's Hardware UART
  }

  if (Serial1.available())
  { // If data comes in from ESP32 connected to hardware UART,
    //display it on the Serial Monitor or Terminal Window
    Serial.write(Serial1.read());//display serial data received from 
  }

}