So I'm trying to write a program that allows me to turn a stepper motor on and off wirelessly through an app.
For the wireless connection, I use a NodeMcu ESP8266 and for the motor driver, I use an EasyDriver v4.5.
I programmed the app with MIT App Inventor and the app works well.
The problem is that either my motor turns 1 step (after I turn it on through the app) (first program), or it keeps rotating when I turn the motor on through the app but I can't turn it off (second program). I've tried so many things for days but none of them seem to work.
So what I want is that when I press the "LedAan" button in the app, the motor keeps rotating until I press the "LedUit" button in the app.
I think the problem in the first program is that the wifi client request is "blocking" the other code unless it receives an input (through my app).
I'm sure the driver and motor are working correctly since those work flawlessly in a standalone program I wrote.
I've only been programming for a year so I'm not that good and my way of working / programming might be inefficient.
First program:
#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const char WiFiPassword[] = "StarTracker";
const char AP_NameChar[] = "StarTracker" ;
#define SCREEN_WIDTH 128 //breedte OLED scherm (in pixels)
#define SCREEN_HEIGHT 64 //hoogte OLED scherm (in pixels)
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //configureert het display met de juiste settings
WiFiServer server(80);
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form>
";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form>
";
String html_4 = "</div></body></html>";
String request = "";
static const uint8_t LED = 16;
int BuiltinLedHIGH = 0;
int BuiltinLedLOW = 1;
int StarTrackerMain = 0;
int MotorStep = 12;
int MotorDir = 14;
int OLEDPRINTED = 0;
void setup()
{
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, BuiltinLedLOW);
pinMode (14, OUTPUT); //MotorDir output
digitalWrite (14, LOW); //MotorDir LOW
pinMode (12, OUTPUT); //MotorStep output
digitalWrite (12, LOW); //MotorStep LOW
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("LED = Uit");
display.display();
boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
server.begin();
} // void setup()
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
request = client.readStringUntil('\r');
if ( request.indexOf("LedAan") > 0 ) {
StarTrackerMain = 1;
OLEDPRINTED = 0;
}
else if ( request.indexOf("LedUit") > 0 ) {
StarTrackerMain = 0;
OLEDPRINTED = 0;
}
if (StarTrackerMain == 1 & OLEDPRINTED == 0) {
digitalWrite(LED, BuiltinLedHIGH);
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.println("LED = Aan");
display.display();
OLEDPRINTED = 1;
}
if (StarTrackerMain == 1) {
digitalWrite (12, HIGH);
delay(1);
digitalWrite (12, LOW);
delay(1);
}
if (OLEDPRINTED == 0) {
digitalWrite(LED, BuiltinLedLOW);
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.println("LED = Uit");
display.display();
OLEDPRINTED = 1;
}
client.flush();
client.print( header );
client.print( html_1 );
client.print( html_2 );
client.print( html_3 );
client.print( html_4);
delay(5);
// The client will actually be disconnected when the function returns and 'client' object is detroyed
}
Second program:
#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const char WiFiPassword[] = "StarTracker";
const char AP_NameChar[] = "StarTracker" ;
#define SCREEN_WIDTH 128 //breedte OLED scherm (in pixels)
#define SCREEN_HEIGHT 64 //hoogte OLED scherm (in pixels)
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //configureert het display met de juiste settings
WiFiServer server(80);
String header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
String html_1 = "<!DOCTYPE html><html><head><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>";
String html_2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='LED ON' ></form>
";
String html_3 = "<form id='F2' action='LEDOFF'><input class='button' type='submit' value='LED OFF' ></form>
";
String html_4 = "</div></body></html>";
String request = "";
static const uint8_t LED = 16;
int BuiltinLedHIGH = 0;
int BuiltinLedLOW = 1;
int StarTrackerMain = 0;
int MotorStep = 12;
int MotorDir = 14;
char user_input;
int OLED = 0;
void setup()
{
Serial.begin(9600);
pinMode(LED, OUTPUT);
digitalWrite(LED, BuiltinLedLOW);
pinMode (14, OUTPUT); //MotorDir output
digitalWrite (14, LOW); //MotorDir LOW
pinMode (12, OUTPUT); //MotorStep output
digitalWrite (12, LOW); //MotorStep LOW
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("LED = Uit");
display.display();
boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
server.begin();
}
void loop() {
WiFiclient();
MotorOn();
MotorOff();
}
void WiFiclient() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
request = client.readStringUntil('\r');
if ( request.indexOf("LedAan") > 0 ) {
OLED = 1;
StarTrackerMain = 1;
}
else if ( request.indexOf("LedUit") > 0 ) {
OLED = 1;
StarTrackerMain = 0;
}
client.flush();
client.print( header );
client.print( html_1 );
client.print( html_2 );
client.print( html_3 );
client.print( html_4);
delay(5);
}
void MotorOn() {
if (StarTrackerMain == 1 & OLED == 1) {
digitalWrite(LED, BuiltinLedHIGH);
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.println("LED = Aan");
display.display();
OLED = 0;
}
while (StarTrackerMain == 1) {
digitalWrite (12, HIGH);
delay(1);
digitalWrite (12, LOW);
delay(1);
}
}
void MotorOff() {
if (StarTrackerMain == 0 & OLED == 1) {
digitalWrite(LED, BuiltinLedLOW);
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.println("LED = Uit");
display.display();
OLED = 0;
}
}