Kybernetiks
Published © GPL3+

Voice Controlled Wooden Edison Lamp - Question Price $5

"I have not failed. I've just found 10'000 ways that won't work. " - Thomas Alva Edison

IntermediateShowcase (no instructions)700
Voice Controlled Wooden Edison Lamp - Question Price $5

Things used in this project

Hardware components

Sonoff Basic
Itead Sonoff Basic
×1
Silicon Labs CP2102 USB 2.0 to TTL UART Module FT232
×1

Software apps and online services

Arduino IDE
Arduino IDE
Android Studio
Android Studio

Story

Read more

Schematics

Wifi Relay SONOFF pins

Connecting Wi-Fi relay for flashing.

Code

Android main code parts, to use voice recognition.

Java
You just need to put it in you app. For example - to put on processing the pressing of a button.
// Main code to start speech recognition.
// You can put it's execution, on some button in your app.

        Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak please");
        startActivityForResult(speechIntent, RESULT_SPEECH_TO_TEXT);
    

// Then on onAcivityResult we will get result

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RESULT_SPEECH_TO_TEXT && resultCode == RESULT_OK) {

            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            if (!matches.isEmpty()) {
              
              String result_recognition_string = matches.get(0);

		// convert to lower case, since google speech recognition
                 // returns "different" results. For example: youtube defines //as YouTube
                result_recognition_string = result_recognition_string.toLowerCase(Locale.getDefault());


                  if (result_recognition_string.contains("lamp on") ) {
			// lamp on 
			new LightOn().execute();
                }
             }
    }
}

Sonoff esp8266 code

C/C++
Firmware for SONOFF Wi-Fi relay. To use it through your home router.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
 
// name and password from WiFi network
const char* ssid = "Your access point (router) name";
const char* password = "router password";


IPAddress ip(192,168,1,112); // enter static ip
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

 
// server port 80
 
WiFiServer server(80);
 
void setup() {
 Serial.begin(115200);
 delay(100);
 
 //preparing GPIO

 pinMode(12, OUTPUT);
 digitalWrite(12, 1);

 pinMode(13, OUTPUT);
 digitalWrite(13, 1);


 
 // connecting to WiFi
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 WiFi.config(ip, gateway, subnet);
 
 // waiting for connection
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
 
 // run server
 server.begin();
 Serial.println("Server started");
 
 // show ip
 Serial.println(WiFi.localIP());
}
 
void loop() {
 
 // connection check
 WiFiClient client = server.available();
 if (!client) {
 return;
 }
 
 // Waiting for data
 Serial.println("new client");
 while (!client.available()) {
 delay(1);
 }
 
 // Reading the first line of the query
 String req = client.readStringUntil('\r');
 Serial.println(req);
 client.flush();

 // works with GPIO
 if (req.indexOf("/12/0") != -1)
 digitalWrite(12, 0);
 else if (req.indexOf("/12/1") != -1){
 digitalWrite(12, 1);
 Serial.println("TEST OK");
 String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nTest OK. Uptime: ";


 // UpTime
 int Sec = (millis() / 1000UL) % 60;
 int Min = ((millis() / 1000UL) / 60UL) % 60;
 int Hours = ((millis() / 1000UL) / 3600UL) % 24;
 int Day = ((millis() / 1000UL) / 3600UL / 24UL);
 s += Day;
 s += "d ";
 s += Hours;
 s += ":";
 s += Min;
 s += ":";
 s += Sec;
 s += "</html>\n";
 client.print(s);
 client.stop();
 return;
 }
 else
 // If an invalid query write error
 {
 Serial.println("invalid request");
 String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nInvalid request";
 s += "</html>\n";
 client.print(s);
 client.stop();
 return;
 }
 
 client.flush();
 
 
 // Response formation
 String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO set OK";
 s += "</html>\n";
 
 // Send the response to the client
 client.print(s);
 delay(1);
 Serial.println("Client disonnected");
 
}

Android code to work with WiFi

Java
This code is responsible for sending commands to the server (our light bulb)
public class LightOn extends AsyncTask<Void,Void,Void> {

    private static final String LOG_TAG = "MyLog";

    @Override
    protected Void doInBackground(Void... params) {

        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL("http://192.168.1.112/12/1"); // "0" to turn off 

            urlConnection = (HttpURLConnection) url
                    .openConnection();

            InputStream in = urlConnection.getInputStream();

            InputStreamReader isw = new InputStreamReader(in);

            int data = isw.read();
            while (data != -1) {
                char current = (char) data;
                data = isw.read();
                System.out.print(current);
                Log.e(LOG_TAG, "Reply from server - " + current);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }

        return null;
    }
}



// run it
new LightOn().execute();

Android code work with WiFi

Java
This code is responsible for sending commands to the server (our light bulb)
public class LightOn extends AsyncTask<Void,Void,Void> {

    private static final String LOG_TAG = "MyLog";

    @Override
    protected Void doInBackground(Void... params) {

        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL("http://192.168.1.112/12/1"); // "0" to turn off 

            urlConnection = (HttpURLConnection) url
                    .openConnection();

            InputStream in = urlConnection.getInputStream();

            InputStreamReader isw = new InputStreamReader(in);

            int data = isw.read();
            while (data != -1) {
                char current = (char) data;
                data = isw.read();
                System.out.print(current);
                Log.e(LOG_TAG, "Reply from server - " + current);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }

        return null;
    }
}



// run it
new LightOn().execute();

Credits

Kybernetiks

Kybernetiks

0 projects • 1 follower

Comments