ESP cookbook

Collecting snippets that can quickly be copied to assemble a program for different sensors etc. Mostly about ESP8266/32 with Arduino IDE.

Similar projects worth following
Mostly, when starting a new project, I go through sample programs for each sensor/display needed for the job, and copy snippets from them. Testing, if it would be beneficial to collect this kind of ready-made snippets on a single page, from where they could be quickly copied to whatever project under work.

See "Instructions" for the recipes, "Details" for the table of contents.

List of current "instructions"

Page 1 - General

1. ESP 8266 pin uses and restrictions

2. Serial communication

3. Network connectivity - TCP/IP and HTTP

4. Thingspeak

5. Writing float to character buffer

6. GPIO & timer interrupts, deep sleep

7. Persistent variable storage - EEPROM, RTC, SPIFFS

8. 74HC4051 Multiplexer

9. MCP4725 I²C DAC

Page 2 - Temperature etc sensors

11. TMP36 analog temperature sensor

12. DS18B20 one-wire temperature sensor

13. DHTxx temperature / humidity sensor (AKA AM23xx)

14. BMP280 / BME280 I²C air pressure, temperature and humidity (BME) sensor.

15. MLX90614 I²C infrared temperature sensor

16. Dew point calculation

17. BH1750FVI I²C light intensity sensor

18. HC-SR04 ultrasonic distance sensor

19. VL53L0x I²C laser Time of Flight distance sensor

Page 3 - Displays

21. 0.96" / 1,3" I²C OLED displays

22. 0.96 / 1.3" SPI OLED displays

23. SPI LCD displays

124. WaveShare SPI 2.9" e-Paper

25. WS2811, WS2812, WS2812B, WS2813 individually addressable LEDs (no snippets so far)

  • 1
    ESP 8266 pin uses and restrictions

    Not all ESP 8266's GPIOs are born equal, something I have now and then been banging my head at (and burned some ESPs). Some are hard wired for some use, like SPI clock and data, some wired to functionality like flashing and booting. For now Wemos D1 mini's pins, others to be added. Some limitations/features might be missing, to be added when bumped into.

    Dev board Dx, Arduino IDE aliasGPIO # (for LUA etc)Notes
    D016- Wire to RST for timed wake from deep sleep
    - Doesn't support interrupts, PWM, I²C, one-wire
    D15Default I²C SCL, but others except D0 fine for that too.
    D24Default I²C SDA, but others except D0 fine for that too.
    D30- Wemos has 10k pull-up, convenient for I²C.
    - Used for booting/flashing, don't hard wire to GND/Vcc. Wemos has hard pull-down for flashing by serial RTS.
    - Otherwise after boot works as a generic digital I/O
    D42- Built in LED
    - Wemos has 10k pull-up, convenient for I²C.
    - Used for booting, doesn't boot up if pulled down while starting.
    - Otherwise after boot works as a generic digital I/O
    D514Hardware SPI clock, but others can be used for software SPI. Fine for other uses.
    D612Hardware SPI MISO, but others can be used for software SPI. Fine for other uses.
    D713Hardware SPI MOSI, but others can be used for software SPI. Fine for other uses.
    D815- Wemos has 10k pull-down. Not good for I²C, which would require pull-up.
    - Used for booting/flashing, doesn't boot up if pulled up while starting.
    - Otherwise after boot works as a generic digital I/O
    A0A0ADC input, 0-1 V for generic ESP8266, 0-3.3 V for Wemos D1 mini (which has a 220k/100k resistor voltage divider)
    TX1Serial TX, other GPIO use possible
    RX3Serial RX, I²S DMA output, other GPIO use possible
  • 2
    Serial communication

    After soon 60 years the RS-232 standard is doing strong, as that's what we're talking with to our most modern IoT thingys (yep I know about TTL vs positive/negative levels, pretty close anyway I'd say). Pretty much any MCU board talks RS-232 to the computer, nowadays generally through a UART inside the MCU board or cable, thus the Serial library is probably the most common denominator that's always available for I/O.


    None needed


      Serial.begin(115200); //9600 would be more common, but now and then a tad slow


        Serial.println("hello"); //With line feed 
        Serial.print((String)"Temperature: " + temp + " humidity: " + humidity); //String library handles concatenating 
    // Input two comma separated integers. Note: This simple version blocks CPU, if needs to wait (default 1 sec) for another integer.
        if (Serial.available() > 0)
          if (Serial.peek() > 42) { //Ignore LF,CR etc
            userInput1 = Serial.parseInt(); //Adjust number of these as needed
            userInput2 = Serial.parseInt();
          } else {
            Serial.readString(); //Ditch useless data

  • 3
    Network connectivity - TCP/IP and HTTP

    For pretty much any ESP project I'd want the data to be sent to somewhere, thus networking would be like garlic in cooking - first chop it and then start wondering what are you actually going to cook.


    #include <ESP8266WiFi.h>
    #include <ESP8266HTTPClient.h>
    const char *WLAN_SSID = "My-network-SID-address";
    const char *WLAN_PASSWORD = "My-network-pw";
    HTTPClient http;


    while (WiFi.status() != WL_CONNECTED) { 
    Serial.println((String)"\nConnected: " + WiFi.localIP() + " RSSI: " + WiFi.RSSI());

    Use (HTTP GET call)

    // Helper function
    int doHttpGet(String url) {
        int httpCode = -1;
        for(int i = 0; i < 20; i++) {
        int httpCode = http.GET();
        Serial.println((String)"URL: " + url);
        Serial.println((String)"HTTP return code: " + httpCode + " " + http.errorToString(httpCode));
        if(httpCode > 0) //Other retry codes can be added
        return httpCode;
    // Do the request:

View all 25 instructions

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates