Skip to content

Instantly share code, notes, and snippets.

@TheSkorm
Created May 10, 2016 10:27
Show Gist options
  • Select an option

  • Save TheSkorm/3977f6e6c2d319dfd1061a192a4b848f to your computer and use it in GitHub Desktop.

Select an option

Save TheSkorm/3977f6e6c2d319dfd1061a192a4b848f to your computer and use it in GitHub Desktop.
Plant monitoring slack thing
/*
* HTTP over TLS (HTTPS) example sketch
*
* This example demonstrates how to use
* WiFiClientSecure class to access HTTPS API.
* We fetch and display the status of
* esp8266/Arduino project continuous integration
* build.
*
* Created by Ivan Grokhotkov, 2015.
* This example is in public domain.
*/
// Modified for basic plant monitoring
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "";
const char* password = "";
const char* slackPath = "";
const char* host = "hooks.slack.com";
const int httpsPort = 443;
bool notified = false;
const long interval = 300000; // 5 minutes
unsigned long previousMillis = 0;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
pinMode(D0, INPUT);
}
void loop() {
delay(1000);
unsigned long currentMillis = millis();
if (digitalRead(D0) == 1){
if (notified == false && currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
sendNotice(false);
notified = true;
}
} else {
if (notified == true){
sendNotice(true);
notified = false; // reset notification flag
}
Serial.println("All good");
}
}
void sendNotice(bool healthy){
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = slackPath;
Serial.print("requesting URL: ");
Serial.println(url);
String payload;
if (healthy == true){
payload = "payload={\"text\": \"@mwheeler Thank you for watering me.\",\"username\": \"Bob the pot plant\",\"icon_url\": \"http://vignette4.wikia.nocookie.net/fantendo/images/0/04/Baby_Piranha_Plant.png/revision/latest?cb=20121122165156\"}";
} else {
payload = "payload={\"text\": \"@mwheeler It's time to water me.\",\"username\": \"Bob the pot plant\",\"icon_url\": \"http://vignette4.wikia.nocookie.net/fantendo/images/0/04/Baby_Piranha_Plant.png/revision/latest?cb=20121122165156\"}";
}
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: iPlant\r\n" +
"Content-Length: " + payload.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n"+payload+"\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
Serial.println(line);
Serial.println("==========");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment