Skip to content

Commit 9b66e48

Browse files
authored
Merge pull request #397 from sinricpro/SetFixedIPAddress
feat: set fixed IP address example
2 parents c04129a + 67dee69 commit 9b66e48

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Example for how to use SinricPro Settings:
3+
*
4+
* If you encounter any issues:
5+
* - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
6+
* - ensure all dependent libraries are installed
7+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
8+
* - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
9+
* - open serial monitor and check whats happening
10+
* - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
11+
* - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
12+
*/
13+
14+
// Uncomment the following line to enable serial debug output
15+
// #define ENABLE_DEBUG
16+
17+
#ifdef ENABLE_DEBUG
18+
#define DEBUG_ESP_PORT Serial
19+
#define NODEBUG_WEBSOCKETS
20+
#define NDEBUG
21+
#endif
22+
23+
#include <Arduino.h>
24+
#if defined(ESP8266)
25+
#include <ESP8266WiFi.h>
26+
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
27+
#include <WiFi.h>
28+
#endif
29+
30+
#include "SinricPro.h"
31+
#include "SinricProSwitch.h"
32+
#include "ArduinoJson.h"
33+
34+
#define WIFI_SSID ""
35+
#define WIFI_PASS ""
36+
#define APP_KEY "" // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
37+
#define APP_SECRET "" // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
38+
#define SWITCH_ID "" // Should look like "5dc1564130xxxxxxxxxxxxxx"
39+
40+
#define BAUD_RATE 115200 // Change baudrate to your need
41+
42+
#define SET_FIXED_IP_ADDRESS "pro.sinric::set.fixed.ip.address"
43+
44+
bool onSetModuleSetting(const String &id, const String &value) {
45+
// Handle module settings.
46+
JsonDocument doc;
47+
DeserializationError error = deserializeJson(doc, value);
48+
49+
if (error) {
50+
Serial.print(F("onSetModuleSetting::deserializeJson() failed: "));
51+
Serial.println(error.f_str());
52+
return false;
53+
}
54+
55+
if (id == SET_FIXED_IP_ADDRESS) {
56+
String localIP = doc["localIP"];
57+
String gateway = doc["gateway"];
58+
String subnet = doc["subnet"];
59+
String dns1 = doc["dns1"] | "";
60+
String dns2 = doc["dns2"] | "";
61+
62+
// Change your WiFi config here.
63+
Serial.printf("localIP:%s, gateway:%s, subnet:%s, dns1:%s, dns2:%s \r\n", localIP.c_str(), gateway.c_str(), subnet.c_str(), dns1.c_str(), dns2.c_str());
64+
return true;
65+
} else {
66+
return false;
67+
}
68+
}
69+
70+
// setup function for WiFi connection
71+
void setupWiFi() {
72+
Serial.printf("\r\n[Wifi]: Connecting");
73+
74+
#if defined(ESP8266)
75+
WiFi.setSleepMode(WIFI_NONE_SLEEP);
76+
WiFi.setAutoReconnect(true);
77+
#elif defined(ESP32)
78+
WiFi.setSleep(false);
79+
WiFi.setAutoReconnect(true);
80+
#endif
81+
82+
WiFi.begin(WIFI_SSID, WIFI_PASS);
83+
84+
while (WiFi.status() != WL_CONNECTED) {
85+
Serial.printf(".");
86+
delay(250);
87+
}
88+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
89+
}
90+
91+
// setup function for SinricPro
92+
void setupSinricPro() {
93+
SinricProSwitch &mySwitch = SinricPro[SWITCH_ID];
94+
95+
// setup SinricPro
96+
SinricPro.onConnected([]() {
97+
Serial.printf("Connected to SinricPro\r\n");
98+
});
99+
SinricPro.onDisconnected([]() {
100+
Serial.printf("Disconnected from SinricPro\r\n");
101+
});
102+
103+
SinricPro.onSetSetting(onSetModuleSetting);
104+
SinricPro.begin(APP_KEY, APP_SECRET);
105+
}
106+
107+
// main setup function
108+
void setup() {
109+
Serial.begin(BAUD_RATE);
110+
Serial.printf("\r\n\r\n");
111+
setupWiFi();
112+
setupSinricPro();
113+
}
114+
115+
void loop() {
116+
SinricPro.handle();
117+
}

0 commit comments

Comments
 (0)