116 lines
3.1 KiB
C++
116 lines
3.1 KiB
C++
/*
|
|
Change these lines as per yours:
|
|
const char *ssid = "REPLACE_WITH_YOUR_SSID"; // replace with your SSID
|
|
const char *password = "REPLACE_WITH_YOUR_PASSWORD"; // replace with your Password
|
|
const uint8_t servoPin = D4; // replace with servo pin
|
|
|
|
Parts list:
|
|
|
|
NodeMCU :
|
|
https://amzn.to/397GzNe
|
|
|
|
Download ESPAsyncWebServer library :
|
|
https://github.com/me-no-dev/ESPAsyncWebServer/archive/master.zip
|
|
|
|
Download ESPAsyncTCP library :
|
|
https://github.com/me-no-dev/ESPAsyncTCP/archive/master.zip
|
|
|
|
Download ESP8266 Filesystem Uploader :
|
|
https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.5.0/ESP8266FS-0.5.0.zip
|
|
|
|
*/
|
|
|
|
// Import libraries
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESPAsyncTCP.h>
|
|
#include <ESPAsyncWebServer.h>
|
|
#include <AccelStepper.h>
|
|
#include <string>
|
|
|
|
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
|
|
bool initializing = false;
|
|
|
|
// ULN2003 Motor Driver Pins
|
|
#define IN1 5
|
|
#define IN2 4
|
|
#define IN3 14
|
|
#define IN4 12
|
|
|
|
// initialize the stepper library
|
|
AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
|
|
|
|
const char *ssid = "TARDIS"; // replace with your SSID
|
|
const char *password = "EvangelionUnit-01"; // replace with your Password
|
|
|
|
// Create Server instance
|
|
AsyncWebServer server(80);
|
|
void setup()
|
|
{
|
|
// Attach Servo, start SPIFFS and Connect to WiFi
|
|
Serial.begin(115200);
|
|
stepper.setMaxSpeed(3000);
|
|
stepper.setAcceleration(2500);
|
|
// set target position
|
|
stepper.moveTo(0);
|
|
|
|
|
|
if (!SPIFFS.begin())
|
|
{
|
|
Serial.println("An Error has occurred while mounting SPIFFS");
|
|
return;
|
|
}
|
|
WiFi.begin(ssid, password);
|
|
Serial.print("Connecting to WiFi..");
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
{
|
|
delay(1000);
|
|
Serial.print(".");
|
|
}
|
|
Serial.print("\nConnected to the WiFi network: ");
|
|
Serial.print(WiFi.SSID());
|
|
Serial.print("IP address:");
|
|
Serial.print(WiFi.localIP());
|
|
// Send home page from SPIFFS
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
|
Serial.println("Send index.html.");
|
|
request->send(SPIFFS, "/index.html", "text/html");
|
|
});
|
|
|
|
|
|
server.on("/setTemperature", HTTP_POST, [](AsyncWebServerRequest *request) {
|
|
int rotations = request->arg("rotations").toInt();
|
|
double multiplier = 0.2; // TODO: find me out
|
|
stepper.moveTo((multiplier * rotations) * stepsPerRevolution);
|
|
request->send(200);
|
|
});
|
|
|
|
|
|
//rotate a looot back to 90 F, as we cant go lower, so we over rotate then mark that as 0 POSITION
|
|
server.on("/initialize", HTTP_POST, [](AsyncWebServerRequest *request) {
|
|
initializing = true;
|
|
stepper.moveTo(-10 * stepsPerRevolution);
|
|
request->send(200);
|
|
});
|
|
|
|
|
|
// Send Favicon
|
|
server.serveStatic("/favicon.ico", SPIFFS, "/favicon.ico");
|
|
// Begin Server
|
|
server.begin();
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
{
|
|
if (stepper.distanceToGo() == 0 && initializing){
|
|
initializing = false;
|
|
stepper.stop();
|
|
stepper.setCurrentPosition(0);
|
|
}
|
|
|
|
Serial.print("Current Position: ");
|
|
Serial.println(stepper.currentPosition());
|
|
stepper.run();
|
|
}
|