The ESP-8266, in my case the ESP-01, is a postage-stamp size bit of electronics that contains everything you need to add wireless networking to a project. They are cheap too - when I bought mine, they were around $5 each. They are even less now.
Able to work as a wireless client or as a wi-fi access point, I have had a ton of fun with these in the past, the best one was making a captive portal for nosy neighbors who wonder what the "FBI SURVEILLANCE VAN" was that showed up on their list of wireless networks. When they connected to it, it showed a convincing login page with scary legalese and played back a loop of recorded random police scanner traffic.
Usually I use them for more mundane activities - another variant called the WiFi Kit 8 is about the size of a thumb drive and as thick as a stick of gum. It has a built-in OLED display that displays a list of active WiFi networks whenever you apply 5V power to it.
So back to the ESP8266.
For as nice as these things are, they are not friendly to hook up. To me, friendly is the ability to use it with a breadboard - adapters exist for this, but they were not available when I ordered them, and I don't have any, so there you go.
The ESP-01 likes 3.3V, so it is not 5V friendly. So when pairing it up to an Arduino, Teensy, or what have you, you'll need to either convert the voltages from 5V to 3.3V, or just do what I do and get a 3.3V Arduino or Teensy.
So how easy is this thing to use? Pretty easy:
That's it. All the code necessary to start up and connect to a WiFi network.
Setting up an access point isn't that much different:
All that in a few lines of code, amazing.
But that's just the setup, now you need to do something with it.... I needed to sync with a time source - Internet, GPS, I don't care how, but I have these ESP8266s sitting around waiting to be put to good use. So NTP it is. Rather than re-invent the wheel, I looked for existing solutions, and there was:
ARDUINO TIME SYNC FROM NTP SERVER USING ESP8266 WIFI MODULE
The code from here for the ESP8266 was used unmodified aside from making a blinking LED to tell me the ESP8266 is actually running.
The Teensy's Serial1 was used by the WAV Trigger, so Serial2 is used instead.
On the Matrix board, I modified what was supplied at the site above into a function:
This is called from the main loop(). If it finds a message waiting - that means the time sync signal (which comes in every few minutes, I need to slow it down a bit) has arrived. The ESP8266 runs independently from the Teensy I am using, so it can take up to a minute or so for the time to sync properly.
My modifications are:
1. Do not sync the time if a ludicrous result arrives. Ignore any time change more than 2 hours (7200 seconds) unless we are starting up.
2. Set a flag when the time syncs for the first time - we always sync in that case.
3. Sync the Teensy's RTC with the received time
4. Get the local time (PDT and PST) appropriately.
Gotta love that Timezone Library, it made life a lot easier, and I know the precise time in my area:
And there it is:
Blinking blue light means it's still alive and working.
Able to work as a wireless client or as a wi-fi access point, I have had a ton of fun with these in the past, the best one was making a captive portal for nosy neighbors who wonder what the "FBI SURVEILLANCE VAN" was that showed up on their list of wireless networks. When they connected to it, it showed a convincing login page with scary legalese and played back a loop of recorded random police scanner traffic.
Usually I use them for more mundane activities - another variant called the WiFi Kit 8 is about the size of a thumb drive and as thick as a stick of gum. It has a built-in OLED display that displays a list of active WiFi networks whenever you apply 5V power to it.
So back to the ESP8266.
For as nice as these things are, they are not friendly to hook up. To me, friendly is the ability to use it with a breadboard - adapters exist for this, but they were not available when I ordered them, and I don't have any, so there you go.
The ESP-01 likes 3.3V, so it is not 5V friendly. So when pairing it up to an Arduino, Teensy, or what have you, you'll need to either convert the voltages from 5V to 3.3V, or just do what I do and get a 3.3V Arduino or Teensy.
So how easy is this thing to use? Pretty easy:
C:
#include <ESP8266WiFi.h>
char ssid[] = "MY NETWORK"; // your network SSID (name)
char pass[] = "MY NETWORK PASSWORD"; // your network password
void setup()
{
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
That's it. All the code necessary to start up and connect to a WiFi network.
Setting up an access point isn't that much different:
C:
#include <ESP8266WiFi.h>
char ssid[] = "MY NETWORK"; // your network SSID (name)
char pass[] = "MY NETWORK PASSWORD"; // your network password
void setup()
{
WiFi.softAP(ssid, pass);
}
All that in a few lines of code, amazing.
But that's just the setup, now you need to do something with it.... I needed to sync with a time source - Internet, GPS, I don't care how, but I have these ESP8266s sitting around waiting to be put to good use. So NTP it is. Rather than re-invent the wheel, I looked for existing solutions, and there was:
ARDUINO TIME SYNC FROM NTP SERVER USING ESP8266 WIFI MODULE
The code from here for the ESP8266 was used unmodified aside from making a blinking LED to tell me the ESP8266 is actually running.
The Teensy's Serial1 was used by the WAV Trigger, so Serial2 is used instead.
On the Matrix board, I modified what was supplied at the site above into a function:
C:
void syncTime()
{
char buffer[40];
int buffer_loop = 0;
TimeChangeRule *tcr; // pointer to the time change rule, use to get the TZ abbrev
utc = now();
time_t old_t;
while (Serial2.available()) {
buffer[buffer_loop++] = Serial2.read();
dataSync = true;
}
if (dataSync == true) {
if ((buffer[0] == 'U') && (buffer[1] == 'N') && (buffer[2] == 'X')) {
// if data sent is the UNX token, take it
unixString[0] = buffer[3];
unixString[1] = buffer[4];
unixString[2] = buffer[5];
unixString[3] = buffer[6];
unixString[4] = buffer[7];
unixString[5] = buffer[8];
unixString[6] = buffer[9];
unixString[7] = buffer[10];
unixString[8] = buffer[11];
unixString[9] = buffer[12];
unixString[10] = '\0';
// print the UNX time on the UNO serial
Serial.println();
Serial.print("TIME FROM ESP: ");
Serial.print(unixString[0]);
Serial.print(unixString[1]);
Serial.print(unixString[2]);
Serial.print(unixString[3]);
Serial.print(unixString[4]);
Serial.print(unixString[5]);
Serial.print(unixString[6]);
Serial.print(unixString[7]);
Serial.print(unixString[8]);
Serial.print(unixString[9]);
Serial.println();
unixTime = atol(unixString);
// Synchronize the time with the internal clock
// for external use RTC.setTime();
old_t = now();
if ((abs(unixTime - old_t) < 7200) || !timeSync) //sync if time is recent (within 2 hours) or we just had power applied
{
setTime(unixTime); //set current time to UNIX time from NTP server
Teensy3Clock.set(now()); //set RTC to current time
utc = now(); //now pull the current time
pacific = usPacific.toLocal(utc, &tcr); //and load it into the time change rule.
if (usPacific.utcIsDST(utc)) //check for DST and print out results
printDateTime(usPDT, utc, " Los Angeles");
else
printDateTime(usPST, utc, " Los Angeles");
timeSync = true;
}
else
{
Serial.print("Sync error: delta = "); // if difference is too great, print the difference
Serial.println(abs(unixTime - old_t));
}
dataSync = false;
}
}
}
This is called from the main loop(). If it finds a message waiting - that means the time sync signal (which comes in every few minutes, I need to slow it down a bit) has arrived. The ESP8266 runs independently from the Teensy I am using, so it can take up to a minute or so for the time to sync properly.
My modifications are:
1. Do not sync the time if a ludicrous result arrives. Ignore any time change more than 2 hours (7200 seconds) unless we are starting up.
2. Set a flag when the time syncs for the first time - we always sync in that case.
3. Sync the Teensy's RTC with the received time
4. Get the local time (PDT and PST) appropriately.
Gotta love that Timezone Library, it made life a lot easier, and I know the precise time in my area:
Code:
18:36:44 Fri 08 May 2020 PDT Los Angeles
And there it is:
Blinking blue light means it's still alive and working.
Last edited: