ESP8266 ioT Timer
Lots of people ask me about IoT ("The Internet of Things"). It's a currently warm, if no longer hot, buzzword.
Problem is I find a lot of the instruction out there overcomplicate things at the beginning, but also often leave people right at the dangerous part (security).
In this tutorial, I will show you how to make a simple sensor data logger/timer. We will sense a pin has been made active, increment a counter on an LED screen, and send data to a "cloud" data service over wifi.
With these basics, you will have the foundations you need to really dig into what you can do.
I originally started working on this as a way to measure the time people were spending using the welding equipment at our maker space. The trigger of the welder would send the pin high, which would start the counter and the data logging.
After tinkering with it I realized it was a good demo for anything that needed to log data over the internet.
As you look at the code, consider how you might modify it for your own uses.
What you will need
NodeMCU/ESP8266 based board
I2C LED Screen
Breadboard (optional)
UPDATE: the current tutorial uses Data.Sparkfun which just closed - I will update with an alternative
As you can see, the LED display takes two wires plus 5v and Ground.
I am using a NodeMCU, which is an easy to obtain and cheap ESP8266 board. You could just as easily using an Adafruit Feather, or the Sparkfun equivalent.
Code
All the code is available in Github here. I doubt it will change at this point because I've been demoing and teaching it for a while without modification, but if I do change it those changes will appear in the repo :)
First we will look at the Arduino side:
Include the libraries with
#include <TM1637Display.h>
... format.
For this demo we are using A0 as the input pin. This is an "analog" pin, You can attach a light sensor, sound sensor, etc, or just apply voltage manually. We start the count if the input is greater than roughly half (500).
The Wifi piece should be relatively straightforward, provided you remembered to add your wifi network and password.
We send the data to data.sparkfun.com service. This allows us to log info using straight URL parameters.
Just to demonstrate also creating a visual confirmation, we use our I2C LED display using a simple call to
display.showNumberDec( count );
After the Arduino code we can call back some data using PHP.
#include <TM1637Display.h>
#include <ESP8266WiFi.h >
#define DEVICE\_NAME "TIG\_WELDER" // So the same code/stream can work for different tools
const char* ssid = "YOUR NETWORK"; // SSID
const char* password = "YOUR PASSWORD"; // Password
const char* host = "data.sparkfun.com"; // website
//input (assuming an analog signal)
int sensorPin = A0; //the analog pin
// Module connection pins (Digital Pins)
#define CLK D5
#define DIO D4
// The amount of time (in milliseconds) to wait
// take into account the time to send the data also
#define TEST_DELAY 1000
// LED pin to blink
#define ESP8266_LED 16
// set up the display
TM1637Display display(CLK, DIO);
// set up and initialize
void setup()
{
// set up the pins
pinMode(ESP8266_LED, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
// set the brightness
display.setBrightness(0x0a);
// start the wifis!
WiFi.begin(ssid, password);
// wait for it to work
while (WiFi.status() != WL_CONNECTED) {
display.showNumberDec( 8080 );
delay(500);
}
}
// Rinse and repeat
void loop() {
// reset to zero
display.showNumberDec( 0 ); // send to the display
int reading = analogRead( sensorPin ); // current voltage off the sensor
// we just want to know if it is a strong signal right now
if ( reading &gt; 500 ) {
// this is the internet bit
// send that a session started
connect( "START" );
}
// set counter to zero
int count = 0;
// while we have a strong signal, keep counting
while ( reading > 500 ) {
// increment!
count++;
// now we display it in decimal
display.showNumberDec( count ); // send to the display
// we are counting "seconds" so put in a delay
delay(TEST_DELAY);
// get another reading
reading = analogRead(sensorPin); // current voltage off the sensor
}
// if the count is greater than zero there must have been a session
// that ended so send the end event
if ( count &gt; 0 )
{
connect( "END" );
}
}
void blink()
{
// blinking the LED
// yes I could use a for loop but I didn't. Copypasta FTW
digitalWrite(ESP8266_LED, HIGH);
delay(100);
digitalWrite(ESP8266_LED, LOW);
delay(100);
digitalWrite(ESP8266_LED, HIGH);
delay(100);
digitalWrite(ESP8266_LED, LOW);
delay(100);
digitalWrite(ESP8266_LED, HIGH);
delay(500);
digitalWrite(ESP8266_LED, LOW);
delay(100);
}
void connect( String event ) {
// you probably don't need this but I found
// it unresponsive unless I waited a bit
delay(200);
// blink the LED to show stuff is happening
blink();
// Use WiFiClient class to create TCP connections
WiFiClient client;
// port = 80 for web stuffs
const int httpPort = 80;
// try out that connection plz
if (!client.connect(host, httpPort)) {
return;
}
// We now create a URI for the request including our data
String url = "/input/NJODn6OOZwF30qpYVpwo?private_key=ENTER YOUR PRIVATE KEY&amp;device=";
url += DEVICE_NAME; // name of the device, eg. WELDER
url += "&amp;event=";
url += event; // did the session start or end?
// This will send the request to the server
// we are not interested in the response in this example
client.print(String("GET ") + url + " HTTP/1.1\\r\\n" +
"Host: " + host + "\\r\\n" +
"Connection: close\\r\\n\\r\\n");
// tiny delay
delay(10);
}
PHP Code
I am using PHP because that is what I had access to for sure on the Protospace Makerspace machine, but it is simply calling cURL so any language will do:
<html>
<head>
<title>Protospace Machine Log</title>
</head>
<body>
<h2>Machine Log</h2>
<pre>
<? // Sparkfun uses keys rather than passwords $api\_key = "ENTER YOUR API KEY HERE"; // create curl resource $ch = curl\_init(); curl\_setopt\_array($ch, array( //don't output CURLOPT\_RETURNTRANSFER =><span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce\_SELRES_start"></span> 1,
// URL w. device-id and function:
CURLOPT\_URL => "https://data.sparkfun.com/output/$api\_key.json",
// useragent: your cloud user
CURLOPT_USERAGENT => "internets"
));
//execute the post
$result = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$results = json_decode( $result );
print_r( $results );
?>
</pre>
</body>
</html>
Damn! Think I'll need to go get some of that and visit a nearby makers' space! :D
Has data.sparkfun.com any limitations about size or number of requests?
It actually appears the end of life'd the service - I will update with another tutorial for alternatives
That’s always my main concern about IOT, how will the item work when a central data service is going down. I think IOT needs Blockchain to be able to maintain the Data req. for the lifetime of an item without requiring the manufacturer to survive.
Your post had been curated by the @buildawhale & @ipromote team and mentioned here:
https://steemit.com/curation/@buildawhale/buildawhale-curation-digest-02-01-18
Keep up the good work and original content, everyone appreciates it!
Does this timer has any gps functions?
If not, a gps function would be nice to have in case you started developing products for the IOT industry.
Keep me informed on you next hack.