ESP32: Laboratorios
ESP32: Laboratórios
A placa Heltec ESP32 LoRa é uma placa de desenvolvimento que integra três formas distintas de comunicação: Wifi, Bluetooth e a rede de comunicação de longo alcance LoRa. Além disto a placa apresenta um display OLED, o que facilita o controle da operação do dispositivo localizado em locais remotos.
- Referências sobre o Heltec ESP32
- Heltec ESP32+LoRa Series Quick Start: https://heltec-automation-docs.readthedocs.io/en/latest/esp32/quick_start.html
- Instalação da placa do Heltec ESP32 com a IDE do Arduino
- IDE -> Arquivo -> Preferências
- URLs Adicionais para Gerenciadores de Placa: https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series/releases/download/0.0.5/package_heltec_esp32_index.json
- Ferramentas -> Gerenciadores de Placa -> Instalar Heltec ESP32 Series Dev/boards
- Selecionar placa WiFi LoRa 32
Laboratório 1: Wifi
Exemplo pronto da biblioteca padrão do Arduino: Arquivo -> Exemplos -> Wifi -> SimpleWifiServer:
- Configurar apenas ssid e passwd da rede Wifi;
- Identificar via terminal serial o endereço IP recebido pelo ESP32;
- Será possível controlar led (gpio 5, ou outro) via nagerador Web.
Laboratório 2: Bluetooth
Exemplo pronto da biblioteca padrão do Arduino: Arquivo -> Exemplos -> BluetoothSerial -> SerialToSerialBT:
- Exemplo de interação bluetooth entre ESP32 e Android Serial Bluetooth Terminal.
- Carregar código no ESP32;
- Instalar aplicativo Serial Bluetooth Terminal no Android;
- Parear o ESP32 com o Android;
- Trocar mensagens seriais entre o terminal do ESP32 e o aplicativo Serial Bluetooth Terminal
Laboratório 3: Bluetooth Low Energy - BLE
Ver detalhes do BLE: https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/.
- Biblioteca: https://github.com/nkolban/ESP32_BLE_Arduino
- Exemplo pronto Arquivo -> Exemplos -> ESP BLE Arduino -> BLEserver;
- Instalar aplicativo BLE Terminal no Android;
- Conectar ao dispositivo.
Laboratório 4: Servidor Web e Display OLED
Exemplo de Web Server construído a partir de dois exemplos prontos
- Wifi: busca de redes e conexão baseadas no exemplo:
Arquivo -> Exemplos -> Heltec ESP Dev-Boards -> Factory_Tests -> WIFI_Kit_32_Factory_Test * Servidor Web: baseado no exemplo: Arquivo -> Exemplos -> Wifi -> SimpleWiFiServer
Código
<syntaxhighlight lang="c"> /*
* Web Server * * Wifi: busca de redes e conexão baseadas no exemplo: * Arquivo -> Exemplos -> Heltec ESP Dev-Boards -> Factory_Tests -> WIFI_Kit_32_Factory_Test * * Servidor Web: baseado no exemplo: * Arquivo -> Exemplos -> Wifi -> SimpleWiFiServer
- /
- include "heltec.h"
- include "WiFi.h"
- include "images.h"
WiFiServer server(80);
void logo(){ Heltec.display -> clear(); Heltec.display -> drawXbm(0,5,HelTec_LOGO_width,HelTec_LOGO_height,(const unsigned char *)HelTec_LOGO_bits); Heltec.display -> display();
delay(1000); Heltec.display->clear();
}
void wifi_logo(){
Heltec.display -> clear(); Heltec.display->drawXbm(34, 19, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits); Heltec.display -> display(); delay(1000); Heltec.display->clear();
}
void WIFISetUp(void)
{
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.disconnect(true);
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true);
WiFi.begin("Zhone_EB1C","aaaaabbbbb");
delay(100);
byte count = 0; while(WiFi.status() != WL_CONNECTED && count < 10) { count ++;
if (count == 1) Serial.println("Connecting...");
delay(500);
Heltec.display->clear();
Heltec.display -> drawString(0, 0, "Connecting..."); Heltec.display -> display(); }
Heltec.display -> clear(); if(WiFi.status() == WL_CONNECTED) { Heltec.display -> drawString(0, 0, "Connecting...OK."); Heltec.display -> display(); delay(500);
Heltec.display -> drawString(0, 10, "IP address: "); Heltec.display -> display(); delay(500); Heltec.display -> drawString(0, 20, WiFi.localIP().toString()); Heltec.display -> display(); delay(2000); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP());
} else { Heltec.display -> clear(); Heltec.display -> drawString(0, 0, "Connecting...Failed"); Heltec.display -> display();
Serial.println("Connecting...Failed");
// while(1); } Heltec.display -> drawString(0, 40, "WIFI Setup done"); Heltec.display -> display();
Serial.println("WIFI Setup done");
delay(500);
Heltec.display->clear();
}
void WIFIScan(void) { Heltec.display -> drawString(0, 20, "Scan start..."); Heltec.display -> display();
Serial.println("Scan start...");
int n = WiFi.scanNetworks(); Heltec.display -> drawString(0, 30, "Scan done"); Heltec.display -> display();
Serial.println("Scan done");
delay(500); Heltec.display -> clear();
if (n == 0) { Heltec.display -> clear(); Heltec.display -> drawString(0, 0, "no network found"); Heltec.display -> display();
Serial.println("No network found");
while(1); } else {
Serial.print(n);
Heltec.display -> drawString(0, 0, (String)n); Heltec.display -> drawString(14, 0, "networks found:"); Heltec.display -> display();
Serial.print(n); Serial.println(" networks found:");
delay(500);
for (int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found Heltec.display -> drawString(0, (i+1)*9,(String)(i + 1)); Heltec.display -> drawString(6, (i+1)*9, ":"); Heltec.display -> drawString(12,(i+1)*9, (String)(WiFi.SSID(i))); Heltec.display -> drawString(90,(i+1)*9, " ("); Heltec.display -> drawString(98,(i+1)*9, (String)(WiFi.RSSI(i))); Heltec.display -> drawString(114,(i+1)*9, ")"); // display.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
Serial.print(i+1); Serial.print(" : "); Serial.print(WiFi.SSID(i)); Serial.print(" ("); Serial.print(WiFi.RSSI(i)); Serial.println(")");
delay(10); } }
Heltec.display -> display(); delay(800); Heltec.display -> clear();
}
void setup() { pinMode(LED,OUTPUT);
Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Enable*/, true /*Serial Enable*/);
logo();
wifi_logo(); WIFIScan();
WIFISetUp();
//WiFi.disconnect(true);//重新初始化WIFI //delay(1000); //WiFi.mode(WIFI_STA); //WiFi.setAutoConnect(true);
server.begin(); Heltec.display -> drawString(0, 0, "Waiting Web Clients"); Heltec.display -> display();
}
void loop(){
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 Heltec.display -> clear(); Heltec.display -> drawString(0, 0, "New Web Client"); Heltec.display -> display(); 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(); // the content of the HTTP response follows the header: client.print("Click <a href=\"/H\">here</a> to turn the LED on.
"); client.print("Click <a href=\"/L\">here</a> to turn the LED off.
");
// 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(LED, HIGH); // GET /H turns the LED on Heltec.display -> drawString(0, 10, "Turn LED on"); Heltec.display -> display(); } if (currentLine.endsWith("GET /L")) { digitalWrite(LED, LOW); // GET /L turns the LED off Heltec.display -> drawString(0, 10, "Turn LED off"); Heltec.display -> display(); } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); }
}
Referências
Evandro.cantu (discussão) 15h48min de 8 de fevereiro de 2022 (-03)