Hi all,
I've been working through a project controlling a 12v diesel heater for my houseboat.
Basically its an Arduino, 12v shield for power & a relay which is connected to soldered wires on the back of the main heater control panel on/off switch.
I modified some code which emulates a Belkin wifi switch (I honestly cant remember where to give credit, probably kakopappa) & is picked up by alexa, then when it activates it pulses the relay for 600ms then turns the relay off emulating the button press, turning off is the same but with a longer delay of the relay turned on to turn off the heater.
Its all working 100%, i have control & heater fires up & shuts down as it should.
The last little niggle for me is that the relay is only on when it needs to be & not on all the time when the heaters running, so Alexa sees the pulse & registers it on then its default to registered off when the heaters running.
Does anyone know how Alexa sets its state ? I figured it was with isHeaterButtonOn but even setting that to true it turns off anyway, I'm actually now wondering if i can take it out of the loop function & set it to On afterwards?
bool HeaterButtonOn() {
int i;
Serial.println("Heater On");
for(i=0;i<2;i++)
{
Serial.println("Heater On");
digitalWrite(Heater_pin,HIGH);
delay(600);
digitalWrite(BUILTIN_LED,HIGH);
delay(100);
digitalWrite(Heater_pin,LOW);
digitalWrite(BUILTIN_LED,LOW);
}
isHeaterButtonOn = true;
return isHeaterButtonOn;
}
Full code below
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
/* For Alexa */
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
unsigned long previousMillis = 0;
long int startDelayMilliseconds;
int delayInMilliSeconds = 10*1000;
const long interval = 1000;
boolean connectWifi();
const int Heater_pin=D1;
//on/off callbacks
bool HeaterButtonOn();
bool HeaterButtonOff();
// Change this before you flash
const char* ssid = "****";
const char* password = "****";
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *HeaterButton = NULL;
bool isHeaterButtonOn = false;
void setup()
{
pinMode(BUILTIN_LED, OUTPUT); // set onboard LED as output
Serial.begin(9600);
Serial.println("Setup");
digitalWrite(BUILTIN_LED,LOW);
//PIN SETUP
pinMode(Heater_pin, OUTPUT);
digitalWrite(Heater_pin,LOW);
wifiConnected = connectWifi();
if (wifiConnected) {
upnpBroadcastResponder.beginUdpMulticast();
/* Switch setup */
HeaterButton = new Switch("Diesel Heater" ,83,HeaterButtonOn, HeaterButtonOff);
Serial.println("Adding new Heater switch device" );
upnpBroadcastResponder.addDevice(*HeaterButton);
}
}
void loop()
{
if (wifiConnected) {
upnpBroadcastResponder.serverLoop();
HeaterButton->serverLoop();
}
}
bool HeaterButtonOn() {
int i;
Serial.println("Heater On");
for(i=0;i<2;i++)
{
Serial.println("Heater On");
digitalWrite(Heater_pin,HIGH);
delay(600);
digitalWrite(BUILTIN_LED,HIGH);
delay(100);
digitalWrite(Heater_pin,LOW);
digitalWrite(BUILTIN_LED,LOW);
}
isHeaterButtonOn = true;
return isHeaterButtonOn;
}
bool HeaterButtonOff() {
int i;
// Turning Heater off is same as turning it on - just pressing button
Serial.println("Heater Off");
for(i=0;i<2;i++)
{
Serial.println("Heater Off");
digitalWrite(Heater_pin,HIGH);
delay(1200);
digitalWrite(BUILTIN_LED,HIGH);
delay(100);
digitalWrite(Heater_pin,LOW);
digitalWrite(BUILTIN_LED,LOW);
}
isHeaterButtonOn = false;
return isHeaterButtonOn;
}
// 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;
}