- cleanup, interpret /motor/1/123

This commit is contained in:
FrYakaTKoP
2017-10-07 17:26:37 +02:00
parent 6333e98e32
commit 81871b0965
3 changed files with 111 additions and 169 deletions

View File

@@ -1,14 +1,9 @@
# Esp8266-Laser # Esp8266-Laser
Wifi Host for the Arduino-Laser-Spirograph Wifi Host for the Arduino-Laser-Spirograph
# install instructions #install
- install arduino ide (1.8.5) - install arduino ide (1.8.5)
- follow this instruction to install esp8266-arduino
https://github.com/esp8266/Arduino#installing-with-boards-manager
- get and install driver for nodemcu - get and install driver for nodemcu
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

View File

@@ -24,9 +24,9 @@
console.log(uri); console.log(uri);
var xh = new XMLHttpRequest(); var xh = new XMLHttpRequest();
xh.onreadystatechange = function(){ xh.onreadystatechange = function(){
if (xh.readyState == 4){ //if (xh.readyState == 4){
if(xh.status == 200) { //if(xh.status == 200) {
if (this.responseText != null) { // if (this.responseText != null) {
var res = JSON.parse(xh.responseText); var res = JSON.parse(xh.responseText);
var ledDiv; var ledDiv;
if(initial) if(initial)
@@ -73,7 +73,7 @@
{ {
ledElement.checked = false; ledElement.checked = false;
} }
} // }
if(initial) if(initial)
{ {
@@ -84,8 +84,8 @@
} }
} //}
} //}
} }
}; };
xh.overrideMimeType("text/json"); xh.overrideMimeType("text/json");
@@ -136,7 +136,8 @@
} }
} }
} }
console.log("res: " + res);
} }
} }
}; };

View File

