Code for EchoDot
C/C++This is the code for esp8266, to communicate with Echo dot, and control the load (in this case the load is lamp).
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
#include <functional>
#define lamp1 D7
#define lamp2 D6
#define lamp3 D5
#define lamp4 D0
#define LED D2
boolean connectWifi();
//on/off callbacks
void lightOneSwitch();
void lightTwoSwitch();
void lightThreeSwitch();
void lightFourSwitch();
void lightOn();
void lightOff();
void lighthold();
String sendlight;
// Change this before you flash
const char* ssid = "Lab Mikro";
const char* password = "duemilanove";
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *lightOne = NULL;
Switch *lightTwo = NULL;
Switch *lightThree = NULL;
Switch *lightFour = NULL;
Switch *light = NULL;
Switch *hold = NULL;
boolean wifiConnected = false;
void setup(){
Serial.begin(115200);
pinMode(lamp1, OUTPUT);
pinMode(lamp2, OUTPUT);
pinMode(lamp3, OUTPUT);
pinMode(lamp4, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(lamp1, LOW);
digitalWrite(lamp2, LOW);
digitalWrite(lamp3, LOW);
digitalWrite(lamp4, LOW);
digitalWrite(LED, LOW);
wifiConnected = connectWifi();
if(wifiConnected){
upnpBroadcastResponder.beginUdpMulticast();
lightOne = new Switch("First Light", 80, lightOneSwitch, lightOneSwitch);
lightTwo = new Switch("Second Light", 81, lightTwoSwitch, lightTwoSwitch);
lightThree = new Switch("Third Light", 82, lightThreeSwitch, lightThreeSwitch);
lightFour = new Switch("Fourth Light", 83, lightFourSwitch, lightFourSwitch);
light = new Switch("Light", 84, lightOn, lightOff);
hold = new Switch("pause", 85, lighthold , lighthold);
upnpBroadcastResponder.addDevice(*lightOne);
upnpBroadcastResponder.addDevice(*lightTwo);
upnpBroadcastResponder.addDevice(*lightThree);
upnpBroadcastResponder.addDevice(*lightFour);
upnpBroadcastResponder.addDevice(*light);
upnpBroadcastResponder.addDevice(*hold);
}
}
void loop(){
if(wifiConnected){
upnpBroadcastResponder.serverLoop();
lightOne->serverLoop();
lightTwo->serverLoop();
lightThree->serverLoop();
lightFour->serverLoop();
light->serverLoop();
hold->serverLoop();
}
// Serial.println("tesi tesi");
// delay(100);
}
void lightOff() {
sendlight="zero";
Serial.print(sendlight);
}
void lightOn() {
sendlight="one";
Serial.print(sendlight);
}
void lighthold() {
sendlight="two";
Serial.print(sendlight);
}
void lightOneSwitch() {
digitalWrite(lamp1, !digitalRead(lamp1));
// Serial.println("lamp1");
}
void lightTwoSwitch() {
digitalWrite(lamp2, !digitalRead(lamp2));
// Serial.println("lamp2");
}
void lightThreeSwitch() {
digitalWrite(lamp3, !digitalRead(lamp3));
// Serial.println("lamp3");
}
void lightFourSwitch() {
digitalWrite(lamp4, !digitalRead(lamp4));
// Serial.println("lamp4");
}
// connect to wifi returns true if successful or false if not
boolean connectWifi(){
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting ...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 10){
state = false;
break;
}
i++;
}
if (state){
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
String command;
int lampstate;
int defaultstate;
int startstate;
int endstate;
int i;
void setup() {
DDRD = (1 << PD5) | (0 << PD2);
PORTD = (1 << PD5) | (1 << PD2);
EICRA = (1 << ISC00) | (1 << ISC01);
EIMSK = (1 << INT0);
sei();
startstate = 920;
endstate = 40;
Serial.begin(115200);
}
void loop() {
skip:
if(Serial.available()){
command = Serial.readString();
if(command == "one"){
endstate = 40;
for( i = startstate;i >= endstate;i--){
lampstate=i;
delay(10);
startstate = lampstate;
if(Serial.available()){
command = Serial.readString();
if(command == "two" || command == "zero"){
goto skip;
}
}
}
}
else if(command == "zero"){
startstate = 920;
for( i = endstate;i <= startstate;i++){
lampstate=i;
delay(10);
endstate = lampstate;
if(Serial.available()){
command = Serial.readString();
if(command == "two" || command == "one"){
goto skip;
}
}
}
}
}
}
ISR(INT0_vect){
_delay_us(440);
PORTD &= ~(1 << PD5);
delayMicroseconds(lampstate * 10);
PORTD |= (1 << PD5);
_delay_us(10);
PORTD &= ~(1 << PD5);
}
Comments