JChristensen / JC_Button

Arduino library to debounce button switches, detect presses, releases, and long presses
GNU General Public License v3.0
423 stars 101 forks source link

Usage notes for ESP8266 #9

Closed bperrybap closed 6 years ago

bperrybap commented 6 years ago

The current library code does work on esp8266 even though the architecture in the .properties says avr only. That said, I spent a day pulling my hair out trying to figure out some issues that were due to some internal design limitations and external h/w used on the esp8266 parts. I was using a WeMos D1 mini. So here are some usage notes from my clock project:

 * GPIO0, GPIO2, and GPIO15 are special inputs used at reset and power up.
 * GPIO2 is also used for LED control of the built in LED.
 * They control the boot mode:
 *  GPIO15    GPIO0    GPIO2    MODE       Description
 *    L         L        H      UART    Download Code from Uart
 *    L         H        H      FLASH   Boot from SPI flash (normal)
 *    H         x        x      SDIO    Boot from SD card
 *
 * Programing flash using uart0 requires a boot mode of "boot from uart0"
 * with boot_sel=b001, that is, it must make sure
 * GPIO15=0, GPIO0=0, and GPIO2=1 during reset 
 * Because of this, GPIO15 has an external pulldown.
 * This makes these GPIO pins unsuitable for simple grounding switches especially
 * when switches can be pressed at powerup to set modes.
 * GPIO15 and GPIO0 do appear to work correctly after boot and GPIO0 does
 * not appear to have a pull down on it.
 * more docs here:
 * http://esp8266.github.io/Arduino/versions/2.0.0/doc/reference.html
 * https://github.com/CHERTS/esp8266-devkit/tree/master/Espressif/docs/ESP8266

Then there is the difference between Arduino pin #s, which match GPIOn bit numbers on all esp8266 modules, but are different from the Dn symbols provided by the WeMos boards - which are only used on the WeMos boards. i.e. on WeMos boards pin 0 is not the same as pin D0

I think some form of this information, perhaps placed in the readme or the examples, could be very helpful for esp8266 users.

For the examples, GPIO2 is used for the LED so Arduino pin 2 should not be used.


If you don't have any ESP8266 boards to play with, I'd recommend getting a few they are really inexpensive. My preference is the Wemos D1 mini, and the WeMos D1/Linknode D1 (for uno form factor), and don't get the Wemos D1 R2 as the pinout mapping is a total wreck on the board. I would encourage everyone to avoid that board. On the uno formfactor boards you want to see D0, D1, etc.. in the normal uno positions starting with Rx on pin 0. The R2 boards don't do this so the pin mapping is mess and makes the board a pain to use.

JChristensen commented 6 years ago

It's hard to know what to say. I haven't gotten into the ESP8266 boards at all. This is not a matter of expense, but rather a matter of time. Certainly they sound like they are capable and a lot of fun. I set the architecture in my libraries to AVR because that's all I am working with. I do not and cannot test on other architectures. There is a proliferation of boards and architectures and while I think that's a fine thing, it forces me to make choices. I can work to continually update my libraries for every new kid on the block (new boards and architectures that I do not, and may never use) or I can work on my own projects.

Sorry for the rant, I hope it explains where I'm coming from. I do appreciate the effort and enthusiasm. I do wonder if I'm in the wrong game if the general expectation is to support any and all comers.

bperrybap commented 6 years ago

I had assumed you were at least looking at the esp8266 parts from seeing issue #4. I get that time is limited and I'm not suggesting that any/all architectures and boards should be supported; however, do consider that there are other (non AVR) more cost effective Arduino solutions for hobbyists out there especially for IoT type projects. I think the esp modules are by far the most interesting non AVR Arduino supported devices. For just a few dollars, you get megabytes of flash that can even be used as a file system for file storage, and tons of ram, not mention they are 32 bit and much faster than AVR AND include wifi support including the ability do OTA sketch updates from the Arduino IDE. There is no other solution out there that offers all this at this price point.

