I am trying to connect my alexa to an arduino to control an EV charger with relays. I am trying to setup my arduino code so i can send a command to my alexa saying "Power my home with my EV." I believe i setup everything correctly but I cannot get the alexa app to find my arduino. Can you help me? Code Pasted Below.
My Code:
#include "thingProperties.h"
// Pin connected to the relay
const int relayPin = 7;
// Timing variables
unsigned long lastManualInteraction = 0;
const unsigned long manualOverrideTimeout = 2 * 60 * 60 * 1000; // 2 hours in milliseconds
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
delay(1500);
// Initialize the relay pin
pinMode(relayPin, OUTPUT);
// Set the relay to the correct state according to the schedule at startup
updateRelayState();
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Automatically resume schedule after 2 hours of no manual interaction
if (millis() - lastManualInteraction > manualOverrideTimeout) {
updateRelayState();
}
}
void updateRelayState() {
// Get the current local time in seconds since the Unix epoch
unsigned long currentTime = ArduinoCloud.getLocalTime();
// Convert seconds to a tm structure
time_t rawTime = currentTime;
struct tm *timeInfo = localtime(&rawTime);
int currentHour = timeInfo->tm_hour;
if (currentHour >= 7 && currentHour < 21) {
// Between 7 AM and 9 PM, the vehicle should be discharging (relay off)
digitalWrite(relayPin, LOW);
Serial.println("Vehicle is discharging, home is being powered by the EV.");
} else {
// All other times, the vehicle should be charging (relay on)
digitalWrite(relayPin, HIGH);
Serial.println("Vehicle is charging.");
}
}
// Function called when ChargerFunction changes from the IoT Cloud dashboard or Alexa
void onChargerFunctionChange() {
if (chargerFunction) {
digitalWrite(relayPin, HIGH);
Serial.println("Charger turned on manually via dashboard or Alexa.");
} else {
digitalWrite(relayPin, LOW);
Serial.println("Charger turned off manually via dashboard or Alexa.");
}
// Record the time of manual interaction
lastManualInteraction = millis();
}
// Function called when the Alexa CloudSwitch changes
void onPowermyhomewithmyevChange() {
if (powermyhomewithmyev) {
digitalWrite(relayPin, LOW); // Turn off the relay to power the home with EV
Serial.println("Alexa command: Powering home with EV.");
} else {
digitalWrite(relayPin, HIGH); // Turn on the relay to start charging the EV
Serial.println("Alexa command: Charging EV.");
}
// Record the time of manual interaction
lastManualInteraction = millis();
}
Here are the thing properties:
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char SSID[] = SECRET_SSID; // Network SSID (name)
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
void onPowermyhomewithmyevChange();
void onChargerFunctionChange();
CloudSwitch powermyhomewithmyev;
bool chargerFunction;
void initProperties(){
ArduinoCloud.addProperty(powermyhomewithmyev, READWRITE, ON_CHANGE, onPowermyhomewithmyevChange);
ArduinoCloud.addProperty(chargerFunction, READWRITE, ON_CHANGE, onChargerFunctionChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);