Sunspot Home

more notes for backup and a bad memory - - -

Using Amazon Alexa to control network devices

Objective - Control devices on my home network by verbal commands to an Amazon Alexa Dot

This project demonstrates control by Amazon Alexa of Raspberry Pi GPIO,
an ESP8266 by html code and the use of external bash or python etc. scripts running in the Pi

I experimented with Alexa running on a Raspberry Pi - it can work well but I was not able to get it to boot up after a power loss.
Also it would not find faux wemo devices
.

So I bought an Alexa Dot - it can control 16 WeMo devices and I modified this software that can make a a single python program on a Raspberry Pi simulate up to 16 WeMo devices.
This version of the basic program can control GPIO pins on the Pi

The python program fauxmo.py is for use with an ESP8266 - developed after this Sonoff work - see after "FAUXMOS =" where the list of devices can be modified
"kitchen radio" is the Sonoff in my list

The scripts on the Pi that the main program will run depending on the request to Alexa
These are simple Python GPIO controls but they could have any function in any language.

#!/usr/bin/env python

# /home/pi/Alexa-gpio/both_off.py

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.output(11,GPIO.LOW)
GPIO.output(13,GPIO.LOW)


#!/usr/bin/env python

# /home/pi/Alexa-gpio/both_on.py

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.output(11,GPIO.HIGH)
GPIO.output(13,GPIO.HIGH)


#!/usr/bin/env python

# /home/pi/Alexa-gpio/yellow_on.py

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# set up GPIO output channel
GPIO.setup(13, GPIO.OUT)
GPIO.output(13,GPIO.HIGH)


#!/usr/bin/env python

# /home/pi/Alexa-gpio/yellow_off.py

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
GPIO.output(11,GPIO.LOW)


#!/usr/bin/env python

# /home/pi/Alexa-gpio/yellow_on.py

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# set up GPIO output channel
GPIO.setup(13, GPIO.OUT)
GPIO.output(13,GPIO.HIGH)


#!/usr/bin/env python

# /home/pi/Alexa-gpio/red_off.py

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)

# set up GPIO output channel
GPIO.setup(13, GPIO.OUT)
GPIO.output(13,GPIO.LOW)



An ESP8266 - 01 program that uses the Arduino IDE - it uses a fixed IP address
GPIO 0 and 2 have LEDs connected via a resistor (>500 ohm) to earth

/home/pi/Alexa-gpio/Alexa_Pi_bridge-1.py -d above is now adapted for my Sonoff ESP8266 switch

/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
*/

#include <ESP8266WiFi.h>

//from a browser send - for example - http://192.168.0.104/(01)

// from a terminal send curl "http://192.168.0.104/ followed by -
// (00)" //0 off
// (01)" //0 on
// (20)" //2 off
// (21)" //2 on

const char* ssid = "my SSID";
const char* password = "my password";
int gpio0_pin = 0;
int gpio2_pin = 2;
int val0 = 0;
int val2 = 0;
int old_val0 = 0;
int old_val2 = 0;

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

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

// prepare GPIO0
pinMode(gpio0_pin, OUTPUT);
digitalWrite(gpio0_pin, 0);

// prepare GPIO2
pinMode(gpio2_pin, OUTPUT);
digitalWrite(gpio2_pin, 0);

// Connect to WiFi network
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");

// Start the server
server.begin();
Serial.println("Server started");

// config static IP
IPAddress ip(192, 168, 0, xxx); // where xxx is the desired IPAddress
IPAddress gateway(192, 168, 0,yyy); // set gatewa yyyy to match your network
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
WiFi.config(ip, gateway, subnet);

// Print the IP address
Serial.println(WiFi.localIP());
}
//=================================================== loop start
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}

// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();

// Match the request
// first digit is gpio number second digit is state requested

if (req.indexOf("(00)") != -1) //not equal to -1 means it was present
{val0 = 0; val2 = old_val2;} // 0 turns off, 2 does not change

if (req.indexOf("(01)") != -1)
{val0 = 1; val2 = old_val2;} // 0 turns on, 2 does not change

if (req.indexOf("(20)") != -1)
{val0 = old_val0; val2 = 0;} // 0 does not change, 2 turns off

if (req.indexOf("(21)") != -1)
{val0 = old_val0; val2 = 1;} // 0 does not change, 2 turns on

 

Serial.print("old_val0 = "); Serial.println(old_val0);
Serial.print("old_val2 = "); Serial.println(old_val2);
Serial.print("val0 = "); Serial.println(val0);
Serial.print("val2 = "); Serial.println(val2);

old_val0 = val0;
old_val2 = val2;

//else {
//Serial.println("invalid request");
//client.stop();
//return;
//}

// Set GPIO according to the request
digitalWrite(gpio2_pin, val2);
digitalWrite(gpio0_pin, val0);

client.flush();

// Prepare the response - for later use????
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val2)?"high":"low";
s += "<BR>";

String temp;
temp = "22.5"; // (more later?)

// Send the response to the client
client.print(s);
client.print("the temperature is - ");
client.print(temp);
client.print(" degrees");
client.print("</html>");
delay(1);
Serial.println("Client disonnected");

// The client will actually be disconnected
// when the function returns and 'client' object is destroyed
}
//=================================================== loop end


Please email me if you want to swap notes

SUNSPOT HOME