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
Other
234 stars 124 forks source link

update SWITCH_ID OTA #203

Closed abdoshamma closed 3 years ago

abdoshamma commented 3 years ago

hello, friend, I have a question if there any way to update the ID or the credentials over the air like a web page, is this possible?

sivar2311 commented 3 years ago

Surely this is possible. But this is not handled by the library and must be implemented accordingly in the Sketch. For example, you could read the ID from a configuration file. You could change the file via a WebInterface.

abdoshamma commented 3 years ago

Thank you for your reply, Actually I tried to implement this code "https://randomnerdtutorials.com/esp32-esp8266-input-data-html-form/" But unfortunately I failed to convert the Value in spiffs to a text to make the sinric code recognize it

define APP_KEY "YOUR_APP_KEY_HERE"

define GARAGEDOOR_ID "YOUR_DEVICE_ID_HERE"

abdoshamma commented 3 years ago

If you can help me out I'll be very thankful 🙏

sivar2311 commented 3 years ago

At the moment I am unfortunately busy with the implementation of the SinricPro SDK version 3.0.0 and other projects and therefore I don't have the necessary time to give you detailed support.

So I can only give you tips if you describe me your project a little bit more detailed.

abdoshamma commented 3 years ago

I'm working in garage door project and my desire is to implement some codes in sinric-pro garage door example to allow me to input the wifi, password, app key, app secret and garage door ID all through a web page so the Same code can be installed in multiple esp8266 and the variables can be defined over the air through web page I've succeeded do it for the Wi-Fi name and password but the challenge now is to make the same web page have another 3 text boxes for the app key, secret and device ID.

sivar2311 commented 3 years ago

I think the easiest way would be to use WiFiManager. You can define your own additional parameters like app-key, app-secret, deviceId, etc. You don't have to bother with programming the web interface. The only thing you need to do is to store and load your additional parameters in SPIFFS / LittleFS or EEPROM.

abdoshamma commented 3 years ago

Unfortunately I know very little about programming that's why I asked for help thank you for your time and please if you remember me after you finish ur work just help me and I'll try to follow what you told earlier, thank you

abdoshamma commented 3 years ago

done by wifi manager thank you. But the issue is that in the first time setup I added the password, app key, secret etc, And its working perfectly but once I restart the esp it's not connecting to sinric-pro so please take a look at the code in the next comment.

abdoshamma commented 3 years ago

