This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
SAMD_WiFiNINA_Switch.ino
148 lines (117 loc) · 4.15 KB
/
SAMD_WiFiNINA_Switch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/****************************************************************************************************************************
SAMD_WiFiNINA_Switch.ino
For SAMD boards, running WiFiNINA
Based on and modified from SinricPro libarary (https://github.com/sinricpro/)
to support other boards such as SAMD21, SAMD51, Adafruit's nRF52 boards, etc.
Built by Khoi Hoang https://github.com/khoih-prog/SinricPro_Generic
Licensed under MIT license
Copyright (c) 2019 Sinric. All rights reserved.
Licensed under Creative Commons Attribution-Share Alike (CC BY-SA)
This file is part of the Sinric Pro (https://github.com/sinricpro/)
**********************************************************************************************************************************/
// STM32 Boards supported: Nucleo-144, Nucleo-64, Nucleo-32, Discovery, STM32F1, STM32F3, STM32F4, STM32H7, STM32L0, etc.
// SAM DUE
// Teensy 4.1, 4.0, 3.6, 3.5, 3.2/3.1, 3.0
#include "defines.h"
#include "SinricPro_Generic.h"
#include "SinricProSwitch.h"
bool myPowerState = false;
unsigned long lastBtnPress = 0;
/* bool onPowerState(String deviceId, bool &state)
Callback for setPowerState request
parameters
String deviceId (r)
contains deviceId (useful if this callback used by multiple devices)
bool &state (r/w)
contains the requested state (true:on / false:off)
must return the new state
return
true if request should be marked as handled correctly / false if not
*/
bool onPowerState(const String &deviceId, bool &state)
{
Serial.print("Device ");
Serial.print(deviceId.c_str());
Serial.print(state ? " turned on" : " turn off");
Serial.println(" (via SinricPro)");
myPowerState = state;
digitalWrite(LED_PIN, myPowerState ? LOW : HIGH);
return true; // request handled properly
}
void handleButtonPress()
{
unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
if (digitalRead(BUTTON_PIN) == LOW && actualMillis - lastBtnPress > 1000)
{
// is button pressed (inverted logic! button pressed = LOW) and debounced?
if (myPowerState)
{
// flip myPowerState: if it was true, set it to false, vice versa
myPowerState = false;
}
else
{
myPowerState = true;
}
digitalWrite(LED_PIN, myPowerState ? LOW :
HIGH); // if myPowerState indicates device turned on: turn on led (builtin led uses inverted logic: LOW = LED ON / HIGH = LED OFF)
// get Switch device back
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
// send powerstate event
mySwitch.sendPowerStateEvent(myPowerState); // send the new powerState to SinricPro server
Serial.print("Device ");
Serial.print(mySwitch.getDeviceId().toString());
Serial.print(myPowerState ? "turned on" : "turn off");
Serial.println(" (manually via flashbutton)");
lastBtnPress = actualMillis; // update last button press variable
}
}
// setup function for WiFi connection
void setupWiFi()
{
Serial.println("\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(250);
}
Serial.print("\n[WiFi]: IP-Address is ");
Serial.println(WiFi.localIP());
}
// setup function for SinricPro
void setupSinricPro()
{
// add device to SinricPro
SinricProSwitch& mySwitch = SinricPro[SWITCH_ID];
// set callback function to device
mySwitch.onPowerState(onPowerState);
// setup SinricPro
SinricPro.onConnected([]()
{
Serial.println("Connected to SinricPro");
});
SinricPro.onDisconnected([]()
{
Serial.println("Disconnected from SinricPro");
});
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup()
{
pinMode(BUTTON_PIN, INPUT_PULLUP); // GPIO 0 as input, pulled high
pinMode(LED_PIN, OUTPUT); // define LED GPIO as output
digitalWrite(LED_PIN, HIGH); // turn off LED on bootup
Serial.begin(BAUD_RATE);
while (!Serial);
Serial.println("\nStarting SAMD_WiFiNINA_Switch on " + String(BOARD_NAME));
Serial.println("Version : " + String(SINRICPRO_VERSION_STR));
setupWiFi();
setupSinricPro();
}
void loop()
{
handleButtonPress();
SinricPro.handle();
}