Skip to content

Commit 6f05da4

Browse files
jtfelligrr
authored andcommitted
add ESP8266WiFiMesh library
1 parent b745111 commit 6f05da4

File tree

5 files changed

+319
-0
lines changed

5 files changed

+319
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <ESP8266WiFi.h>
2+
#include <ESP8266WiFiMesh.h>
3+
4+
/* Create the mesh node object */
5+
ESP8266WiFiMesh mesh_node = ESP8266WiFiMesh(ESP.getChipId(), manageRequest);
6+
7+
/**
8+
* Callback for when other nodes send you data
9+
*
10+
* @request The string received from another node in the mesh
11+
* @returns The string to send back to the other node
12+
*/
13+
String manageRequest(String request)
14+
{
15+
/* Print out received message */
16+
Serial.print("received: ");
17+
Serial.println(request);
18+
19+
/* return a string to send back */
20+
return String("Hello world response.");
21+
}
22+
23+
void setup()
24+
{
25+
Serial.begin(115200);
26+
delay(10);
27+
28+
Serial.println();
29+
Serial.println();
30+
Serial.println("Setting up mesh node...");
31+
32+
/* Initialise the mesh node */
33+
mesh_node.begin();
34+
}
35+
36+
void loop()
37+
{
38+
/* Accept any incoming connections */
39+
mesh_node.acceptRequest();
40+
41+
/* Scan for other nodes and send them a message */
42+
mesh_node.attemptScan("Hello world request.");
43+
delay(1000);
44+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#######################################
2+
# Syntax Coloring Map For ESP8266WiFiMesh
3+
#######################################
4+
5+
#######################################
6+
# Library (KEYWORD3)
7+
#######################################
8+
9+
ESP8266WiFiMesh KEYWORD3
10+
11+
#######################################
12+
# Datatypes (KEYWORD1)
13+
#######################################
14+
15+
ESP8266WiFiMesh KEYWORD1
16+
17+
#######################################
18+
# Methods and Functions (KEYWORD2)
19+
#######################################
20+
21+
begin KEYWORD2
22+
attemptScan KEYWORD2
23+
acceptRequest KEYWORD2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=ESP8266WiFiMesh
2+
version=1.0
3+
author=Julian Fell
4+
maintainer=
5+
sentence=Mesh network library
6+
paragraph=The library sets up a Mesh Node which acts as a router, creating a Mesh Network with other nodes.
7+
category=Communication
8+
url=
9+
architectures=esp8266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
ESP8266WiFiMesh.cpp - Mesh network node
3+
Sets up a Mesh Node which acts as a router, creating a Mesh Network with other nodes. All information
4+
is passed in both directions, but it is up to the user what the data sent is and how it is dealt with.
5+
6+
Copyright (c) 2015 Julian Fell. All rights reserved.
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation; either
11+
version 2.1 of the License, or (at your option) any later version.
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include <Arduino.h>
22+
#include <ESP8266WiFi.h>
23+
#include <WiFiClient.h>
24+
#include <WiFiServer.h>
25+
26+
#include "ESP8266WiFiMesh.h"
27+
28+
#define SSID_PREFIX "Mesh_Node"
29+
#define SERVER_IP_ADDR "192.168.4.1"
30+
#define SERVER_PORT 4011
31+
32+
ESP8266WiFiMesh::ESP8266WiFiMesh(uint32_t chip_id, std::function<String(String)> handler)
33+
: _server(SERVER_PORT)
34+
{
35+
_chip_id = chip_id;
36+
_ssid = String( String( SSID_PREFIX ) + String( _chip_id ) );
37+
_ssid_prefix = String( SSID_PREFIX );
38+
_handler = handler;
39+
}
40+
41+
void ESP8266WiFiMesh::begin()
42+
{
43+
WiFi.mode(WIFI_AP_STA);
44+
WiFi.softAP( _ssid.c_str() );
45+
_server.begin();
46+
}
47+
48+
/**
49+
* Wait for a WiFiClient to connect
50+
*
51+
* @returns: True if the client is ready, false otherwise.
52+
*
53+
*/
54+
bool ESP8266WiFiMesh::waitForClient(WiFiClient curr_client, int max_wait)
55+
{
56+
int wait = max_wait;
57+
while(curr_client.connected() && !curr_client.available() && wait--)
58+
delay(3);
59+
60+
/* Return false if the client isn't ready to communicate */
61+
if (WiFi.status() == WL_DISCONNECTED || !curr_client.connected())
62+
return false;
63+
64+
return true;
65+
}
66+
67+
/**
68+
* Send the supplied message then read back the other node's response
69+
* and pass that to the user-supplied handler.
70+
*
71+
* @target_ssid The name of the AP the other node has set up.
72+
* @message The string to send to the node.
73+
* @returns: True if the exchange was a succes, false otherwise.
74+
*
75+
*/
76+
bool ESP8266WiFiMesh::exchangeInfo(String message, WiFiClient curr_client)
77+
{
78+
curr_client.println( message.c_str() );
79+
80+
if (!waitForClient(curr_client, 1000))
81+
return false;
82+
83+
String response = curr_client.readStringUntil('\r');
84+
curr_client.readStringUntil('\n');
85+
86+
if (response.length() <= 2)
87+
return false;
88+
89+
/* Pass data to user callback */
90+
_handler(response);
91+
return true;
92+
}
93+
94+
/**
95+
* Connect to the AP at ssid, send them a message then disconnect.
96+
*
97+
* @target_ssid The name of the AP the other node has set up.
98+
* @message The string to send to the node.
99+
*
100+
*/
101+
void WiFiMesh::connectToNode(String target_ssid, String message)
102+
{
103+
WiFiClient curr_client;
104+
WiFi.begin( target_ssid.c_str() );
105+
106+
int wait = 1500;
107+
while((WiFi.status() == WL_DISCONNECTED) && wait--)
108+
delay(3);
109+
110+
/* If the connection timed out */
111+
if (WiFi.status() != 3)
112+
return;
113+
114+
/* Connect to the node's server */
115+
if (!curr_client.connect(SERVER_IP_ADDR, SERVER_PORT))
116+
return;
117+
118+
if (!exchangeInfo(message, curr_client))
119+
return;
120+
121+
curr_client.stop();
122+
WiFi.disconnect();
123+
}
124+
125+
void ESP8266WiFiMesh::attemptScan(String message)
126+
{
127+
/* Scan for APs */
128+
int n = WiFi.scanNetworks();
129+
130+
for (int i = 0; i < n; ++i) {
131+
String current_ssid = WiFi.SSID(i);
132+
int index = current_ssid.indexOf( _ssid_prefix );
133+
uint32_t target_chip_id = (current_ssid.substring(index + _ssid_prefix.length())).toInt();
134+
135+
/* Connect to any _suitable_ APs which contain _ssid_prefix */
136+
if (index >= 0 && (target_chip_id < _chip_id)) {
137+
138+
WiFi.mode(WIFI_STA);
139+
delay(100);
140+
connectToNode(current_ssid, message);
141+
WiFi.mode(WIFI_AP_STA);
142+
delay(100);
143+
}
144+
}
145+
}
146+
147+
void ESP8266WiFiMesh::acceptRequest()
148+
{
149+
while (true) {
150+
_client = _server.available();
151+
if (!_client)
152+
break;
153+
154+
if (!waitForClient(_client, 1500)) {
155+
continue;
156+
}
157+
158+
/* Read in request and pass it to the supplied handler */
159+
String request = _client.readStringUntil('\r');
160+
_client.readStringUntil('\n');
161+
162+
String response = _handler(request);
163+
164+
/* Send the response back to the client */
165+
if (_client.connected())
166+
_client.println(response);
167+
}
168+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
ESP8266WiFiMesh.h - Mesh network node
3+
Sets up a Mesh Node which acts as a router, creating a Mesh Network with other nodes. All information
4+
is passed in both directions, but it is up to the user what the data sent is and how it is dealt with.
5+
6+
Copyright (c) 2015 Julian Fell. All rights reserved.
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation; either
11+
version 2.1 of the License, or (at your option) any later version.
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifndef __WIFIMESH_H__
22+
#define __WIFIMESH_H__
23+
24+
#include <WiFiClient.h>
25+
#include <WiFiServer.h>
26+
#include <functional>
27+
28+
class ESP8266WiFiMesh {
29+
30+
private:
31+
String _ssid;
32+
String _ssid_prefix;
33+
uint32_t _chip_id;
34+
35+
std::function<String(String)> _handler;
36+
37+
WiFiServer _server;
38+
WiFiClient _client;
39+
40+
void connectToNode(String target_ssid, String message);
41+
bool exchangeInfo(String message, WiFiClient curr_client);
42+
bool waitForClient(WiFiClient curr_client, int max_wait);
43+
44+
public:
45+
46+
/**
47+
* WiFiMesh Constructor method. Creates a WiFi Mesh Node, ready to be initialised.
48+
*
49+
* @chip_id A unique identifier number for the node.
50+
* @handler The callback handler for dealing with received messages. Takes a string as an argument which
51+
* is the string received from another node and returns the string to send back.
52+
*
53+
*/
54+
ESP8266WiFiMesh(uint32_t chip_id, std::function<String(String)> handler);
55+
56+
/**
57+
* Initialises the node.
58+
*/
59+
void begin();
60+
61+
/**
62+
* Scan for other nodes, and exchange the chosen message with any that are found.
63+
*
64+
* @message The message to send to all other nodes.
65+
*
66+
*/
67+
void attemptScan(String message);
68+
69+
/**
70+
* If any clients are connected, accept their requests and call the hander function for each one.
71+
*/
72+
void acceptRequest();
73+
};
74+
75+
#endif

0 commit comments

Comments
 (0)