I'm using the arduino IDE to program an ESP8266 and I imported a library called "Hello Mesh"
In the main sketch I have this.
and in the main loop of the program it will do the below.
void loop will forever send a formatted string to the library below.
The ESP module boots up and it starts the wifi access point using a prefix MESH_NODE and append the chip id to it. as displayed in the main sketch void Setup.
After the AP started it starts to loop sending a message over and over using attemptScan
which then scans for a list of access point with MESH_NODE prefix infront of the AP name and iterates through them by sending the message to all of them on port 4011, but before connecting it will change its mode to a client, send the message then revert back to access point mode. (I think its doing this because it cannot be an AP and client at the same time.)
So this means all nodes will communicate with all nodes.
Now this is where I'm losing it. After it sent the message it send it to exchangeInfo wich the n sends it back to the call back on the main sketch called manageRequest ?
If I'm correct then where does the buzz word "MESH" come in, the above does not look like a true mesh to me ?
A mesh in my mind is when you have the following.
4 nodes in a row over 4 km, each node only has a range of 1Km, so Node 1 cannot reach node Node 4 directly, but because of a mesh topology Node1 can communicate with node 4 with the help of Node 2 and 3
In the main sketch I have this.
PHP:
String manageRequest(String request)
{
/* Print out received message */
Serial.print("received: ");
Serial.println(request);
/* return a string to send back */
//char response[60];
//sprintf(response, "MY NAME IS %d another value", ESP.getChipId());
//return response;
}
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.println("Setting up mesh node...");
/* Initialise the mesh node */
mesh_node.begin();
}
and in the main loop of the program it will do the below.
PHP:
void loop()
{
/* Accept any incoming connections */
mesh_node.acceptRequest();
/* Scan for other nodes and send them a message */
char request[60];
sprintf(request, "MY NAME IS %d", ESP.getChipId());
mesh_node.attemptScan(request);
delay(500);
}
void loop will forever send a formatted string to the library below.
PHP:
/*
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.
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
*/
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=include]#include[/URL] <Arduino.h>
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=include]#include[/URL] <ESP8266WiFi.h>
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=include]#include[/URL] <WiFiClient.h>
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=include]#include[/URL] <WiFiServer.h>
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=include]#include[/URL] "ESP8266WiFiMesh.h"
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=define]#define[/URL] SSID_PREFIX "Mesh_Node"
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=define]#define[/URL] SERVER_IP_ADDR "192.168.4.1"
[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=define]#define[/URL] SERVER_PORT 4011
ESP8266WiFiMesh::ESP8266WiFiMesh(uint32_t chip_id, std::function<String(String)> handler)
: _server(SERVER_PORT)
{
_chip_id = chip_id;
_ssid = String( String( SSID_PREFIX ) + String( _chip_id ) );
_ssid_prefix = String( SSID_PREFIX );
_handler = handler;
}
void ESP8266WiFiMesh::begin()
{
WiFi.mode(WIFI_AP_STA);
WiFi.softAP( _ssid.c_str());
_server.begin();
}
/**
* Wait for a WiFiClient to connect
*
* [MENTION=192203]ret[/MENTION]urns: True if the client is ready, false otherwise.
*
*/
bool ESP8266WiFiMesh::waitForClient(WiFiClient curr_client, int max_wait)
{
int wait = max_wait;
while(curr_client.connected() && !curr_client.available() && wait--)
delay(3);
/* Return false if the client isn't ready to communicate */
if (WiFi.status() == WL_DISCONNECTED || !curr_client.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.
*
* [MENTION=76598]target[/MENTION]_ssid The name of the AP the other node has set up.
* [MENTION=255574]Mess[/MENTION]age The string to send to the node.
* [MENTION=192203]ret[/MENTION]urns: True if the exchange was a succes, false otherwise.
*
*/
bool ESP8266WiFiMesh::exchangeInfo(String message, WiFiClient curr_client)
{
curr_client.println( message.c_str() );
if (!waitForClient(curr_client, 1000))
return false;
String response = curr_client.readStringUntil('\r');
curr_client.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.
*
* [MENTION=76598]target[/MENTION]_ssid The name of the AP the other node has set up.
* [MENTION=255574]Mess[/MENTION]age The string to send to the node.
*
*/
void ESP8266WiFiMesh::connectToNode(String target_ssid, String message)
{
WiFiClient curr_client;
WiFi.begin( target_ssid.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 (!curr_client.connect(SERVER_IP_ADDR, SERVER_PORT))
return;
if (!exchangeInfo(message, curr_client))
return;
curr_client.stop();
WiFi.disconnect();
}
void ESP8266WiFiMesh::attemptScan(String message)
{
/* Scan for APs */
int n = WiFi.scanNetworks();
for (int i = 0; i < n; ++i) {
String current_ssid = WiFi.SSID(i);
int index = current_ssid.indexOf( _ssid_prefix );
uint32_t target_chip_id = (current_ssid.substring(index + _ssid_prefix.length())).toInt();
/* Connect to any _suitable_ APs which contain _ssid_prefix */
if (index >= 0 && (target_chip_id < _chip_id)) {
WiFi.mode(WIFI_STA);
delay(100);
connectToNode(current_ssid, message);
WiFi.mode(WIFI_AP_STA);
delay(100);
}
}
}
void ESP8266WiFiMesh::acceptRequest()
{
while (true) {
_client = _server.available();
if (!_client)
break;
if (!waitForClient(_client, 1500)) {
continue;
}
/* Read in request and pass it to the supplied handler */
String request = _client.readStringUntil('\r');
_client.readStringUntil('\n');
String response = _handler(request);
/* Send the response back to the client */
if (_client.connected())
_client.println(response);
}
}
The ESP module boots up and it starts the wifi access point using a prefix MESH_NODE and append the chip id to it. as displayed in the main sketch void Setup.
After the AP started it starts to loop sending a message over and over using attemptScan
which then scans for a list of access point with MESH_NODE prefix infront of the AP name and iterates through them by sending the message to all of them on port 4011, but before connecting it will change its mode to a client, send the message then revert back to access point mode. (I think its doing this because it cannot be an AP and client at the same time.)
So this means all nodes will communicate with all nodes.
Now this is where I'm losing it. After it sent the message it send it to exchangeInfo wich the n sends it back to the call back on the main sketch called manageRequest ?
If I'm correct then where does the buzz word "MESH" come in, the above does not look like a true mesh to me ?
A mesh in my mind is when you have the following.
4 nodes in a row over 4 km, each node only has a range of 1Km, so Node 1 cannot reach node Node 4 directly, but because of a mesh topology Node1 can communicate with node 4 with the help of Node 2 and 3