sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
227 stars 121 forks source link

for multi switches and dimmer with pwm and zero crossing #365

Closed sewaks3 closed 4 months ago

sewaks3 commented 4 months ago

Dear sir i need complete code for 3 relays 2 for lights and 1 for Fan ac dimmer module and i want to control with iot and physical switch and a pot for ac dimmer
i create the code like this but i unable to add physical switches and pot(Potentometer) please . error shown wen i select esp8266 nodemcu "Dose not compiled or esp8266" and this code is not compiled for esp8266 its only compile for esp32 , please guide me, i want to compile for esp8266 nodemcu or esp12E i mention my cose below.

But this code only work with esp32

//Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif

#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include <WiFi.h>
#endif

#include <RBDdimmer.h> //https://github.com/RobotDynOfficial/RBDDimmer

#include "SinricPro.h"
#include "SinricProDimSwitch.h"
#include "SinricProSwitch.h"
#define WIFI_SSID         "GNXS-7E3420"
#define WIFI_PASS         "12345678"
#define APP_KEY           "xxxxxxxxxxxxxxxxxxx"   // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define DIMSWITCH_ID      "xxxxxxxxxxxxxxxxxxx"   // Should look like "5dc1564130xxxxxxxxxxxxxx"

#define SWITCH_ID_1       "xxxxxxxxxxxxxxxxxx"

#define SWITCH_ID_2       "xxxxxxxxxxxxxxxxxxxxxxxxxx"

#define BAUD_RATE         9600                // Change baudrate to your need

const int ZC_PIN  = 5;
const int PWM_PIN  = 4;
const int MIN_POWER  = 30;
const int MAX_POWER  = 95;
int power  = 0;

#define RELAYPIN_1 12
#define RELAYPIN_2  13
const int RELAYPIN_3 = 14;
dimmerLamp acd(PWM_PIN, ZC_PIN);

// we use a struct to store all states and values for our dimmable switch
struct {
  bool powerState = false;
  int powerLevel = 0;
} device_state;

void setPWM(int powerLevel) {
  int power = map(powerLevel, 0, 100, MIN_POWER, MAX_POWER); // Map power level from 0 to 100, to a value between 0 to 80
  acd.setPower(power);
  delay(30);
}

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state ? "on" : "off");
  digitalWrite(RELAYPIN_3, state ? LOW : HIGH);

  device_state.powerState = state;
  setPWM(state ? 100 : 0);
  return true; // request handled properly
}

bool onPowerLevel(const String &deviceId, int &powerLevel) {
  device_state.powerLevel = powerLevel;
  setPWM(powerLevel);
  Serial.printf("Device %s power level changed to %d\r\n", deviceId.c_str(), device_state.powerLevel);
  return true;
}

bool onAdjustPowerLevel(const String &deviceId, int &levelDelta) {
  device_state.powerLevel += levelDelta;
  Serial.printf("Device %s power level changed about %i to %d\r\n", deviceId.c_str(), levelDelta, device_state.powerLevel);
  levelDelta = device_state.powerLevel;
  setPWM(levelDelta);
  return true;
}

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state ? "on" : "off");
 digitalWrite(RELAYPIN_1, state ? LOW : HIGH);
 return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
 Serial.printf("Device %s power turned %s \r\n", deviceId.c_str(), state ? "on" : "off");
 digitalWrite(RELAYPIN_2, state ? LOW : HIGH);
 return true; // request handled properly
}

void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");

#if defined(ESP8266)
  WiFi.setSleepMode(WIFI_NONE_SLEEP);
  WiFi.setAutoReconnect(true);
#elif defined(ESP32)
  WiFi.setSleep(false);
  WiFi.setAutoReconnect(true);
#endif

  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  IPAddress localIP = WiFi.localIP();
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

void setupSinricPro() {

    pinMode(RELAYPIN_1, OUTPUT);
  pinMode(RELAYPIN_2, OUTPUT);

  SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];
  mySwitch1.onPowerState(onPowerState1);

  SinricProSwitch& mySwitch2 = SinricPro[SWITCH_ID_2];
  mySwitch2.onPowerState(onPowerState2);

  //SinricPro.begin(APP_KEY, APP_SECRET);  

  SinricProDimSwitch &myDimSwitch = SinricPro[DIMSWITCH_ID];
 myDimSwitch.onPowerState(onPowerState);
  myDimSwitch.onPowerLevel(onPowerLevel);
  myDimSwitch.onAdjustPowerLevel(onAdjustPowerLevel);
  // set callback function to device

  pinMode(RELAYPIN_3, OUTPUT);
  // setup SinricPro
  SinricPro.onConnected([]() {
    Serial.printf("Connected to SinricPro\r\n");
  });
  SinricPro.onDisconnected([]() {
    Serial.printf("Disconnected from SinricPro\r\n");
  });
  SinricPro.begin(APP_KEY, APP_SECRET);
}

void setupRBDdimmer() {
  acd.begin(NORMAL_MODE, ON);

}

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");
  setupRBDdimmer();
  setupWiFi();
  setupSinricPro();
}

void loop() {
SinricPro.handle();
}
kakopappa commented 4 months ago

Your code compiles fine for me for ESP-12E

image

sivar2311 commented 4 months ago

Code also compiles fine on PlatformIO:

Processing esp12e (platform: espressif8266; board: esp12e; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif8266/esp12e.html
PLATFORM: Espressif 8266 (4.2.1) > Espressif ESP8266 ESP-12E
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
PACKAGES:
 - framework-arduinoespressif8266 @ 3.30102.0 (3.1.2)
 - tool-esptool @ 1.413.0 (4.13)
 - tool-esptoolpy @ 1.30000.201119 (3.0.0)
 - toolchain-xtensa @ 2.100300.220621 (10.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 41 compatible libraries
Scanning dependencies...
Dependency Graph
|-- SinricPro @ 3.0.1
|-- RBDdimmer @ 0.0.0+sha.7008738
|-- ESP8266WiFi @ 1.0
Building in release mode
Retrieving maximum program size .pio\build\esp12e\firmware.elf
Checking size .pio\build\esp12e\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [====      ]  38.9% (used 31852 bytes from 81920 bytes)
Flash: [====      ]  40.1% (used 418545 bytes from 1044464 bytes)

platformio.ini

[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino
lib_deps = 
  SinricPro/SinricPro
  https://github.com/RobotDynOfficial/RBDDimmer

wen i select esp8266 nodemcu "Dose not compiled or esp8266"

So what's the exactly error message you get?