Skip to content

Instantly share code, notes, and snippets.

@smukkejohan
Created November 30, 2016 13:04
Show Gist options
  • Select an option

  • Save smukkejohan/bd9ee0651ea01fe617e165eebddc5965 to your computer and use it in GitHub Desktop.

Select an option

Save smukkejohan/bd9ee0651ea01fe617e165eebddc5965 to your computer and use it in GitHub Desktop.
svg ellipse joystick webserver kea example
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "KEAIOT";
const char* password = "IOT16KEA";
ESP8266WebServer server(80);
const int led = 13;
const int drainXpin = 4;
const int drainYpin = 5;
const int clickPin = 16;
int joystickXValue = 0;
int joystickYValue = 0;
void handleRoot() {
digitalWrite(led, 1);
joystickXValue = getJoystickX();
joystickYValue = getJoystickY();
String output;
output = "<!DOCTYPE html> <html> <head>";
output += " <meta http-equiv='refresh' content='2'> <style> #svgelem {";
output += "position: relative;";
//output += "left: 50%; -webkit-transform: translateX(-20%); -ms-transform: translateX(-20%);";
//output += "transform: translateX(-20%);}
output += "</style>";
output += "<title>JOY</title>";
output += "<meta charset='utf-8' /></head><body>";
output += "<svg id='svgelem' height='200' xmlns='http://www.w3.org/2000/svg'>";
output += "<ellipse id='redcircle' cx='60' cy='60' rx='" + String(joystickXValue/100.0) + "' ry='" + String(joystickYValue/100.0) + "' fill='red'/>";
output += "</svg>";
output +="</body> </html>";
server.send(200, "text/html", output);
}
bool getIsClicked() {
}
int getJoystickX() {
pinMode(drainXpin, INPUT);
digitalWrite(drainXpin, LOW);
pinMode(drainYpin, OUTPUT);
digitalWrite(drainYpin, LOW);
return analogRead(0);
}
int getJoystickY() {
pinMode(drainXpin, OUTPUT);
digitalWrite(drainXpin, LOW);
pinMode(drainYpin, INPUT);
digitalWrite(drainYpin, LOW);
return analogRead(0);
}
void handleNotFound(){
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void){
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// 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());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", [](){
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment