Skip to content

Commit 930339b

Browse files
committed
made library ArduinoIDE compatible
1 parent b5c7297 commit 930339b

File tree

9 files changed

+207
-10
lines changed

9 files changed

+207
-10
lines changed

README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11

22
# SinricPro (ESP8266 / ESP32 SDK)
33
## Installation
4-
This library is designed for use with [platform.io](https://platformio.org/platformio-ide). We recommend using [VS Code](https://code.visualstudio.com/) as code editor.
54

5+
### VS Code & PlatformIO:
66
1. Install [VS Code](https://code.visualstudio.com/)
7-
2. Install [platform.io](https://platformio.org/platformio-ide)
8-
3. Open the [example](https://github.com/sinricpro/esp8266-esp32-sdk/tree/master/examples/Switch) project using VS Code.
9-
4. Install **SinricPro** library by using [Library Manager](https://docs.platformio.org/en/latest/librarymanager/)
7+
2. Install [PlatformIO](https://platformio.org/platformio-ide)
8+
3. Install **SinricPro** library by using [Library Manager](https://docs.platformio.org/en/latest/librarymanager/)
109

1110
![sinricpro library manager](https://github.com/sinricpro/images/blob/master/platformio-install-sinricpro.png)
1211

12+
### ArduinoIDE
13+
1. Download **SinricPro** [esp8266-esp32-sdk-master.zip](https://github.com/sinricpro/esp8266-esp32-sdk/archive/master.zip) from git
14+
2. Extract ZIP File to Arduino Library Folder (C:\Users\USERNAME\Documents\Arduino\libraries\SinricPro)
15+
3. Open Example in ArduinoIDE (File / Examples / SinricPro / ... )
1316

1417
---
1518

1619
## Dependencies
17-
[ArduinoJson](https://github.com/bblanchon/ArduinoJson)<br>
18-
[WebSocketsClient](https://github.com/Links2004/arduinoWebSockets)<br>
20+
[ArduinoJson](https://github.com/bblanchon/ArduinoJson) (Version 6.12.0)
21+
[WebSocketsClient](https://github.com/Links2004/arduinoWebSockets) (Version 2.2.0)
1922
[NTPClient](https://github.com/arduino-libraries/NTPClient)
2023

2124
---
File renamed without changes.
File renamed without changes.

library.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
},
99
"authors": [
1010
{
11-
"name": "sivar2311",
11+
"name": "Boris Jaeger",
1212
"email": "[email protected]",
1313
"maintainer": true
1414
}
1515
],
16-
"version": "2.0.1",
16+
"version": "2.0.2",
1717
"frameworks": "arduino",
1818
"platforms": [
1919
"espressif8266",
2020
"espressif32"
2121
],
2222
"examples":[
23-
"examples/Switch/src/switch.cpp",
24-
"examples/Doorbell/src/doorbell.cpp"
23+
"pio-examples/switch/src/switch.cpp",
24+
"pio-examples/doorbell/src/doorbell.cpp"
2525
]
2626
}

library.properties

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name=SinricPro
2+
version=2.0.1
3+
author=Boris Jäger <[email protected]>
4+
maintainer=Boris Jäger <[email protected]>
5+
sentence=An Arduino SDK for https://sinric.pro
6+
paragraph=Simple way to control your IOT development boards like ESP8226 or ESP32 with Amazon Alexa or Google Home
7+
category=Communication
8+
url=https:://sinric.pro
9+
architectures=esp8266,esp32
10+
repository=https://github.com/sinricpro/esp8266-esp32-sdk.git
11+
license=CC-BY-SA
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Example for how to use SinricPro Doorbell device:
3+
* - setup a doorbell device
4+
* - send event to sinricPro server if button is pressed
5+
*/
6+
7+
#include <Arduino.h>
8+
#ifdef ESP8266
9+
#include <ESP8266WiFi.h>
10+
#endif
11+
#ifdef ESP32
12+
#include <WiFi.h>
13+
#endif
14+
15+
#include "SinricPro.h"
16+
#include "SinricProDoorbell.h"
17+
18+
#define WIFI_SSID "YOUR-WIFI-SSID"
19+
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
20+
#define SOCKET_AUTH_TOKEN "YOUR-SOCKET-AUTH-TOKEN"
21+
#define SIGNING_KEY "YOUR-SIGNING-KEY"
22+
23+
#define DOORBELL_ID "YOUR-DEVICE-ID"
24+
25+
26+
// change this to your button PIN
27+
// on NodeMCU D3 / GPIO-0 is flash button PIN so you can use the builtin flash button
28+
#define BUTTON_PIN 0
29+
30+
// define SinricPro Doorbell Device
31+
SinricProDoorbell myDoorbell(DOORBELL_ID);
32+
33+
// checkButtonpress
34+
// reads if BUTTON_PIN gets LOW and send Event
35+
void checkButtonPress() {
36+
static unsigned long lastBtnPress;
37+
unsigned long actualMillis = millis();
38+
39+
if (actualMillis-lastBtnPress > 500) {
40+
if (digitalRead(BUTTON_PIN)==LOW) {
41+
lastBtnPress = actualMillis;
42+
myDoorbell.sendDoorbellEvent();
43+
}
44+
}
45+
}
46+
47+
// setup function for WiFi connection
48+
void setupWiFi() {
49+
Serial.printf("\r\n[Wifi]: Connecting");
50+
WiFi.begin(WIFI_SSID, WIFI_PASS);
51+
52+
while (WiFi.status() != WL_CONNECTED) {
53+
Serial.printf(".");
54+
delay(250);
55+
}
56+
IPAddress localIP = WiFi.localIP();
57+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
58+
}
59+
60+
// setup function for SinricPro
61+
void setupSinricPro() {
62+
// add device to SinricPro
63+
SinricPro.add(myDoorbell);
64+
// setup SinricPro
65+
SinricPro.begin(SOCKET_AUTH_TOKEN, SIGNING_KEY);
66+
}
67+
68+
// main setup function
69+
void setup() {
70+
pinMode(BUTTON_PIN, INPUT_PULLUP); // BUTTIN_PIN as INPUT
71+
72+
Serial.begin(115200);
73+
setupWiFi();
74+
setupSinricPro();
75+
}
76+
77+
void loop() {
78+
checkButtonPress();
79+
SinricPro.handle();
80+
}
File renamed without changes.

pio-examples/switch/src/switch.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Example for how to use SinricPro Switch device:
3+
* - setup a switch device
4+
* - handle request using callback (turn on/off builtin led indicating device power state)
5+
* - send event to sinricPro server (flash button is used to turn on/off device manually)
6+
*/
7+
8+
#include <Arduino.h>
9+
#ifdef ESP8266
10+
#include <ESP8266WiFi.h>
11+
#endif
12+
#ifdef ESP32
13+
#include <WiFi.h>
14+
#endif
15+
16+
#include "SinricPro.h"
17+
#include "SinricProSwitch.h"
18+
19+
#define WIFI_SSID "YOUR-WIFI-SSID"
20+
#define WIFI_PASS "YOUR-WIFI-PASSWORD"
21+
#define SOCKET_AUTH_TOKEN "YOUR-SOCKET-AUTH-TOKEN"
22+
#define SIGNING_KEY "YOUR-SIGNING-KEY"
23+
#define SWITCH_ID "YOUR-DEVICE-ID"
24+
25+
#define BTN_FLASH 0
26+
27+
SinricProSwitch mySwitch(SWITCH_ID);
28+
bool myPowerState = false;
29+
unsigned long lastBtnPress = 0;
30+
31+
/* bool onPowerState(String deviceId, bool &state)
32+
*
33+
* Callback for setPowerState request
34+
* parameters
35+
* String deviceId (r)
36+
* contains deviceId (useful if this callback used by multiple devices)
37+
* bool &state (r/w)
38+
* contains the requested state (true:on / false:off)
39+
* must return the new state
40+
*
41+
* return
42+
* true if request should be marked as handled correctly / false if not
43+
*/
44+
bool onPowerState(String deviceId, bool &state) {
45+
Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
46+
myPowerState = state;
47+
digitalWrite(LED_BUILTIN, myPowerState?LOW:HIGH);
48+
return true; // request handled properly
49+
}
50+
51+
void handleButtonPress() {
52+
unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
53+
if (digitalRead(BTN_FLASH) == LOW && actualMillis - lastBtnPress > 1000) { // is button pressed (inverted logic! button pressed = LOW) and debounced?
54+
if (myPowerState) { // flip myPowerState: if it was true, set it to false, vice versa
55+
myPowerState = false;
56+
} else {
57+
myPowerState = true;
58+
}
59+
digitalWrite(LED_BUILTIN, myPowerState?LOW:HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)
60+
Serial.printf("Device %s turned %s (manually via flashbutton)\r\n", mySwitch.getDeviceId(), myPowerState?"on":"off");
61+
mySwitch.sendPowerStateEvent(myPowerState); // send the new powerState to SinricPro server
62+
lastBtnPress = actualMillis; // update last button press variable
63+
}
64+
}
65+
66+
// setup function for WiFi connection
67+
void setupWiFi() {
68+
Serial.printf("\r\n[Wifi]: Connecting");
69+
WiFi.begin(WIFI_SSID, WIFI_PASS);
70+
71+
while (WiFi.status() != WL_CONNECTED) {
72+
Serial.printf(".");
73+
delay(250);
74+
}
75+
IPAddress localIP = WiFi.localIP();
76+
Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
77+
}
78+
79+
// setup function for SinricPro
80+
void setupSinricPro() {
81+
// set callback function to device
82+
mySwitch.onPowerState(onPowerState);
83+
// add device to SinricPro
84+
SinricPro.add(mySwitch);
85+
// setup SinricPro
86+
SinricPro.begin(SOCKET_AUTH_TOKEN, SIGNING_KEY);
87+
}
88+
89+
// main setup function
90+
void setup() {
91+
pinMode(BTN_FLASH, INPUT_PULLUP); // GPIO 0 as input, pulled high
92+
pinMode(LED_BUILTIN, OUTPUT); // define LED GPIO as output
93+
digitalWrite(LED_BUILTIN, HIGH); // turn off LED on bootup
94+
95+
Serial.begin(115200);
96+
setupWiFi();
97+
setupSinricPro();
98+
}
99+
100+
void loop() {
101+
handleButtonPress();
102+
SinricPro.handle();
103+
}

0 commit comments

Comments
 (0)