`#include

include

include

include

include

include

include

include

include "SinricPro.h"

include "SinricProGarageDoor.h"

//#define BAUD_RATE 9600 // Change baudrate to your need

define Switch_off 3 // PIN where contactsensor is connected to

define Switch_on 2

String ssid, password, APP_SECRET, APP_KEY, GARAGEDOOR_ID; void initSystem(); void initWifiManager(); void configModeCallback(WiFiManager *myWiFiManager); void saveConfigCallback();

bool shouldSaveConfig = false;

//define your default values here, if there are different values in config.json, they are overwritten. //char mqtt_server[40]; char APPKEY[64] = "App Key"; char APPSECRET[96] = "App Secret";

char GARAGEDOORID[64] = "GARAGEDOOR ID";

bool myPowerState = true; // assume device is turned on bool onDoorState(const String& deviceId, bool &doorState) { Serial.printf("Garagedoor is %s now.\r\n", doorState?"closed":"open");

switch(doorState) { case true: digitalWrite(Switch_on, LOW); delay(1000); digitalWrite(Switch_on, HIGH); // digitalWrite(LED_PIN, LOW) break; case false: digitalWrite(Switch_off, LOW); delay(1000); digitalWrite(Switch_off, HIGH); //digitalWrite(LED_PIN, HIGH) break;
} return true; }

bool onPowerState(const String &deviceId, bool &state) { Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off"); myPowerState = state; return true; // request handled properly }

// setup function for SinricPro

void setup() { Serial.println("setupSinricPro"); setupSinricPro(); delay(50); Serial.begin(115200); EEPROM.begin(1024); WiFi.mode(WIFI_STA); webconnect(); pinMode(LED_BUILTIN, OUTPUT); pinMode(Switch_off, OUTPUT);digitalWrite(Switch_off, HIGH); pinMode(Switch_on, OUTPUT );digitalWrite(Switch_on, HIGH);

Serial.println("connected...so easy :)"); digitalWrite(LED_BUILTIN, HIGH); Serial.println("The following information read for EEPROM:"); String count=Read_String(EEPROM.read(800),816); delay(50); Serial.println(count); String pass=Read_String(EEPROM.read(804),856); delay(50); Serial.println(pass); String key = Read_String(EEPROM.read(808), 884); delay(50); Serial.println(key); String secret = Read_String(EEPROM.read(812), 932); delay(50); Serial.println(secret); String id = Read_String(EEPROM.read(816), 976); delay(50); Serial.println(id);

Serial.println("setupSinricPro"); setupSinricPro();

//SinricPro.handle();

}

void loop() {

// put your main code here, to run repeatedly: SinricPro.handle(); }

void setupSinricPro() { // add device to SinricPro

SinricProGarageDoor& myGarageDoor = SinricPro[GARAGEDOOR_ID]; myGarageDoor.onDoorState(onDoorState); // 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); } / Function Description: Initializing WiFiManager / void webconnect() { // Step 1: Create a WiFiManager object ** / WiFiManager wifiManager;// Institute of WiFiManager

// * Step 2: Take a series of configurations, refer to configuration class method /

// Reset Saved Modification The Target is to remove the configuration page for each time. //wifiManager.resetSettings();

// Configure connection timeout wifiManager.setConnectTimeout(1000); // Print debug content wifiManager.setDebugOutput(false); // Set the minimum signal strength // wifiManager.setMinimumSignalQuality(30); // Set the fixed AP information

//wifiManager.setAPStaticIPConfig(IPAddress(192,168,0,150), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

//IPAddress _ip = IPAddress(192, 168, 0, 150); // IPAddress _gw = IPAddress(192, 168, 0, 1); //IPAddress _sn = IPAddress(255, 255, 255, 0); //wifiManager.setAPStaticIPConfig(_ip, _gw, _sn);*/ // Set the callback to the AP mode wifiManager.setAPCallback(configModeCallback); // Set click saved callback wifiManager.setSaveConfigCallback(saveConfigCallback); // Setup If you configure an error SSID or password exit configuration mode // wifiManager.setBreakAfterConfig(true); // Set filter repetition AP defaults to call wifiManager.setRemoveDuplicateAPs(true); // Adding additional parameters, such as adding an APPKEY, you can use the annet to use // WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); WiFiManagerParameter custom_APPKEY("APPKEY", "APPKEY", APPKEY, 64);

WiFiManagerParameter custom_APPSECRET("APPSECRET", "APPSECRET", APPSECRET, 96);

WiFiManagerParameter custom_GARAGEDOORID("GARAGEDOORID", "GARAGEDOORID", GARAGEDOORID, 64); // wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_APPKEY);

wifiManager.addParameter(&custom_APPSECRET);

wifiManager.addParameter(&custom_GARAGEDOORID);

/***/ // * Step 3: Try to connect the network, fail to the configuration page ** / // ssid named ESP8266WIFIAP PWD is 12345678 // wifiManager.autoConnect("AutoConnectAP", "password") if (!wifiManager.autoConnect("Smart Garage ", "123456789")) { Serial.println("Connection failed and timeout"); // ESP.reset(); ESP.restart(); delay(1000); } /*****/ // Read configuration page configuration information // strcpy(mqtt_server, custom_mqtt_server.getValue()); // Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str()); // Serial.printf("PSW:%s\r\n", WiFi.psk().c_str()); ssid = WiFi.SSID(); password = WiFi.psk(); Serial.println(ssid); Serial.println(password); // WiFi.printDiag(Serial);

strcpy(APPSECRET, custom_APPSECRET.getValue()); APP_SECRET = String(APPSECRET); Serial.println(APP_SECRET); strcpy(APPKEY, custom_APPKEY.getValue()); APP_KEY = String(APPKEY); Serial.println(APP_KEY); strcpy(GARAGEDOORID, custom_GARAGEDOORID.getValue()); GARAGEDOOR_ID = String(GARAGEDOORID); Serial.println(GARAGEDOOR_ID); // Save Custom Information

if (shouldSaveConfig) { Serial.println("saving config"); StaticJsonDocument<128> doc; JsonObject root = doc.to(); JsonObject json = root.createNestedObject("json"); json["ssid"] = ssid; json["password"] = password; json["APPSECRET"] = APPSECRET; json["APPKEY"] = APPKEY; json["GARAGEDOORID"] = GARAGEDOORID;

serializeJson(root, Serial);

File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { Serial.println("failed to open config file for writing"); } configFile.close(); shouldSaveConfig = false; Write_String(800, 816, ssid); delay(30); Write_String(804, 856, password); delay(30); Write_String(808, 884, APP_SECRET); delay(30); Write_String(812, 932, APP_KEY); delay(30); Write_String(816, 976, GARAGEDOOR_ID); delay(30); } Serial.print("local ip:"); Serial.println(WiFi.localIP()); yield(); // setupSinricPro(); // SinricPro.handle(); }

/* Function Description: Configure the access to the AP mode notification callback / void configModeCallback (WiFiManager *myWiFiManager) { Serial.println("Entered config mode"); Serial.println(WiFi.softAPIP()); //if you used auto generated SSID, print it Serial.println(myWiFiManager->getConfigPortalSSID()); }

/* Function Description: Set click saved callback / void saveConfigCallback () { Serial.println("Should save config"); shouldSaveConfig = true; }

// A is written to the string length, B is the start bit, the string Str is the string to be saved void Write_String(int a, int b, String str) { EEPROM.write(a, str.length());// EEPROM No. A, write to the length of the STR string // Save all the STR data in EEPROM for (int i = 0; i < str.length(); i++) { EEPROM.write(b + i, str[i]); } EEPROM.commit(); } // A bit is a string length, b is the starting position String Read_String(int a, int b) { String data = ""; // Take out each bit of values ​​from the EEPROM and link for (int i = 0; i < a; i++) { data += char(EEPROM.read(b + i)); } return data; }`

