Instantly share code, notes, and snippets.

Embed
What would you like to do?
arduino stepper curtain controller
// Load Wi-Fi library
#include <ESP8266WiFi.h>
// Replace with your network credentials
const char* ssid = "haha";
const char* password = "noway";
// Set web server port number to 80
WiFiServer server(80);
// Pin numbers for motor.
const int PUL = 14; // D5
const int DIR = 4; // D2
const int ENA = 15; // D8
// Variable to store the HTTP request
String header;
// Number of steps left.
int steps_left = 0;
int direction_written = 0;
void setup() {
Serial.begin(9600);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Set the pins.
pinMode(PUL, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(ENA, OUTPUT);
digitalWrite(ENA,LOW);
// Start the server.
server.begin();
}
void MaybeRunMotor() {
if (steps_left != 0) {
if (steps_left < 0) {
if (!direction_written) {
digitalWrite(ENA,LOW);
digitalWrite(DIR, HIGH);
direction_written = 1;
}
++steps_left;
} else {
if (!direction_written) {
digitalWrite(ENA,LOW);
digitalWrite(DIR, LOW);
direction_written = 1;
}
--steps_left;
}
// Driver configuration: 110110.
for (int i = 0; i < 800; ++i) {
digitalWrite(PUL, HIGH);
delayMicroseconds(500);
digitalWrite(PUL, LOW);
delayMicroseconds(500);
}
} else {
direction_written = 0;
}
}
void loop(){
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("/left") > 0) {
client.println("Motor-state: left");
Serial.println("Motor-state: left");
int leftIndex = header.indexOf("/left");
int spaceIndex = header.indexOf(" ", leftIndex);
if (spaceIndex > 0) {
int steps = header.substring(leftIndex + 6, spaceIndex).toInt();
steps_left = steps;
} else {
steps_left = 10;
}
direction_written = 0;
} else if (header.indexOf("/right") > 0) {
client.println("Motor-state: right");
Serial.println("Motor-state: right");
int rightIndex = header.indexOf("/right");
int spaceIndex = header.indexOf(" ", rightIndex);
if (spaceIndex > 0) {
int steps = header.substring(rightIndex + 7, spaceIndex).toInt();
steps_left = -steps;
} else {
steps_left = -10;
}
direction_written = 0;
} else if (header.indexOf("/off") > 0) {
client.println("Motor-state: off");
Serial.println("Motor-state: off");
steps_left = 0;
} else if (header.indexOf("/enahigh") > 0) {
digitalWrite(ENA, HIGH);
client.println("Ena: HIGH");
Serial.println("Ena: HIGH");
} else if (header.indexOf("/enalow") > 0) {
digitalWrite(ENA, LOW);
client.println("Ena: LOW");
Serial.println("Ena: LOW");
}
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
MaybeRunMotor();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment