PCOMP 5: ANALOG / DIGITAL I/O REFRESHER, WIFI-CONTROL

After a lot of audio synthesis with the Arduino-tone library I went back to the labs and played with servo, peak detection and explored WIFI-control. 

I mounted a thumb-piano on the servo and controlled the movements with an analog pressure sensor.

I got excited with this control (servos are really really exciting!), so I rewrote a WIFI-control script to control everything from my ESP8266 nodeMCU module via WIFI (locally).

Here my (pretty messy ...) re-edit of the code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

#include <Servo.h>
     // include the servo library
 
Servo servoMotor;       // creates an instance of the servo object to control a servo
int servoPin = 13;       // Control pin for servo motor
 
MDNSResponder mdns;
 
ESP8266WebServer server(80);
String webPage;
const char* ssid     = "enter SSID here";      //wifi name
const char* password = "enter your pw here";  //wifi password
 
void setup() {
  servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object

 
  pinMode(16, OUTPUT);  //led pin 16
 
  webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 ";
  webPage += "<a href=\"socket1On\"><button>ON</button></a>&nbsp;";
  webPage += "<a href=\"socket1Off\"><button>OFF</button></a></p>";
  
  Serial.begin(74880);
  delay(100);
 
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  if (mdns.begin("esp8266", WiFi.localIP())) 
    Serial.println("MDNS responder started");
 
  server.on("/", [](){
    int servoAngle = 180;
    server.send(200, "text/html", webPage);
  });
  server.on("/socket1On", [](){
    int servoAngle = 180;
    server.send(200, "text/html", webPage);
    // Turn off LED
    servoMotor.write(servoAngle - 90);
    delay(1000);
  });
  server.on("/socket1Off", [](){
    int servoAngle = 180;
    server.send(200, "text/html", webPage);
    //Turn on LED
    servoMotor.write(servoAngle);
    delay(1000); 
  });

  server.begin();
  Serial.println("HTTP server started");
}
 
void loop() {
  server.handleClient();
  
}

... and here we go:

After this I completed the last labs with state change, treshold, peak detection. I liked the possibility to clean up a signal by adding a noise-variable to it. 

On the weekend an ITP-alumn showed me how to control from p5 and write this into a txt-file via php on my server. This is awesome! I can check the contents of this file from my ESPmodule via web-sockets and control a servo in Micro-Python with it. I need to put all the parts together this week, but that means I can write a sketch that lets the user control physical devices from everywhere. I am excited!