@@ -29,9 +29,12 @@
#include <WiFiClient.h> #include <WiFiClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <DNSServer.h>
#include <FS.h> #include <FS.h>
/*
#include <DNSServer.h>
*/
#define DBG_OUTPUT_PORT Serial #define DBG_OUTPUT_PORT Serial
const char *ssid = "ESP-test"; const char *ssid = "ESP-test";
@@ -42,16 +45,15 @@ const char *myHostname = "esp8266";
const char *metaRefreshStr = "<head><meta http-equiv=\"refresh\" content=\"3; url=http://192.168.4.1/index.html\" /></head><body><p>redirecting...</p></body>"; const char *metaRefreshStr = "<head><meta http-equiv=\"refresh\" content=\"3; url=http://192.168.4.1/index.html\" /></head><body><p>redirecting...</p></body>";
/*
// DNS server // DNS server
const byte DNS_PORT = 53; const byte DNS_PORT = 53;
DNSServer dnsServer; DNSServer dnsServer;
*/
// LED Animations // LED Animations
unsigned long previousMillis = 0; unsigned long previousMillis = 0;
bool ledAniRider = 0;
bool ledAniFlipper = 0;
bool ledAniDir = 0;
int ledAniPos = 0;
int interval = 100; // ms int interval = 100; // ms
/* Soft AP network parameters */ /* Soft AP network parameters */
@@ -61,10 +63,6 @@ IPAddress netMsk(255, 255, 255, 0);
// Web server // Web server
ESP8266WebServer server(80); ESP8266WebServer server(80);
// Gpios with LEDs
byte ledIoNames[] = {5, 4, 13, 12, 14, 16 }; // GPIO 5&4 12&13 flipped on my board so dirty patch here 5<->4 12<->13
//format bytes //format bytes
String formatBytes(size_t bytes){ String formatBytes(size_t bytes){
if (bytes < 1024){ if (bytes < 1024){
@@ -136,40 +134,55 @@ void handleFileList() {
server.send(200, "text/json", output); server.send(200, "text/json", output);
} }
void handleLeds() // void handleLeds()
{ // {
if(server.arg("LED") ) { // if(server.arg("LED") ) {
int pin = server.arg("LED").toInt(); // int pin = server.arg("LED").toInt();
bool onOFF = server.arg("state").toInt(); // bool onOFF = server.arg("state").toInt();
digitalWrite(pin, onOFF); // digitalWrite(pin, onOFF);
} // }
String json = "{ \"leds\": ["; // String json = "{ \"leds\": [";
for (int i = 0; i < sizeof(ledIoNames); i++) // for (int i = 0; i < sizeof(ledIoNames); i++)
{ // {
bool state = digitalRead(ledIoNames[i]); // bool state = digitalRead(ledIoNames[i]);
json += "{ \"" + String(i) + "\": ["; // json += "{ \"" + String(i) + "\": [";
json += "\""+String(ledIoNames[i])+"\", "; // json += "\""+String(ledIoNames[i])+"\", ";
json += "\""+String(state)+"\" ] }"; // json += "\""+String(state)+"\" ] }";
if(!(i == sizeof(ledIoNames)-1)) // if(!(i == sizeof(ledIoNames)-1))
json += ","; // json += ",";
} // }
json += "] }"; // json += "] }";
server.send(200, "text/json", json); // server.send(200, "text/json", json);
json = String(); // json = String();
} // }
void clearAll() { void handleMotor()
//stop Animations {
ledAniRider = 0; Serial.println("handleMotor");
ledAniFlipper = 0; if(server.arg("motor") ) {
ledAniPos = 0; int pin = server.arg("motor").toInt();
ledAniDir = 0; bool onOFF = server.arg("state").toInt();
// clear all leds digitalWrite(pin, onOFF);
for (int i = 0; i < sizeof(ledIoNames); i++) { }
digitalWrite(ledIoNames[i], LOW); // String json = "{ \"leds\": [";
} // for (int i = 0; i < sizeof(ledIoNames); i++)
// {
// bool state = digitalRead(ledIoNames[i]);
// json += "{ \"" + String(i) + "\": [";
// json += "\""+String(ledIoNames[i])+"\", ";
// json += "\""+String(state)+"\" ] }";
// if(!(i == sizeof(ledIoNames)-1))
// json += ",";
// }
// json += "] }";
server.send(200, "text/plain", "blub");
// server.send(200, "text/json", json);
// json = String();
} }
void setup(void){ void setup(void){
@@ -187,11 +200,6 @@ void setup(void){
DBG_OUTPUT_PORT.printf("\n"); DBG_OUTPUT_PORT.printf("\n");
} }
// Setup LED pins
for (int i = 0; i < sizeof(ledIoNames); i++) {
pinMode(ledIoNames[i],OUTPUT);
}
//WIFI INIT //WIFI INIT
DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid); DBG_OUTPUT_PORT.printf("Connecting to %s\n", ssid);
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
@@ -202,9 +210,11 @@ void setup(void){
DBG_OUTPUT_PORT.print("Connected! IP address: "); DBG_OUTPUT_PORT.print("Connected! IP address: ");
DBG_OUTPUT_PORT.println ( WiFi.softAPIP() ); DBG_OUTPUT_PORT.println ( WiFi.softAPIP() );
/* Setup the DNS server redirecting all the domains to the apIP */ /*
//Setup the DNS server redirecting all the domains to the apIP
dnsServer.setErrorReplyCode(DNSReplyCode::NoError); dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(DNS_PORT, "*", apIP); dnsServer.start(DNS_PORT, "*", apIP);
*/
// Setup MDNS responder // Setup MDNS responder
if (!MDNS.begin(myHostname)) { if (!MDNS.begin(myHostname)) {
@@ -219,123 +229,59 @@ void setup(void){
//SERVER INIT //SERVER INIT
//list directory //list directory
server.on("/listFiles", HTTP_GET, handleFileList); server.on("/listFiles", HTTP_GET, handleFileList);
//called when the url is not defined here server.on("/heap", HTTP_GET, [](){
//use it to load content from SPIFFS
server.onNotFound([](){
if(!handleFileRead(server.uri()))
server.send(302, "text/html", metaRefreshStr);
});
// handle led requests
server.on("/leds", HTTP_GET, handleLeds);
server.on("/clearAll", HTTP_GET, clearAll);
server.on("/rider", HTTP_GET, [](){
ledAniPos = 0;
ledAniDir = 0;
ledAniFlipper = 0;
ledAniRider = 1;
});
server.on("/flipper", HTTP_GET, [](){
ledAniPos = 0;
ledAniDir = 0;
ledAniRider = 0;
ledAniFlipper = 1;
});
//get heap status, analog input value and all GPIO statuses in one json call
server.on("/all", HTTP_GET, [](){
String json = "{"; String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap()); json += "\"heap\":"+String(ESP.getFreeHeap());
json += ", \"analog\":"+String(analogRead(A0));
json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "}"; json += "}";
server.send(200, "text/json", json); server.send(200, "text/json", json);
json = String(); json = String();
}); });
server.onNotFound(handleNotFound);
server.begin(); server.begin();
DBG_OUTPUT_PORT.println("HTTP server started"); Serial.println("HTTP server started");
} }
void handleNotFound() {
Serial.println("handleNotFound");
String uri = server.uri();
Serial.println(uri);
// /motor/1/123
if(uri.substring(0,7) == "/motor/")
{
Serial.println("/motor/");
int motorNr = uri.substring(7,8).toInt();
int motorValue = uri.substring(9).toInt();
Serial.print("motorNr:");
Serial.println(motorNr);
Serial.print("motorValue:");
Serial.println(motorValue);
}
if(!handleFileRead(server.uri()))
{
//server.send(302, "text/html", metaRefreshStr);
server.send(302, "text/plain", "blub");
}
}
void loop(void){ void loop(void){
//DNS
/*
//DNS
dnsServer.processNextRequest(); dnsServer.processNextRequest();
//HTTP */
server.handleClient();
//HTTP
server.handleClient();
// LED Animation //
if(ledAniRider || ledAniFlipper) unsigned long currentMillis = millis();
{ if (currentMillis - previousMillis >= interval)
unsigned long currentMillis = millis(); {
if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis;
// save the last time you blinked the LED }
previousMillis = currentMillis;
if(ledAniRider)
{
if(ledAniDir == 0)
{
if(ledAniPos < sizeof(ledIoNames))
{
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos -1], LOW);
ledAniPos++;
}
else
{
ledAniDir = 1;
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos +1], LOW);
ledAniPos--;
}
}
else
{
if(ledAniPos >= 0)
{
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos +1], LOW);
ledAniPos--;
}
else
{
ledAniDir = 0;
digitalWrite(ledIoNames[ledAniPos], HIGH);
digitalWrite(ledIoNames[ledAniPos -1], LOW);
ledAniPos++;
}
}
}
else if(ledAniFlipper)
{
if(ledAniDir == 0)
{
if(ledAniPos < sizeof(ledIoNames))
{
digitalWrite(ledIoNames[ledAniPos], HIGH);
ledAniPos++;
}
else
{
ledAniDir = 1;
digitalWrite(ledIoNames[ledAniPos], LOW);
ledAniPos--;
}
}
else
{
if(ledAniPos >= 0)
{
digitalWrite(ledIoNames[ledAniPos], LOW);
ledAniPos--;
}
else
{
ledAniDir = 0;
digitalWrite(ledIoNames[ledAniPos], HIGH);
ledAniPos++;
}
}
}
}
}
} }