Hi!
I'm very new to the Arduino world and have very limited background in electronics, so apologies if i'm not using correct terminology or if I'm not clear.
So far i've ran through some of the tutorials without to much issues.
For my first 'real' project I wanted to solve an issue that's annoying me to no end.
My doorbel is a generic wireless unit, and more often than not I don't hear it ring at all (receiver has to be pretty close to the actual button, and my 'office' where I spend most of my time is pretty far).
I thought it'd be pretty easy to hook up an arduino and send some other notification when the doorbell rings.
For this I have hooked up a MKR1000 in parallel (I think) to the speaker, which should trigger a HIGH on a digital IN pin if i'm not mistaken. This actually works pretty well, and pressing the doorbell activates the speaker, and registers on the arduino (which fires of a pushbullet notification and flashes my Hue lights).
The issue i'm having is that the logic is triggered every couple hours even though the doorbell isn't activated. I've measured the current over the arduino ground - digital pin which reads ~0.59V, even when the speaker isn't activated, and ~2.5V when it is.
I'm assuming this is due the difference in ground, the speaker unit is battery powered, and the arduino is hooked up to the mains via a USB adapter?
Could anyone tell me if there's better solutions for what i'm trying to achieve? Basically all i'm trying to accomplish is to detect if the speaker circuit is activated.
And sidequestion, since i'm a noob at the electrical side, am I within the tolerances of the arduino unit? I think I understand the pins can't bear over 3.3V, but is there a limit to the amps as well when the pin is used as an input? - How much of a fire risk have I created?
I don't think it's very relevant, but here's my code:
#include <SPI.h>
#include <WiFi101.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
char serverName[] = "api.pushingbox.com";
char DEVID1[] = "***************";
const char hueServer[] = "****************"; // Hue hub IP
const char hueUsername[] = "*****************************"; // Hue username
const int hueHubPort = 80;
boolean hueOn1;
boolean hueOn2;
int lightNr1 = 3;
int lightNr2 = 6;
int val = 0;
volatile unsigned long elaps = 0;
String commandR = "{\"on\": true,\"hue\": 0,\"sat\":255,\"bri\":255}";
String commandW = "{\"on\": true,\"hue\": 0,\"sat\":0,\"bri\":255}";
String commandRes1;
String commandRes2;
WiFiClient client;
void setup() {
// Serial.begin(9600);
pinMode(6, INPUT);
if (WiFi.status() == WL_NO_SHIELD) {
while (true);
}
while (status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(10000);
}
}
void loop() {
val = digitalRead(6);
if (elaps == 0 && val == HIGH) {
elaps = micros();
}
if (val == HIGH && elaps >= 1000) {
digitalWrite(LED_BUILTIN, HIGH);
pushbullet();
getHue(lightNr1);
getHue(lightNr2);
setHue(lightNr1, commandR);
setHue(lightNr2, commandR);
delay(500);
setHue(lightNr1, commandW);
setHue(lightNr2, commandW);
delay(500);
setHue(lightNr1, commandR);
setHue(lightNr2, commandR);
delay(500);
setHue(lightNr1, commandW);
setHue(lightNr2, commandW);
delay(500);
setHue(lightNr1, commandR);
setHue(lightNr2, commandR);
delay(500);
setHue(lightNr1, commandW);
setHue(lightNr2, commandW);
delay(500);
setHue(lightNr1, commandR);
setHue(lightNr2, commandR);
delay(500);
setHue(lightNr1, commandW);
setHue(lightNr2, commandW);
delay(500);
setHue(lightNr1, commandR);
setHue(lightNr2, commandR);
delay(500);
setHue(lightNr1, commandW);
setHue(lightNr2, commandW);
delay(500);
if (hueOn1) {
commandRes1 = "{\"on\": true}";
} else {
commandRes1 = "{\"on\": false}";
}
if (hueOn2) {
commandRes2 = "{\"on\": true}";
} else {
commandRes2 = "{\"on\": false}";
}
// Serial.println(commandRes1);
// Serial.println(commandRes2);
setHue(lightNr1, commandRes1);
delay(500);
setHue(lightNr2, commandRes2);
}
if (val == LOW) {
elaps = 0;
}
}
void pushbullet() {
if (client.connect(serverName, 80)) {
client.print("GET /pushingbox?devid=");
client.print(DEVID1);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
client.stop();
}
}
void hue() {
if (client.connect(serverName, 80)) {
client.print("GET /pushingbox?devid=");
client.print(DEVID1);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
client.stop();
}
}
boolean setHue(int lightNum, String command)
{
if (client.connect(hueServer, hueHubPort))
{
client.print("PUT /api/");
client.print(hueUsername);
client.print("/lights/");
client.print(lightNum); // hueLight zero based, add 1
client.println("/state HTTP/1.1");
client.println("keep-alive");
client.print("Host: ");
client.println(hueServer);
client.print("Content-Length: ");
client.println(command.length());
client.println("Content-Type: text/plain;charset=UTF-8");
client.println(); // blank line before body
client.println(command); // Hue command
client.stop();
}
}
boolean getHue(int lightNum)
{
if (client.connect(hueServer, hueHubPort))
{
client.print("GET /api/");
client.print(hueUsername);
client.print("/lights/");
client.print(lightNum);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(hueServer);
client.println("Content-type: application/json");
client.println("keep-alive");
client.println();
while (client.connected())
{
if (client.available())
{
client.findUntil(const_cast<char*>("\"on\":"), const_cast<char*>("bri"));
String onState = client.readStringUntil(',');
// Serial.println(lightNum);
// Serial.println(onState);
if (lightNum == lightNr1){
hueOn1 = (onState == "true"); // if light is on, set variable to true
}else{
hueOn2 = (onState == "true"); // if light is on, set variable to true
}
break; // not capturing other light attributes yet
}
}
client.stop();
}
}