-
yuichitamiya authoredyuichitamiya authored
esp32-switch.md 9.51 KiB
ESP32-CAM to Switch both
Setup ESP-32 CAM Programming Environment
Ref. ESP32-CAM_JP
Additional Boards manager URLs:
https://dl.espressif.com/dl/package_esp32_index.json
Boards Manager
ESP32
Setting
Connection
USB-Serial | ESP32-CAM |
---|---|
RX | U0T (UART 0 TX) |
TX | U0R (UART 0 RX) |
VCC | 5V |
GND | GND |
|IO0 to GND (When Programming)
!!! Note
Select Bootloader Mode
GPIO0
The ESP32 will enter the serial bootloader when GPIO0 is held low on reset. Otherwise it will run the program in flash.
GPIO0 Input| Mode
--|--
Low/GND | ROM serial bootloader for esptool
High/VCC|Normal execution mode
GPIO0 has an internal pullup resistor, so if it is left unconnected then it will pull high.
Ref. [Boot Mode Selection](https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/boot-mode-selection.html)
Code
void setup()
{
Serial.begin(9600); // 通信速度
}
void loop()
{
Serial.println("Hello, World"); // Hello, Worldを出力
delay(1000); // 1秒待つ
}
Serial Monitor
- IO0 disconnect to GND(connect to internal Pull-up to execution mode)
- Push
rest button
on ESP32-CAM
Switch bot
Ref. ESP32 Swich bot_JP
Code
//アクセスするときはURLパラメーターの最後に&をつけること!
#include <WiFi.h>
// 使用するWi-Fiとそのパスワードに書き換えてください
const char* ssid = "fablabkannai";
const char* password = "marumie";
// ポート80番を使用
WiFiServer server(80);
// HTTPリクエストを格納する変数
String header;
// 値の設定に使用する変数
String valueString = String(5);
String delay_valueString = String(500);
int pos1 = 0;
int pos2 = 0;
//pin
const int servo_pin = 16;
//サーボモーターの回転角
const int servo_left = 26;
const int servo_center = 75;
const int servo_right = 123;
void setup() {
pinMode(4, OUTPUT);
//ledc setting
ledcSetup(0, 50, 10); // 0ch 50 Hz 10bit resolution
ledcAttachPin(servo_pin, 0); // 15pin, 0ch
//シリアル通信開始
Serial.begin(115200);
//起動時にフラッシュライトを点滅させる
for(int i=0; i<5; i=i+1) {
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
delay(50);
}
// Wi-Fiに接続
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// ローカルIPを表示(このIPにスマホなどからアクセスします)
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in 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
header += c;
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("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" http-equiv=\"content-type\" charset=\"utf-8\"><title>ESP32 servo controller</title>");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>.block{display: inline-block; vertical-align: middle; height: 50px; font-size: 0;}");
client.println(".btn-square-shadow {display: inline-block; width: 300px; height: 50px; text-decoration: none; background: #668ad8; color: #FFF; border-bottom: solid 4px #627295; border-radius: 3px; font-size: 30px;}");
client.println(".btn-square-shadow:active {-webkit-transform: translateY(4px); transform: translateY(4px); box-shadow: 0px 0px 1px rgba(0, 0, 0, 0.2); border-bottom: none;}");
client.println("</style>");
client.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>");
// Web Page
client.println("</head><body style='text-align: center;'><h1>ESP32 サーボモーター コントロールパネル</h1>");
//servo slide bar <--------------------------------------------------------------------------------------------------------------------------------------------------------------------ESP32's IP address. To find open serial monitor with 115200bps
client.println("<div class='left block'> <a href='http://192.168.2.111/?angle=50&delay=500&'><button id='entrance_button' class='btn-square-shadow' type='button'>玄関の電気</button></a></div>");
client.println("<div class='right block'><a href='http://192.168.2.111/?angle=100&delay=500&'><button id='room_button' class='btn-square-shadow' type='button'>部屋の電気</button></a></div>");
client.println("</body></html>");
//HTTPリクエストの処理部分
delay_valueString = "500";
pos1 = header.indexOf('=',18);
pos2 = header.indexOf('&',18);
if(pos1!=-1 && pos2!=-1) {
delay_valueString = header.substring(pos1+1, pos2);
Serial.print("pos:");
Serial.println(pos1);
Serial.println(pos2);
} else {
Serial.print("defaultサーボ待機時間:");
Serial.println(delay_valueString.toInt());
}
Serial.print("サーボ待機時間:");
Serial.println(delay_valueString.toInt());
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
Serial.print("URLパラメーター:");
Serial.println(valueString);
if(valueString.toInt()>=26 && valueString.toInt()<=123) {
//サーボモーターを回転
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
ledcWrite(0, servo_center);
delay(delay_valueString.toInt());
ledcWrite(0, valueString.toInt());
delay(delay_valueString.toInt());
ledcWrite(0, servo_center);
// バグ防止
delay(delay_valueString.toInt());
ledcWrite(0, 0);
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
} else {
Serial.println("URLパラメーター エラー!");
}
// HTTPレスポンスの終了
client.println();
// Break out of the while loop
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
// Clear the header variable
header = "";
// 接続を切断
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Connection
ESP32-CAM | |
---|---|
5V | 5V (Power+) |
GND | GND(Power-) |
3V3 | Servo VCC |
IO16 | Servo signal |
GND | Servo GND |
How to use
Find IP address
- Open Serial monitor and Find IP address
Replace the IP address in this part of the code
client.println("<div class='left block'> <a href='http://192.168.2.111/?angle=50&delay=500&'><button id='entrance_button' class='btn-square-shadow' type='button'>玄関の電気</button></a></div>");
client.println("<div class='right block'><a href='http://192.168.2.111/?angle=100&delay=500&'><button id='room_button' class='btn-square-shadow' type='button'>部屋の電気</button></a></div>");
Direct switch
Access from browser
http://192.168.2.111/?angle=50&delay=500&
- 192.168.2.111: loacal IP address of ESP32-CAM in Arduio Sketch
- angle : servo rotate degree (23-123)
- delay: set delay when servo move back
Switches on screen
Open in browser
http://192.168.2.111