GPIO not switching..code issue?
Posted: Sun Jan 12, 2020 4:47 pm
Hi Peeps, can someone have a nosie through this failry simple sketch pls and see if you can see why my gpio outputs are not triggering my relays? Ive been though it for days trying to figure it out!
Cheers!
Steve
Cheers!
Steve
Code: Select all
/*
* based on-
* Rui Santos project
* Complete Project Details http://randomnerdtutorials.com
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#define WIFI_SSID "wifipass"
#define WIFI_PASS "password"
#define REMOTE_BTN_1 4 //up (D2)
#define REMOTE_BTN_2 14//down (D5)
#define REMOTE_BTN_3 12 //select (D6)
#define REMOTE_BTN_4 5//stop (D1)
#define UP "blinds Up"
#define Down "blinds Down"
#define Light_Block "Blinds light Block"
fauxmoESP fauxmo;
void setup() {
// Init serial port and clean garbage
Serial.begin(115200);
Serial.println();
// Wi-Fi connection
wifiSetup();
// Set Buttons
pinMode(REMOTE_BTN_1, OUTPUT); //up
digitalWrite(REMOTE_BTN_1, HIGH);
pinMode(REMOTE_BTN_2, OUTPUT); //down
digitalWrite(REMOTE_BTN_2, LOW);
pinMode(REMOTE_BTN_3, OUTPUT); //select
digitalWrite(REMOTE_BTN_3, HIGH);
pinMode(REMOTE_BTN_4, OUTPUT); //stop
digitalWrite(REMOTE_BTN_4, HIGH);
fauxmo.createServer(true); // not needed, this is the default value
fauxmo.setPort(80); // This is required for gen3 devices
fauxmo.enable(true);
// You can use different ways to invoke alexa to modify the devices state:
// "Alexa, turn lamp two on"
fauxmo.addDevice(UP);
fauxmo.addDevice(Down);
fauxmo.addDevice(Light_Block);
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) { //command recieved
// Callback when a command from Alexa is received.
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if ( (strcmp(device_name, UP) == 0) ) { //blinds up
down();
}
if ( (strcmp(device_name, Down) == 0) ) { //blinds down
// this just sets a variable that the main loop() does something about
Serial.println("BtnDown 2 called by Alexa");
digitalWrite(REMOTE_BTN_1, LOW);
delay(1000);//1 second
Serial.println("low");
digitalWrite(REMOTE_BTN_1, HIGH);
Serial.println("high");
}
if ( (strcmp(device_name, Light_Block) == 0) ) { //set blinds to block light
// this just sets a variable that the main loop() does something about
Serial.println("Block light btn3 called by Alexa");
digitalWrite(REMOTE_BTN_1, LOW);//open fully
delay(1000);//1 second
digitalWrite(REMOTE_BTN_1, HIGH);
delay(30); //wait for blonds to open fully
digitalWrite(REMOTE_BTN_3, LOW);//select first blind
delay(1000);//1 seconds
digitalWrite(REMOTE_BTN_3, HIGH);
delay(500);
digitalWrite(REMOTE_BTN_1, LOW);//HIGHer it for 12.5 seconds
delay(500);
digitalWrite(REMOTE_BTN_1, HIGH);
delay(12500);//12.5 seconds
digitalWrite(REMOTE_BTN_4, LOW);//stop it
delay(1000);//1 second
digitalWrite(REMOTE_BTN_4, HIGH);
digitalWrite(REMOTE_BTN_3, LOW);//select second blind
delay(1000);//1 seconds
digitalWrite(REMOTE_BTN_3, HIGH);
digitalWrite(REMOTE_BTN_3, LOW);//raise it for ...seconds
delay(8000);//8 seconds
digitalWrite(REMOTE_BTN_3, HIGH);
//etc
}
});
}
void loop() {
// fauxmoESP uses an async TCP server but a sync UDP server
// Therefore, we have to manually poll for UDP packets
fauxmo.handle();
}
void down() {
Serial.println("BtnUp 1 called by Alexa");
digitalWrite(REMOTE_BTN_1, LOW);
delay(1000);//1 second
Serial.println("low");
digitalWrite(REMOTE_BTN_1, HIGH);
Serial.println("high");
}
// Wi-Fi Connection
void wifiSetup() {
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("attempting to connect to wifi");
// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Connected!
Serial.printf("Connected to - SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}