esp8266 modules are easy to use and the espressif documentation including s/w documentation is better than most of what I have seen on many products/projects over the past several decades. It is pretty amazing - they claim to have shipped 100M devices. That is massive. From what I've seen , so far they have responded to reported issues quite quickly. I would encourage you to at least have a look at them. The esp8266 isn't like some of the other architectures that have all kinds Arduino board specific things. The esp architecture is well defined and since it comes in a module from espressif, esp8266 based boards use the the pre-made modules which dramatically reduces the type of differences you can see in other Arduino type boards/devices. They are incredible little modules with outstanding bang for the buck with an ever growing amount of Arduino support and readily available projects.

JChristensen commented 6 years ago

Thank you very much and again I hope I didn't go off too much there. I am aware of the ESP8266's impressive specifications etc. They certainly have become a huge force in the market and community. I wish I had more time to work with them. Certainly my interest is piqued. The other day I traded a couple emails with a guy that was buying ESP-01 modules from AliExpress for $1.62, and with free shipping to boot. Pretty amazing. When simple things like issue #4 come up, I try to be accommodating and not take a hard line.

bperrybap commented 6 years ago

No worries. There is never enough time. At this point the library works great on the esp8266 modules so no actual library code changes are need for other processors. The issues I ran into were esp8266 limitations that are well known but I wasn't aware of and were not related to this library at all.

My preference at this point is WeMos D1 mini and the Wemos D1 R1 / Linknode D1.

I've played with them for quite some time testing them with my own Arduino libraries but now that I'm actually using it in a real project (a WordClock) - replacing a Teensy2 with a WeMos D1 mini to be able to auto detect timezones and get network time, I've bumped into some teething issues in various libraries and even a few issues the esp8266 core code itself. So far, things have been pretty easy to fix and get resolved.

JChristensen commented 6 years ago

See, a project like that might get me interested, LOL! How do you auto-detect the timezone?

bperrybap commented 6 years ago

You can use Timezone API at http://timezoneapi.io/ The timezone API is really cool. It can locate information based on IP address or city, or actual street address. It returns a bunch of JSON encoded information about the location including wheather or not DST is in effect that can easily be extracted using the ArduinoJson library. It is free for low volume use. You can even try it using a web browser to see the results. Here is a sample project that shows how to use it. https://github.com/atatistcheff/ESP8266-Auto-TZ-NTP/blob/master/ESP8266-Auto-TZ-NTP.ino My word clock project will be using Timezone API to set and keep the RTC module in sync and will also check for DST changes at least once per day so that the displayed time is adjusted automatically for DST changes.

I currently have a little demo project I'm using for testing that fetches the timezone information and displays the date/time city and state on a hd44780 i2c based LCD without having to enter anything - other than the wifi router information to connect to the router. No RTC, no battery, just a esp8266 and an LCD with an i2c backpack, and it keeps very accurate time and does automatic DST changes.

For another project (The Evil Mad Scientist bulb dial clock), I'll be using a WeMos D1 mini to interface to the AVR on the bulb dial clock to send a modified/local time_t to the AVR over the serial port. (It will be stuck to back of the main PCB using double stick tape) Using the serial port to set the time is already supported by the bulb dial clock code. All I have to do for this project is get the network time and adjust the time_t to be a local time and send it to the AVR. It is a very simple self contained project that adds network time and automatic DST changes to the bulb dial clock. I originally wanted to use the esp8266 in i2c slave mode to emulate a DS3231 so that the AVR would just get the time over i2c from the esp8266 and not know the difference. Lots of applications for something like this (a networked synced DS3231); However, the esp8266 does not support i2c slave mode due to the way their internal OS works. The Arduino code runs as a task and the interrupt attach stuff doesn't work very well since it doesn't use real interrupts, as such it can't support the real time needs of a s/w based i2c slave.

JChristensen commented 6 years ago

I was looking for something like Timezone API a while back. Figured there should be such a thing, but never found it somehow. Thanks!