General Information Applications Server Routing

Multiple servos connected to Wemos ESP8266 D1 mini

Post Reply
iaot / applications     Views: 33Prev .. Next
Multiple servos connected to Wemos ESP8266 D1 miniPosted: Wednesday, July 3, 2024 [06:25:28] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 363
Switching away from expensive and USB only Phidgets to a simpler WiFi-based ESP8266.
Modifying existing Arduino code worked flawlessly with two and more Servos.

Multiple servos connected to Wemos ESP8266 D1 mini
This is a code:
View Code#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Servo.h>

MDNSResponder mdns;
Servo ACServo; // A/C Toggle Switch
Servo SWServo; // Modem Switch
static const int ServoGPIO1 = D1;
static const int ServoGPIO2 = D2;

const char* ssid = "SSID";
const char* password = "Password";

ESP8266WebServer server(80);

String webPage = "";

void setup(){
webPage += "<h1>Switches</h1><p>Modem: <a href=\"on\"><button>ON</button></a> <a href=\"off\"><button>OFF</button></a></p><p>A/C Toggle: <a href=\"toggle\"><button>Toggle</button></a></p>";
Serial.begin(115200);
SWServo.attach(ServoGPIO1);
AcServo.attach(ServoGPIO2);
// Next two code lines are optional
// Bring servos to the positions you want on boot
SWServo.write(110);
ACServo.write(30);
delay(1000);
Serial.print("Making connection to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
// Default Web page
server.on("/", [](){
server.send(200, "text/html", webPage);
// Next two lines are optional
SWServo.write(110);
ACServo.write(30);
});
// Switch ON
server.on("/on", [](){
server.send(200, "text/html", webPage);
SWServo.write(199);
delay(1000);
SWServo.write(110);
delay(1000);
});
// Switch OFF
server.on("/off", [](){
server.send(200, "text/html", webPage);
SWServo.write(10);
delay(1000);
SWServo.write(110);
delay(1000);
});
// TOGGLE
server.on("/toggle", [](){
server.send(200, "text/html", webPage);
ACServo.write(150);
delay(1000);
ACServo.write(30);
delay(1000);
});

server.begin();
Serial.println("HTTPserver started");
}

void loop(){
server.handleClient();
}


Just enter your WiFi credentials and change servos stop numbers.
Not sure if all GPIO pins are capable of controlling servos.
Tested only from D1 thru D4 pins.There's no place like ~
iaot / applicationsPrev .. Next
 
Post Reply
Home - Iaot: General Information Applications Server Routing
Our Telegram Group