8. Send email when someone turns on light - Spark Core
Here is a user story.
- It’s 5pm and dark
- Someone comes home
- He/She turns light on
- You receive email notification
pushingbox.com
I searched some articles in the spark community. To use smtp server provided by gmail wouldn’t work, as it required TLS connection.
So let’s use pushingbox.com with which we can send a pre-defined email very easily by HTTP GET.
- Login to pushingbox.com using Google Account.
- Click My Scenarios
- Create Scenario (= Add your service(email), subject & body)
- Get devid for the scenario
Let’s test your scenario using curl.
% curl "http://api.pushingbox.com/pushingbox?devid=YOUR_DEVID"
You would receive an email.
bread board and photo resistor
Use same setting as 5. Photo Resistor.
Code
We use TCPClient class to issue HTTP GET.
// Send email example #include <stdarg.h> #define PRINTF_BUFFER_SIZE 128 void Serial_printf(const char* fmt, ...) { char buff[PRINTF_BUFFER_SIZE]; va_list args; va_start(args, fmt); vsnprintf(buff, PRINTF_BUFFER_SIZE, fmt, args); va_end(args); Serial.println(buff); } int lastVoltage = 4096; TCPClient client; void setup() { Serial.begin(9600); pinMode(A0, INPUT); } void loop() { int currentVoltage = analogRead(A0); // When it becomes brighter, we'd trigger email bool shouldTriggerEmail = (lastVoltage - currentVoltage) > 100; Serial_printf("last=%d current=%d", lastVoltage, currentVoltage); if (shouldTriggerEmail) { client.stop(); Serial.println("Sending email... "); if (client.connect("api.pushingbox.com", 80)) { Serial.println("connected"); client.print("GET /pushingbox?devid=YOUR_DEV_ID"); client.println(" HTTP/1.1"); client.print("Host: "); client.println("api.pushingbox.com"); client.println("User-Agent: Spark"); client.println("Connection: close"); client.println(); client.flush(); } else{ Serial.println("connection failed"); } } lastVoltage = currentVoltage; delay(1000); }