abdoshamma commented 3 years ago

333 before and after the reset.

sivar2311 commented 3 years ago

Unfortunately, there are too many errors here.

I recommend to start in small steps. First make a project to use WiFiManger. Then make another project to be able to read and write configuration files. In the context of JSON files, I recommend the example at https://arduinojson.org/v6/example/config/. Then put everything together.

abdoshamma commented 3 years ago

Thank you so much for your quick response, setupSinricPro seems the function which is not working that's why I tried it in different places hoping it will work 😅 I don't have enough experience in programming that's why I guess there's a lot of mistakes, however I did before what u told, first I made the code working with wifi Manger lib then I started to implement the balance parameters I got the code from here " https://www.programmersought.com/article/94898280046/" Then I started to customize. I think now I'm stuck and I'll try from the beginning. Thank you for your time I'll update here if i found a way.

stale[bot] commented 3 years ago

This issue has gone quiet. Spooky quiet. We currently close issues after 14 days of inactivity. It’s been at least 7 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks for being a part of the SinricPro community!

stale[bot] commented 3 years ago

Hey again! It’s been 14 days since anything happened on this issue, so our friendly robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to comment on this issue or create a new one if you need anything else. As a friendly reminder, the best way to fix this or any other problem is to provide a detailed error description including a serial log. Thanks again for being a part of the SinricPro community!