Close

Code (Version 0.1)

A project log for Voice Kitchen Faucet

de Bergerac The Dishes

Vije MillerVije Miller 08/29/2018 at 22:490 Comments

This is the bare bones version .. as opposed to a more elegant version one may make with map() the positions and a variable speed servo script

Relative to your voice command system .. simply have a series of keyword commands like 'kitchen' and 'hot' etc to load the NodeMCU ip address and add /hot=50 et cetera ..

// Voice Kitchen Faucet V.0.1 (NodeMCU)
// Vije Miller

#include <ESP8266WiFi.h>
#include <Servo.h>

const char* ssid = "ssid";
const char* pass = "password";
WiFiServer server(80);

Servo left;
Servo right;

// Servo Pins
const int ser_l = D6;
const int ser_r = D5;

// Version 2.0 would map() distance
// between off and peak temp ..

// Off
int l_off = 60;
int r_off = 110;

// Hot
int hfl = 70;
int hfr = 50;
int hol = 60;
int hor = 10;

// Mix
int mfl = 90;
int mfr = 75;
int mol = 130;
int mor = 35;

// Cold
int cfl = 110;
int cfr = 90;
int col = 160;
int cor = 90;

// Position
int pl = 70;
int pr = 90;

// LED
int led = D0;

void setup() {
  Serial.begin(115200);

  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);

  left.attach(ser_l);
  right.attach(ser_r);
  left.write(l_off);
  right.write(r_off);

  // Connect
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected");
  server.begin();
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {

  // Check
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  // Wait
  while (!client.available()) {
    delay(1);
  }
  // Read
  String query = client.readStringUntil('\r');
  client.flush();

  if (query.indexOf("/hot=50") != -1)  {
    hot(50);
  }
  if (query.indexOf("/hot=100") != -1)  {
    hot(100);
  }
  if (query.indexOf("/cold=50") != -1)  {
    cold(50);
  }
  if (query.indexOf("/cold=100") != -1)  {
    cold(100);
  }
  if (query.indexOf("/mix=50") != -1)  {
    mix(50);
  }
  if (query.indexOf("/mix=100") != -1)  {
    mix(100);
  }
  if (query.indexOf("/off") != -1)  {
    off();
  }

  delay(1);
}


void off() {
  left.write(l_off);
  right.write(r_off);
  delay(5);
}

void hot(int q) {
  if (q == 50) {
    pl = hfl;
    pr = hfr;
  } else if (q == 100) {
    pl = hol;
    pr = hor;
  }
  left.write(pl);
  right.write(pr);
  delay(5);
}

void mix(int q) {
  if (q == 50) {
    pl = mfl;
    pr = mfr;
  } else if (q == 100) {
    pl = mol;
    pr = mor;
  }
  left.write(pl);
  right.write(pr);
  delay(5);
}

void cold(int q) {
  if (q == 50) {
    pl = cfl;
    pr = cfr;
  } else if (q == 100) {
    pl = col;
    pr = cor;
  }
  left.write(pl);
  right.write(pr);
  delay(5);
}

Discussions