Skip to content

Instantly share code, notes, and snippets.

@awade
Created July 26, 2017 18:32
Show Gist options
  • Save awade/2fd7554542e250e4ccd7529d1acd8ad6 to your computer and use it in GitHub Desktop.
Save awade/2fd7554542e250e4ccd7529d1acd8ad6 to your computer and use it in GitHub Desktop.
/***************************************************
Temperature logger
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adapted from code Written by Todd Treece.
Andrew Wade
20161220
Change log:
- Tokens have all been scrubbed and replaced with xxx
****************************************************/
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include "Adafruit_IO_Client.h"
// function prototypes
int temp_level();
void wifi_init();
void TriggerEventReport(boolean ReportState);
void blinkLED();
void trigIFTTT(String triggerName, String token);
String phraseMashup();
// Variables
int TrigCount = 0;
int TrigLevel = 800;
String returnPhrase;
//// Set interval in seconds between log events
#define SLEEP_LENGTH 5
#define WLAN_SSID "xxx"
#define WLAN_PASS "" // This is not needed for open networks
// configure Adafruit IO access
#define AIO_KEY "xxx"
// create an Adafruit IO client instance
WiFiClient client;
Adafruit_IO_Client aio = Adafruit_IO_Client(client, AIO_KEY);
// configure IFTTT IO Maker access
const char host[] = "maker.ifttt.com";
const char ITFFF_token[] = "xxx";
void setup() {
pinMode(0, OUTPUT); // Set up red LED for blinking
Serial.begin(115200); // Start serial link for debug monitoring
while(!Serial){};
Serial.println("Serial port opened");
wifi_init();
}
// noop
void loop() {
temp_level();
int level = analogRead(A0);
Serial.println("Data logged, waiting for next point...");
delay(SLEEP_LENGTH * 1000);
if (level >= TrigLevel) {TrigCount ++;}
else {TrigCount = 0;}
//if (TrigCount == 5) {trigIFTTT(coffeeBrewTrigger,ITFFF_token);}
if (TrigCount == 10) {trigIFTTT("coffee_trigger",ITFFF_token);}
}
/////// Sub routines //////////
// connect to wifi network and establish
void wifi_init() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Starting WiFi");
if (WLAN_PASS == "") {WiFi.begin(WLAN_SSID);} // case no password connect with SSID directly
else {WiFi.begin(WLAN_SSID, WLAN_PASS);}// case password is non-null
}
while (WiFi.status() != WL_CONNECTED) {Serial.write('.'); delay(3000);} // wait for connection
}
void trigIFTTT(String triggerName, String token) {
// Use WiFiClient class to open TCP connections
wifi_init();
WiFiClient client;
if (!client.connect(host, 80)) {
Serial.println("connection failed");
return;
}
// We now create a URL for the request
String url = "https://" + String(host) + "/trigger/" + String(triggerName) + "/with/key/" + token;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(500);
// Read all the lines of the reply from server and print them to Serial (dumping them)
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
// client.stop();
}
int temp_level() {
// read the temp level from the ESP8266 analog in pin.
// analog read level is 10 bit 0-1023 (0V-1V).
int level = analogRead(A0);
// convert temp level count to eqv. temp float
level = map(level, 0, 1000,0, 1000);
Serial.print("Temp level: "); Serial.print(level); Serial.println("C");
// turn on wifi if we aren't connected
wifi_init();
Serial.println("Connecting to Adafruit.io"); // AIO init
aio.begin();
Serial.print(aio.begin());
//while(aio.begin() == False) {aio.begin(); Serial.print(".");delay(300);}
// grab the temperature feed
Adafruit_IO_Feed tempSens = aio.getFeed("tempSens");
// send temp level to AIO
tempSens.send(level);
blinkLED();
return level;
}
// Function for linearlly scaling int values into float values ('map' only does int to int)
float floatmap(long x, long in_min, long in_max, float out_min, float out_max) {
return ((float)x - (float)in_min) * (out_max - out_min) / ((float)in_max - (float)in_min) + out_min;
}
// Function for quick blink of the defult red LED
void blinkLED() {
digitalWrite(0, LOW);
delay(200);
digitalWrite(0, HIGH);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment