1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-15 00:02:49 +03:00
Files
esp8266/libraries/ESP8266WiFiMesh/src/CompatibilityLayer.cpp
aerlon 7d5997dad1 Update of mesh network library. (#4718)
* Make mesh network actually usable. Make mesh network use static IP during initial connection to speed up connection time. Add separate handlers for requests and responses. Add network password. Provide more detailed code example. Add optional verbose mode. Improve comments. Add readme file.

* Fix compiler warnings. Fix code style of HelloMesh.ino to avoid upsetting Travis.

* Remove stray spaces.

* Make mesh network WiFi password settable via the ESP8266WiFiMesh constructor. Make use of static IP optional by moving static IP initialization code to setStaticIP method. Increase scanning interval from one to two seconds in the HelloMesh.ino example to increase chances of successful connections. Update comments. Update README.rst.

* Increase specificity in the conditions of the waitForClientTransmission method (renamed from waitForClient) to avoid issues related to #4626 , #4728 and #4754 in the future.

* Improve most parts of the library to achieve better performance and greatly increase flexibility.

Changes:
* Make WiFi-connection related variables static to allow for the use of multiple ESP8266WiFiMesh instances on a single node (useful e.g. when communicating with several different mesh networks).
* Make it possible to choose AP port, which is helpful when using multiple ESP8266WiFiMesh AP:s on a single node.
* Add user-customizable network filter.
* Make activation of own AP optional for each mesh node.
* Add ways to change mesh network name and node id for existing ESP8266WiFiMesh instances.
* Add verboseModePrint method to clean up the code.
* Add reactivation of static IP after successful data transfers to speed up re-connection attempts.
* Add empty_IP constant which can be used to check if static IP is disabled for a ESP8266WiFiMesh instance.
* Remove the WiFiClient _client class variable in ESP8266WiFiMesh since there is no need to save _client in the class instance.
* Add transmission status as a return value from attemptTransmission.
* Pass calling ESP8266WiFiMesh instance pointer to callback functions to allow for greater range of actions in callbacks.
* Make transmission message a class variable to allow it to be stored in the class and accessed from callbacks.
* Add getters for mesh name and node id to ESP8266WiFiMesh.
* Add getter and setter for networkFilter to ESP8266WiFiMesh.
* Increase range of available node_id:s by changing the type to String and adding functions to convert between String and uint64_t using a customizable radix between 2 and 36.
* Make it possible to connect to several nodes during each attemptTransmission call.
* Add static connection_queue and latest_transmission_outcomes vectors to the ESP8266WiFiMesh class, a NetworkInfo class and a TransmissionResult class to aid in bookkeeping when connecting to several AP:s during one attemptTransmission call.
* Make wifi_channel and BSSID optional when connecting to an AP (though excluding them will slow down the connection process).
* Add optional scan and static ip optimizations available in Arduino core for ESP8266 version 2.4.2.
* Add functions to check lwIP version in order to enable WiFi optimizations only available with lwIP2.
* Add concluding_disconnect, initial_disconnect and no_scan options to the attemptTransmission method.
* Update documentation.

* Improve README.rst formatting.

* Further improve README.rst.

* Even further improve README.rst.

* Make source code comments Doxygen compatible. Improve README file and change its file format to .md.

* Add temporary compatibility layer to ensure backwards compatibility with the old mesh network library API until the next major core release (2.5.0).

* Polish documentation slightly.

* Add scan_all_wifi_channels option to attemptTransmission method.

* - Add getter and setter for the WiFi channel of a ESP8266WiFiMesh instance.
- Separate methods for changing mesh name and node id from AP control methods.
- Add methods getAPController and isAPController to better handle situations when multiple ESP8266WiFiMesh instances take turns to be in control of the AP.
- Create separate UtilityMethods.cpp file for utility methods.
- Improve code efficiency and robustness, e.g. by passing arguments by reference instead of by value for non-POD types and employing typedefs.
- Update README.md.

* Make the code more stylish.

* Update README.md with the new ESP8266WiFiMesh constructor documentation.

* Make attemptScan method in CompatibilityLayer use reference as argument.

* Make it possible to use const String as argument to attemptScan.

* - Make code use camelCase instead of snake_case.
- Improve documentation.

* Rename Uint64ToString to uint64ToString and StringToUint64 to stringToUint64, since they are methods.
2018-07-31 22:46:20 -04:00

181 lines
4.7 KiB
C++

/*
Old version of ESP8266WiFiMesh.cpp - Mesh network node
Sets up a Mesh Node which acts as a router, creating a Mesh Network with other nodes. All information
is passed in both directions, but it is up to the user what the data sent is and how it is dealt with.
Copyright (c) 2015 Julian Fell. All rights reserved.
Updated 2018 by Anders Löfgren.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/********************************************************************************************
* NOTE!
*
* All method signatures in this file are deprecated and will be removed in core version 2.5.0.
* If you are still using these methods, please consider migrating to the new API shown in
* the ESP8266WiFiMesh.h source file.
*
* TODO: delete this file.
********************************************************************************************/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include "ESP8266WiFiMesh.h"
#define SSID_PREFIX "Mesh_Node"
#define SERVER_IP_ADDR "192.168.4.1"
#define SERVER_PORT 4011
// DEPRECATED!
ESP8266WiFiMesh::ESP8266WiFiMesh(uint32_t chipID, ESP8266WiFiMesh::compatibilityLayerHandlerType handler)
: _server(SERVER_PORT)
{
_chipID = chipID;
_SSID = String( String( SSID_PREFIX ) + String( _chipID ) );
_ssidPrefix = String( SSID_PREFIX );
_handler = handler;
}
/**
* Wait for a WiFiClient to connect
*
* @returns: True if the client is ready, false otherwise.
*
*/
// DEPRECATED!
bool ESP8266WiFiMesh::waitForClient(WiFiClient &currClient, int maxWait)
{
int wait = maxWait;
while(currClient.connected() && !currClient.available() && wait--)
delay(3);
/* Return false if the client isn't ready to communicate */
if (WiFi.status() == WL_DISCONNECTED || !currClient.connected())
return false;
return true;
}
/**
* Send the supplied message then read back the other node's response
* and pass that to the user-supplied handler.
*
* @message The string to send to the node.
* @returns: True if the exchange was a succes, false otherwise.
*
*/
// DEPRECATED!
bool ESP8266WiFiMesh::exchangeInfo(const char *message, WiFiClient &currClient)
{
currClient.println( message );
if (!waitForClient(currClient, 1000))
return false;
String response = currClient.readStringUntil('\r');
currClient.readStringUntil('\n');
if (response.length() <= 2)
return false;
/* Pass data to user callback */
_handler(response);
return true;
}
/**
* Connect to the AP at ssid, send them a message then disconnect.
*
* @targetSSID The name of the AP the other node has set up.
* @message The string to send to the node.
*
*/
// DEPRECATED!
void ESP8266WiFiMesh::connectToNode(const String &targetSSID, const char *message)
{
WiFiClient currClient;
WiFi.begin( targetSSID.c_str() );
int wait = 1500;
while((WiFi.status() == WL_DISCONNECTED) && wait--)
delay(3);
/* If the connection timed out */
if (WiFi.status() != 3)
return;
/* Connect to the node's server */
if (!currClient.connect(SERVER_IP_ADDR, SERVER_PORT))
return;
if (!exchangeInfo(message, currClient))
return;
currClient.stop();
WiFi.disconnect();
}
// DEPRECATED!
void ESP8266WiFiMesh::attemptScanKernel(const char *message)
{
/* Scan for APs */
int n = WiFi.scanNetworks();
for (int i = 0; i < n; ++i) {
String currentSSID = WiFi.SSID(i);
int index = currentSSID.indexOf( _ssidPrefix );
uint32_t targetChipID = (currentSSID.substring(index + _ssidPrefix.length())).toInt();
/* Connect to any _suitable_ APs which contain _ssidPrefix */
if (index >= 0 && (targetChipID < _chipID)) {
WiFi.mode(WIFI_STA);
delay(100);
connectToNode(currentSSID, message);
WiFi.mode(WIFI_AP_STA);
delay(100);
}
}
}
// DEPRECATED!
void ESP8266WiFiMesh::attemptScan(const String &message)
{
attemptScanKernel(message.c_str());
}
// DEPRECATED!
void ESP8266WiFiMesh::attemptScan(char *message)
{
attemptScanKernel(message);
}
// DEPRECATED!
template<size_t Size>
void ESP8266WiFiMesh::attemptScan(char (&message)[Size])
{
attemptScanKernel(message);
}