Editing
esp
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Libraries== [https://arduino-esp8266.readthedocs.io/en/latest/libraries.html Reference]<br> ===WiFi(ESP8266WiFi library)=== This is mostly similar to WiFi shield library. Differences include:<br> :'''WiFi.mode(m)''' ::set mode to WIFI_AP, WIFI_STA, WIFI_AP_STA or WIFI_OFF. :'''call WiFi.softAP(ssid)''' ::to set up an open network. :'''call WiFi.softAP(ssid, password)''' ::to set up a WPA2-PSK network (password should be at least 8 characters) :'''WiFi.macAddress(mac)''' ::is for STA, WiFi.softAPmacAddress(mac) is for AP. :'''WiFi.localIP()''' ::is for STA, '''WiFi.softAPIP()''' is for AP. :'''WiFi.printDiag(Serial)''' ::will print out some diagnostic info :'''WiFiUDP class''' ::supports sending and receiving multicast packets on STA interface. When sending a multicast packet, replace udp.beginPacket(addr, port) with udp.beginPacketMulticast(addr, port, WiFi.localIP()). When listening to multicast packets, replace udp.begin(port) with udp.beginMulticast(WiFi.localIP(), multicast_ip_addr, port). You can use udp.destinationIP() to tell whether the packet received was sent to the multicast or unicast address. <br> :WiFiServer, WiFiClient, and WiFiUDP behave mostly the same way as with WiFi shield library. Four samples are provided for this library. <br> You can see more commands [http://www.arduino.cc/en/Reference/WiFi here]. <br> ====WL_DEFINITIONS==== :WL_IDLE_STATUS = 0, :WL_NO_SSID_AVAIL = 1, :WL_SCAN_COMPLETED = 2, :WL_CONNECTED = 3, :WL_CONNECT_FAILED = 4, :WL_CONNECTION_LOST = 5, :WL_WRONG_PASSWORD = 6, :WL_DISCONNECTED = 7 {{template:top}} ====Function to print the wifi.status==== <nowiki>const char* wl_status_to_string(wl_status_t status) { switch (status) { case WL_IDLE_STATUS: return "WL_IDLE_STATUS"; case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL"; case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED"; case WL_CONNECTED: return "WL_CONNECTED"; case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED"; case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST"; case WL_DISCONNECTED: return "WL_DISCONNECTED"; } } Serial.println(wl_status_to_string(WiFi.status()));</nowiki> {{template:toponly}} =====WiFi.status()===== The status function in the WiFi class, doesn’t take any arguments but it returns stuff depending on the status of the network that you’re connected to. ;The WiFi.status function returns a value:<br> {| class="wikitable" ! style="font-weight:bold;" | Code ! style="text-align: center; font-weight:bold;" | Vulue ! style="font-weight:bold;" | Meaning |- | WL_IDLE_STATUS | style="text-align: center;" | 0 | WiFi is in process of changing between statuses |- | WL_NO_SSID_AVAIL | style="text-align: center;" | 1 | SSID cannot be reached |- | WL_SCAN_COMPLETED | style="text-align: center;" | 2 | |- | WL_CONNECTED | style="text-align: center;" | 3 | Successful connection is established |- | WL_CONNECT_FAILED | style="text-align: center;" | 4 | Password is incorrect |- | WL_CONNECTION_LOST | style="text-align: center;" | 5 | |- | WL_CONNECT_WRONG_PASSWORD | style="text-align: center;" | 6 | Password is incorrect | |- | WL_DISCONNECTED | style="text-align: center;" | 7 | not configured in station mode |} If you get code 1, WL_NO_SSID_AVAIL, try adding this before WiFi.begin(): WiFi.enableInsecureWEP(); <br> It is recommended by some to set the mode to STA: WiFi.mode(WIFI_AP_STA); WiFi.begin(); {{template:top}} ===Ticker=== Library for calling functions repeatedly with a certain period. Two examples included.<br> It is currently not recommended to do blocking IO operations (network, serial, file) from Ticker callback functions. Instead, set a flag inside the ticker callback and check for that flag inside the loop function.<br> ;Examples: ::[[:Ticker.h|Ticker.h Examples]]<br> ::[[:C_Reference#Ticker.h|C Reference: ticker.h]]<br> {{template:top}} ===EEPROM=== ;Reading and Writing Data Structures to EEPROM Using structures, reading and writing to the EEPROM is reduced to a single call.<br> (Reference from [https://playground.arduino.cc/Code/EEPROMWriteAnything/ playground.arduino.cc].<br> I used this in my Monkey Detector to record the servo positions and timing between boot sessions.<br> First, add a tab in the IDE named: '''eepromAnything.h''', then add this code:<br> <nowiki>// https://playground.arduino.cc/Code/EEPROMWriteAnything/ #include <EEPROM.h> #include <Arduino.h> // for type definitions template <class T> int EEPROM_writeAnything(int ee, const T& value) { const byte* p = (const byte*)(const void*)&value; unsigned int i; for (i = 0; i < sizeof(value); i++) EEPROM.write(ee++, *p++); return i; } template <class T> int EEPROM_readAnything(int ee, T& value) { byte* p = (byte*)(void*)&value; unsigned int i; for (i = 0; i < sizeof(value); i++) *p++ = EEPROM.read(ee++); return i; }</nowiki> <br><br> ;At the top of the sketch, add this: <nowiki>#include "eepromAnything.h" //structure declaration with three values as members. struct servo { int locked_Position ; int unlocked_Position ; unsigned long drawerTime; }; //create an unitialized struct variable called myServo. struct servo myServo; //Declare the individual variables for use later. //I could also use the struct? int locked_Position = myServo.locked_Position; int unlocked_Position = myServo.unlocked_Position; unsigned long drawerTime = myServo.drawerTime; </nowiki> <br><br> ;In setup(), I read the EEPROM just for verification: <nowiki> EEPROM_readAnything(0, myServo); locked_Position = myServo.locked_Position; unlocked_Position = myServo.unlocked_Position; drawerTime = myServo.drawerTime; Serial.print(F("lock-unlock-time= ")); Serial.print(locked_Position); Serial.print(F(" - ")); Serial.print(unlocked_Position); Serial.print(F(" - ")); Serial.println(drawerTime);</nowiki> <br><br> ;When I write data to the EEPROM: <nowiki>myServo.locked_Position = locked_Position; myServo.unlocked_Position = unlocked_Position; myServo.drawerTime = drawerTime; EEPROM_writeAnything(0, myServo);</nowiki> <br> {{template:top}} ;Old EEPROM Stuff [https://github.com/esp8266/Arduino/tree/master/libraries/EEPROM/examples EEPROM Library] for the ESP devices. <br> :This is a bit different from standard EEPROM class. :'''EEPROM.begin(size)''' ::You need to call EEPROM.begin(size) before you start reading or writing, size being the number of bytes you want to use. Size can be anywhere between 4 and 4096 bytes.<br> :'''EEPROM.write()''' :EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents. :'''EEPROM.commit()''' ::you must call EEPROM.commit() whenever you wish to save changes to flash. :'''EEPROM.end()''' ::EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents. <br> :EEPROM library uses one sector of flash located just after the SPIFFS. <br><br> ;The ESP8266 handles EEPROM different from the Arduino.<br> Here is a sketch that I found on the Google machine a few months ago, edited for readability.<br> Sample code:<br> <nowiki>#include <EEPROM.h> void setup() { Serial.begin(115200); while (!Serial) {} Serial.println(); Serial.println("EEPROM_esp8266_example.ino"); /* Using the ESP8266 EEPROM is different from the standard Arduino EEPROM class. You need to call EEPROM.begin(size) before you can start reading or writing, where the size parameter is the number of bytes you want to use store Size can be anywhere between a minimum of 4 and maximum of 4096 bytes. */ EEPROM.begin(32); //EEPROM.begin(Size) /* Commands EEPROM.write or EEPROM.put do not write to flash immediately, to invoke them you must call EEPROM.commit() to save changes to flash/EEPROM. EEPROM.end() will also commit, but releases the RAM copy of EEPROM contents. */ // Commands to determine variable sizes, needed for storing to EEPROM Serial.println(); Serial.println(" Floating point variables need: " + String(sizeof(float)) + " Bytes"); // Determine how many bytes (4) are needed to save a floating point variable Serial.println("Double size floating point variables need: " + String(sizeof(double)) + " Bytes"); // Determine how many bytes (8) are needed to save a floating point variable Serial.println(" Integer variables need: " + String(sizeof(int)) + " Bytes"); // Determine how many bytes (4) are needed to save an integer variable Serial.println(" Boolean values or variables need: " + String(sizeof(bool)) + " Bytes"); // Determine how many bytes (1) are needed to save a boolean variable Serial.println(" String variables need at least: " + String(sizeof(String)) + " Bytes"); // Determine how many bytes (min. 12) are needed to save a string variable Serial.println(); //---------------------------------------------------------------------------- // Example-1 Write a value to EEPROM at address = 0 int EEaddress = 0; EEPROM.write(EEaddress, 123); // Writes the value 123 to EEPROM // 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM // 7B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of RAM EEPROM.commit(); // 7B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM // Serial.print(F("Write a value: 123 to EEPROM at address = 0")); Serial.print("EEPROM contents at Address=0 is : "); Serial.println(EEPROM.read(EEaddress)); //---------------------------------------------------------------------------- // Example-2 Write a value to EEPROM at address = 0 // 7B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM EEPROM.write(EEaddress, 257); // Writes the value 257 to EEPROM EEPROM.commit(); // 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM Serial.print("EEPROM contents at Address=0 is : "); Serial.println(EEPROM.read(EEaddress)); //---------------------------------------------------------------------------- // Example-3 Write an integer variable to EEPROM at address = 0 int integer_variable = 257; // 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM EEPROM.put(EEaddress, integer_variable); // Writes the value 257 to EEPROM EEPROM.commit(); // 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM Serial.print("EEPROM contents at Address=0 is : "); EEPROM.get(EEaddress, integer_variable); Serial.println(integer_variable); //---------------------------------------------------------------------------- // Example-4 Write another integer variable to EEPROM int integer_variable2 = 1234; EEaddress = EEaddress + sizeof(int); // Moves the address along by 4 EEPROM.put(EEaddress, integer_variable2); // Writes the value 1234 to EEPROM EEPROM.commit(); // 01 01 00 00 D2 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // Contents of EEPROM Serial.print("EEPROM contents at Address=4 is : "); EEPROM.get(EEaddress, integer_variable2); Serial.println(integer_variable2); EEaddress = EEaddress + sizeof(int); // Moves the address along by 4 //---------------------------------------------------------------------------- // Example-5 Write a floating point variable to EEPROM float floatingpoint_variable = 3.141592654; EEPROM.put(EEaddress, floatingpoint_variable); // Writes the value 3.141592654 to EEPROM EEPROM.commit(); Serial.print("EEPROM contents at Address=8 is : "); EEPROM.get(EEaddress, floatingpoint_variable); Serial.println(floatingpoint_variable, 8); EEaddress = EEaddress + sizeof(float); // Moves the address along by 4 //---------------------------------------------------------------------------- // Example-6 Write a string variable to EEPROM String string_variable = "Hello world"; EEPROM.put(EEaddress, string_variable); // Writes the value 3.141592654 to EEPROM EEPROM.commit(); Serial.print("EEPROM contents at Address=12 is : "); EEPROM.get(EEaddress, string_variable); Serial.println(string_variable); EEaddress = EEaddress + sizeof(string_variable); // Moves the address along by 4 //---------------------------------------------------------------------------- // Example-7 Write a series of values to EEPROM for (int i = 1000; i <= 1032; i = i + 4) { EEPROM.put(i - 1000, i); // Address range 0-32 } EEPROM.commit(); for (int j = 1000; j <= 1032; j = j + 4) { EEPROM.get((j - 1000), integer_variable); // Read the 32 values Serial.println(integer_variable); } //---------------------------------------------------------------------------- // Example-8 Testing that the EEPROM extent has not been exceeded, remember not to exceed address space if (EEaddress == 32) { EEaddress = 0; } //---------------------------------------------------------------------------- // Example-9 Compact method of writing and reading values from EEPROM EEaddress = 20; // Writing floatingpoint_variable = 2 * PI; EEaddress += EEPROM.put(EEaddress, floatingpoint_variable); integer_variable = 123456789; EEaddress += EEPROM.put(EEaddress, integer_variable); EEPROM.end(); EEaddress = 20; // Reading EEaddress += EEPROM.get(EEaddress, floatingpoint_variable); EEaddress += EEPROM.get(EEaddress, integer_variable); EEPROM.commit(); Serial.println(floatingpoint_variable, 7); Serial.println(integer_variable); } void loop() {} </nowiki> <br> {{template:top}} ===espnow=== [https://randomnerdtutorials.com/esp-now-one-to-many-esp8266-nodemcu/ ESP-NOW with ESP8266: Send Data to Multiple Boards (one-to-many)]<br> [https://randomnerdtutorials.com/esp-now-many-to-one-esp32/ Many to One]<br> [https://www.youtube.com/watch?v=bEKjCDDUPaU Tutorial from <i>The Workshop</i>]<br> [https://github.com/tomrusteze/esphome-esp-now ESP Now integration for ESPhome (GitHub)]<br> {{template:top}} ===I2C (Wire library)=== Wire library currently supports master mode up to approximately 450KHz.<br> Before using I2C, pins for SDA and SCL need to be set by calling '''Wire.begin(int sda, int scl)''', i.e. Wire.begin(0, 2) on ESP-01, else they default to pins 4(SDA) and 5(SCL).<br> if you have a problems with conflicting I2C addresses use an [[https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/overview I2C multiplexor]]. You can run 8 devices with the same address of a single hardware I2C interface rather than using software I2C <br> {{template:top}} ===SPI=== SPI library supports the entire Arduino SPI API including transactions, including setting phase (CPHA). Setting the Clock polarity (CPOL) is not supported, yet (SPI_MODE2 and SPI_MODE3 not working).<br> {{template:top}} ===SoftwareSerial=== An ESP8266 port of SoftwareSerial library done by Peter Lerup (@plerup) supports baud rate up to 115200 and multiples SoftwareSerial instances. See [https://github.com/plerup/espsoftwareserial developers Git] if you want to suggest an improvement or open an issue related to SoftwareSerial.<br> {{template:top}} ===ESP-specific APIs=== APIs related to deep sleep and watchdog timer are available in the ESP object, only available in Alpha version.<br> :ESP.deepSleep(microseconds, mode) will put the chip into deep sleep. mode is one of WAKE_RF_DEFAULT, WAKE_RFCAL, WAKE_NO_RFCAL, WAKE_RF_DISABLED. (GPIO16 needs to be tied to RST to wake from deepSleep.)<br> :ESP.restart() restarts the CPU. :ESP.getFreeHeap() returns the free heap size. :ESP.getChipId() returns the ESP8266 chip ID as a 32-bit integer. <br> :Several APIs may be used to get flash chip info: :ESP.getFlashChipId() returns the flash chip ID as a 32-bit integer. :ESP.getFlashChipSize() returns the flash chip size, in bytes, as seen by the SDK (may be less than actual size). :ESP.getFlashChipSpeed(void) returns the flash chip frequency, in Hz. :ESP.getCycleCount() returns the cpu instruction cycle count since start as an unsigned 32-bit. This is useful for accurate timing of very short actions like bit banging. :ESP.getVcc() may be used to measure supply voltage. ESP needs to reconfigure the ADC at startup in order for this feature to be available. Add the following line to the top of your sketch to use getVcc: ADC_MODE(ADC_VCC); ::TOUT pin has to be disconnected in this mode. ::Note that by default ADC is configured to read from TOUT pin using analogRead(A0), and ESP.getVCC() is not available. {{template:top}} ===OneWire=== Library was adapted to work with ESP8266 by including register definitions into OneWire.h Note that if you already have OneWire library in your Arduino/libraries folder, it will be used instead of the one that comes with this package.<br> {{template:top}} ===mDNS and DNS-SD responder (ESP8266mDNS library)=== Allows the sketch to respond to multicast DNS queries for domain names like "foo.local", and DNS-SD (service discovery) queries. See example for details.<br> {{template:top}} ===SSDP responder (ESP8266SSDP)=== SSDP is another service discovery protocol, supported on Windows out of the box. See attached example for reference.<br> {{template:top}} ===DNS server (DNSServer library)=== Implements a simple DNS server that can be used in both STA and AP modes. The DNS server currently supports only one domain (for all other domains it will reply with NXDOMAIN or custom status code). With it clients can open a web server running on ESP8266 using a domain name, not an IP address. See attached example for details.<br> {{template:top}} ===Servo=== This library exposes the ability to control RC (hobby) servo motors. It will support upto 24 servos on any available output pin. By default the first 12 servos will use Timer0 and currently this will not interfere with any other support. Servo counts above 12 will use Timer1 and features that use it will be effected. While many RC servo motors will accept the 3.3V IO data pin from a ESP8266, most will not be able to run off 3.3v and will require another power source that matches their specifications. Make sure to connect the grounds between the ESP8266 and the servo motor power supply.<br> {{template:top}} ===Other libraries (not included with the IDE)=== Libraries that don't rely on low-level access to AVR registers should work well. Here are a few libraries that were verified to work:<br> :arduinoWebSockets - WebSocket Server and Client compatible with ESP8266 (RFC6455) :aREST REST API handler library. :Blynk - easy IoT framework for Makers (check out the Kickstarter page). :DallasTemperature :DHT-sensor-library - Arduino library for the DHT11/DHT22 temperature and humidity sensors. Download latest v1.1.1 library and no changes are necessary. Older versions should initialize DHT as follows: DHT dht(DHTPIN, DHTTYPE, 15) :NeoPixel - Adafruit's NeoPixel library, now with support for the ESP8266 (use version 1.0.2 or higher from Arduino's library manager). :NeoPixelBus - Arduino NeoPixel library compatible with ESP8266. Use the "NeoPixelAnimator" branch for ESP8266 to get HSL color support and more. :PubSubClient MQTT library by @Imroy. :RTC - Arduino Library for Ds1307 & Ds3231 compatible with ESP8266. :Souliss, Smart Home - Framework for Smart Home based on Arduino, Android and openHAB. :ST7735 - Adafruit's ST7735 library modified to be compatible with ESP8266. Just make sure to modify the pins in the examples as they are still AVR specific. {{template:top}}
Summary:
Please note that all contributions to Notebook may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Notebook:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Home Page
C++ Reference
ESP Hardware
ESPHome
Home Assistant
Network Stuff
Tools
What links here
Related changes
Special pages
Page information