View Single Post
Old 01-01-18, 09:59 AM   #3
Xringer
Lex Parsimoniae
 
Xringer's Avatar
 
Join Date: Feb 2009
Location: Woburn, MA
Posts: 4,918
Thanks: 114
Thanked 250 Times in 230 Posts
Default My IDE edited sketch

This will give you an idea of how the module code (sketch) works.
The Serial.printf lines will send text back to your PC, so you can see what the program is doing (use the IDE Serial Monitor, in Tools).
Once installed in the garage, those lines won't be needed..


/************************************************** ********************************
* Code for controlling multiple devices connected to one NodeMCU using Amazon Echo
*
* Written by Sid for Sid's E Classroom.. Edited 1/1/2018 by Rich
*
* https://www.youtube.com/c/SidsEClassroom
************************************************** *******************************/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "fauxmoESP.h"

#define WIFI_SSID "*******"//change your Wifi name
#define WIFI_PASS "*******"//Change your Wifi Password
#define SERIAL_BAUDRATE 115200

fauxmoESP fauxmo;
//declare switching pins
//Change pins according to your NodeMCU pinouts
#define Relay1 D1
#define Relay2 D2
#define Relay3 D3
#define Relay4 D4
// -----------------------------------------------------------------------------
// Wifi Setup
// -----------------------------------------------------------------------------

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);

// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();

// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
// -----------------------------------------------------------------------------
// Device Callback
// -----------------------------------------------------------------------------
void callback(uint8_t device_id, const char * device_name, bool state) {
Serial.print("Device "); Serial.print(device_name);
Serial.print(" state: ");
if (state) {
Serial.println("ON");
} else {
Serial.println("OFF");
}
//Switching action on detection of device name
if ( (strcmp(device_name, "Relay1") == 0) ) {
// adjust the relay immediately!
if (state) {
digitalWrite(Relay1, HIGH);
} else {
digitalWrite(Relay1, LOW);
}
}
if ( (strcmp(device_name, "Relay4") == 0) ) {
// adjust the relay immediately!
if (state) {
digitalWrite(Relay4, HIGH);
} else {
digitalWrite(Relay4, LOW);
}
}
if ( (strcmp(device_name, "Relay2") == 0) ) {
// adjust the relay immediately!
if (state) {
digitalWrite(Relay2, HIGH);
} else {
digitalWrite(Relay2, LOW);
}
}
if ( (strcmp(device_name, "Relay3") == 0) ) {
// adjust the relay immediately!
if (state) {
digitalWrite(Relay3, HIGH);
} else {
digitalWrite(Relay3, LOW);
}
}
}

void setup() {
//Initialize pins to Low on device start.. This WAS low, edited to High.
pinMode(Relay1, OUTPUT);
digitalWrite(Relay1, HIGH);
pinMode(Relay2, OUTPUT);
digitalWrite(Relay2, HIGH);
pinMode(Relay3, OUTPUT);
digitalWrite(Relay3, HIGH);
pinMode(Relay4, OUTPUT);
digitalWrite(Relay4, HIGH);

// Init serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println("FauxMo demo sketch");
Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");

// Wifi
wifiSetup();

// Device Names for Simulated Wemo switches
fauxmo.addDevice("Relay3");
fauxmo.addDevice("Relay1");
fauxmo.addDevice("Relay2");
fauxmo.addDevice("Relay4");
fauxmo.onMessage(callback);
}

void loop() {
fauxmo.handle();
}

~~~~
Sketch uses 252385 bytes (24%) of program storage space. Maximum is 1044464 bytes.
Global variables use 34960 bytes (42%) of dynamic memory, leaving 46960 bytes for local variables. Maximum is 81920 bytes.
__________________
My hobby is installing & trying to repair mini-splits
EPA 608 Type 1 Technician Certification ~ 5 lbs or less..
Xringer is offline   Reply With Quote