Servo Sg90 - Pan dan Tilt
Coding 1 ( .ino ) :
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
#include <ArduinoOTA.h>
#include "WebHtml.h";
#define ServoPort1 D1
#define ServoPort2 D2
#define ServoPort3 D3
#define ServoPort4 D4
const char* ssid = "wificarjurnalku2";
const char* password = "wificarjurnalku2";
Servo myservo1, myservo2, myservo3, myservo4;
ESP8266WebServer server(80);
void handleRoot() {
String s = MAIN_page; //Ler o conteudo HTML
//server.send(200, "text/html", s); //Envia pagina da web
server.send(200, "text/html", MAIN_page); //--> Envia pagina da web
}
void handleServo(){
String POS1 = server.arg("servoPOS1");
int pos1 = POS1.toInt();
myservo1.write(pos1);
String POS2 = server.arg("servoPOS2");
int pos2 = POS2.toInt();
myservo2.write(pos2);
String POS3 = server.arg("servoPOS3");
int pos3 = POS3.toInt();
myservo3.write(pos3);
String POS4 = server.arg("servoPOS4");
int pos4 = POS4.toInt();
myservo4.write(pos4);
delay(15);
server.send(200, "text/plane","");
}
void setup() {
Serial.begin(115200);
delay(500);
myservo1.attach(ServoPort1);
myservo2.attach(ServoPort2);
myservo3.attach(ServoPort3);
myservo4.attach(ServoPort4);
WiFi.softAP(ssid, password);
IPAddress apip = WiFi.softAPIP();
Serial.print("Conecte seu laptop / celular wifi a este ponto de acesso NodeMCU : "); // cole o IP do esp para inciar a comunicação
Serial.println(ssid);
Serial.print("Entre com esse IP : ");
Serial.print(apip);
Serial.println(" No seu browser.");
server.on("/",handleRoot);
server.on("/setPOS",handleServo);
server.begin();
Serial.println("HTTP server iniciado");
ArduinoOTA.begin(); // enable to receive update/uploade firmware via Wifi OTA
}
void loop() {
ArduinoOTA.handle(); // listen for update OTA request from clients
server.handleClient();
}
Coding 2 ( WebHtml.h ) :
const char MAIN_page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
h1 {
font-family: arial;
font-size: 20px;
text-align: center;
background: #ccc;
display: inline-block;
padding: 7px;
}
body {
margin: 0;
padding: 0;
text-align: center;
color: #222;
}
label {
display: block;
margin-top: 20px;
}
input {
width: 50%;
}
</style>
</head>
<body>
<h1>Arm Robotic - Jurnalku </h1>
<br>
<h1>by Fajar himawan</h1>
<br><br>
<div class="container">
<div class="leftside wfxc">
<div class="slidecontainer">
<label>Servo 1</label>
<input type="range" min="0" max="180" value="90" id="myRange">
</div>
<div class="slidecontainer">
<label>Servo 2</label>
<input type="range" min="0" max="180" value="90" id="myRange1">
</div>
</div>
<div class="rightside wfxc">
<div class="slidecontainer">
<label>Servo 3</label>
<input type="range" min="0" max="180" value="90" id="myRange2"/>
</div>
<div class="slidecontainer">
<label>Servo 4</label>
<input type="range" min="0" max="180" value="90" id="myRange3"/>
</div>
</div>
</div>
<script>
function sendData(pos1,pos2,pos3,pos4) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.open("GET", "setPOS?servoPOS1="+pos1+"&servoPOS2="+pos2+"&servoPOS3="+pos3+"&servoPOS4="+pos4, true);
xhttp.send();
}
var s1=0,s2=0,s3=0,s4=0;
var slider1 = document.getElementById("myRange");
var slider2 = document.getElementById("myRange1");
var slider3 = document.getElementById("myRange2");
var slider4 = document.getElementById("myRange3");
//var output = document.getElementById("demo");
//output.innerHTML = slider.value;
slider1.oninput = function() {
//output.innerHTML = this.value;
s1 = slider1.value;
sendData(s1, s2, s3, s4);
}
slider2.oninput = function() {
//output.innerHTML = this.value;
s2 = slider2.value;
sendData(s1, s2, s3, s4);
}
slider3.oninput = function() {
//output.innerHTML = this.value;
s3 = slider3.value;
sendData(s1, s2, s3, s4);
}
slider4.oninput = function() {
//output.innerHTML = this.value;
s4 = slider4.value;
sendData(s1, s2, s3, s4);
}
</script>
</body>
</html>
)=====";
No comments