I’m hacking a kettle. There is a wire at 5v that indicates if the kettle is on. How can I detect this?
This is the kettle. I have spliced in wires to interface with the controls in two ways.
The kettle has 4 momentary buttons. The on/off button lights up when the kettle is on.
As you can see I’m wired right into the buttons. The relay shield lets me close, then open these circuits to simulate button presses. This works OK. But, I need to know if the kettle is on, and how warm the water is. I need the relay because the board is 5v. It seems like overkill. But it works…
So I connected to the 5v level input/output I’ve done some tests and the red wire is at 5v when the kettle is on and 0v when it is off… How do I get this information into the core?
I have lots of free pins, but I just don’t know how to set it up. The orange wire seems to be the temp… It’s from 0-5v depending on the kettle. How can I get this info?
I tried making a divider, but the output in analog is just random.
Help?
#include "HttpClient/HttpClient.h"
HttpClient http;
http_request_t request;
http_response_t response;
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers with NULL
};
String search_on = String("\"value\": 1");//To check if a switch is pressed or not
// These are variables for Ubidots.com
#define on_SWITCH "UBIDOTS_VARIBLE_ID1"
#define down_SWITCH "UBIDOTS_VARIBLE_ID2"
#define up_SWITCH "UBIDOTS_VARIBLE_ID3"
#define prog_SWITCH "UBIDOTS_VARIBLE_ID4"
#define kettleTemp "UBIDOTS_VARIBLE_ID5"
#define kettleIsOn "UBIDOTS_VARIBLE_ID6"
#define TOKEN "UBIDOTS_TOKEN"
float temp = 0;
float tempScaled = 0;
char tempresultstr[64];
// Variables to store virtual button presses
int kettle_on;
int kettle_down;
int kettle_up;
int kettle_prog;
// Variable to detect if the kettle is currently on
int kettle_is_on;
char kettle_is_onresultstr[64];
// pin assignments for the relays
int RELAY1 = D0;
int RELAY2 = D1;
int RELAY3 = D2;
int RELAY4 = D3;
void setup()
{
//Initilize the relay control pins as output
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
// get ready to read the temp.
pinMode(A7, INPUT);
// get ready to read if the kettle is on or not
pinMode(A6, INPUT);
// Initialize all relays to an OFF state
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);
//register the Spark function
Spark.function("relay", relayControl);
request.hostname = "things.ubidots.com";
request.port = 80;
// Serial.begin(9600); For debug
Serial.begin(9600);
Spark.variable("kettle_is_on", &kettle_is_on, INT);
Spark.variable("kettle_on", &kettle_on, INT);
}
void loop()
{
// This loops for ever
response.body = "";
response.status = 0;
request.path = "/api/v1.6/variables/"on_SWITCH"/values?page_size=1";
http.get(request, response, headers);
// check if the button is pressed on ubidots.com
kettle_on = response.body.indexOf(search_on);
//check if the 5v signal is there
kettle_is_on = digitalRead(A6);
if (-1 < kettle_on) {
//respond to virtural pressed button on ubidots
digitalWrite(RELAY1, HIGH);
delay(300);
digitalWrite(RELAY1, LOW);
}
if ( kettle_is_on ==1 ) {
// Send to Ubidots
request.path = "/api/v1.6/variables/"kettleIsOn"/values";
sprintf(kettle_is_onresultstr, "{\"value\":%.4f}", 1);
request.body = kettle_is_onresultstr;
http.post(request, response, headers);
Serial.println(response.status);
Serial.println(response.body);
temp = analogRead(A7);
tempScaled = temp/20;
// Send to Ubidots
request.path = "/api/v1.6/variables/"kettleTemp"/values";
sprintf(tempresultstr, "{\"value\":%.4f}", tempScaled);
request.body = tempresultstr;
http.post(request, response, headers);
Serial.println(response.status);
Serial.println(response.body);
}
}
// command format r1,HIGH
int relayControl(String command)
{
int relayState = 0;
// parse the relay number
int relayNumber = command.charAt(1) - '0';
// do a sanity check
if (relayNumber < 1 || relayNumber > 4) return -1;
// find out the state of the relay
if (command.substring(3,7) == "HIGH") relayState = 1;
else if (command.substring(3,6) == "LOW") relayState = 0;
else return -1;
Serial.println("relay triggered");
// write to the appropriate relay
digitalWrite(relayNumber-1, relayState);
return 1;
}
My main questions:
- How can I detect a 5v signal as an (ideally) digital pininput?
- Do I really need these big relays to simulate button presses? Could I use a transistor or mosfet?
- Is there a better way to splice in? Look how ugly it is now.