Skip to content

Instantly share code, notes, and snippets.

@igrr
Last active January 9, 2023 23:35
Show Gist options
  • Save igrr/9ef4d5c74355503e3b1f to your computer and use it in GitHub Desktop.
Save igrr/9ef4d5c74355503e3b1f to your computer and use it in GitHub Desktop.
#include <Servo.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char* ssid = "your-ssid";
const char* pass = "your-password";
ESP8266WebServer server(80);
Servo myservo;
void setup(void){
Serial.begin(115200);
Serial.println("");
WiFi.begin(ssid, pass);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", [](){
server.send(200, "text/plain", "Open /servo?value=90 to control servo");
});
server.on("/servo", [](){
String sval = server.arg("value");
int ival = sval.toInt();
Serial.print("Servo: ");
Serial.println(ival);
myservo.write(ival);
server.send(200, "text/plain", String(ival, DEC));
});
server.begin();
Serial.println("HTTP server started");
myservo.attach(D5); // Servo attached to D5 pin on NodeMCU board
myservo.write(0);
}
void loop(void){
server.handleClient();
}
@jtheires
Copy link

jtheires commented Sep 3, 2015

Thanks for posting this - I want to control a servo using only an ESP8266.
This code does not compile in my Arduino IDE (with esp add-in)...several issues (see Arduino IDE output below).
Is there an updated version of this code that compiles?

After some fussing, I was able to get this to work, by changing the invalid "D5" pin number mentioned in the error message below to a valid value (2, in my case, for GPIO2 on my esp-8266-01 module).
Thanks again for a great posting.

"Arduino: 1.6.4 (Windows 7), Board: "Generic ESP8266 Module, Serial, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS)"
WARNING: library Servo claims to run on [avr, sam] architecture(s) and may be incompatible with your current board which runs on [esp8266] architecture(s).
In file included from HTTPServo.ino:1:0:
C:\Users\Donatech\Documents\Arduino\libraries\Servo\src/Servo.h:67:2: error: #error "This library only supports boards with an AVR or SAM processor."
#error "This library only supports boards with an AVR or SAM processor."
^
HTTPServo.ino: In function 'void setup()':
HTTPServo:45: error: 'D5' was not declared in this scope
Multiple libraries were found for "Servo.h"
Used: C:\Users\Donatech\Documents\Arduino\libraries\Servo
Not used: C:\Users\Donatech\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\Servo
D5' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences."

@baol
Copy link

baol commented Oct 31, 2017

@jtheires you need to disinstall the Servo library, and use the one built-in the Arduino SDK for ESP8266.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment