Created
May 5, 2024 04:41
-
-
Save kakopappa/94f127605cabde8968ab3da97466a8a8 to your computer and use it in GitHub Desktop.
How to change the WIFI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 "SinricPro.h" | |
#include "SinricProSwitch.h" | |
#define WIFI_SSID "" | |
#define WIFI_PASS "" | |
#define APP_KEY "" | |
#define APP_SECRET "" | |
#define SWITCH_ID_1 "" | |
#define RELAYPIN_1 15 | |
#define BAUD_RATE 115200 // Change baudrate to your need | |
bool onPowerState1(const String& deviceId, bool& state) { | |
Serial.printf("Device 1 turned %s", state ? "on" : "off"); | |
digitalWrite(RELAYPIN_1, state ? HIGH : LOW); | |
return true; // request handled properly | |
} | |
bool onSetSetting(const String& deviceId, const String& settingId, const String& settingValue) { | |
Serial.printf("[main.onSetSetting()]: Device: %s, id: %s, value: %s ", deviceId.c_str(), settingId.c_str(), settingValue.c_str()); | |
if (settingId.equals("setWiFi")) { | |
StaticJsonDocument<1024> doc; | |
DeserializationError error = deserializeJson(doc, settingValue); | |
if (error) { | |
Serial.print(F("[main.onSetSetting()]: deserializeJson() failed: ")); | |
Serial.println(error.f_str()); | |
return false; | |
} | |
//const char* ssid = doc[F("ssid")]; // "wifi" | |
//const char* password = doc[F("password")]; // "password" | |
// TODO: ADD YOUR CODE TO SAVE | |
ESP.restart(); | |
} | |
return true; | |
} | |
// setup function for WiFi connection | |
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); | |
} | |
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str()); | |
} | |
// setup function for SinricPro | |
void setupSinricPro() { | |
// add devices and callbacks to SinricPro | |
pinMode(RELAYPIN_1, OUTPUT); | |
SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1]; | |
mySwitch1.onSetSetting(onSetSetting); | |
mySwitch1.onPowerState(onPowerState1); | |
// 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); | |
} | |
// main setup function | |
void setup() { | |
Serial.begin(BAUD_RATE); | |
Serial.printf("\r\n\r\n"); | |
setupWiFi(); | |
setupSinricPro(); | |
} | |
void loop() { | |
SinricPro.handle(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment