|
1 | 1 | #include <ESP8266WiFi.h>
|
2 | 2 | #include <ESP8266WiFiMesh.h>
|
3 | 3 |
|
4 |
| -unsigned int request_i = 0; |
5 |
| -unsigned int response_i = 0; |
| 4 | +String exampleMeshName("MeshNode_"); |
6 | 5 |
|
7 |
| -String manageRequest(String request); |
| 6 | +unsigned int requestNumber = 0; |
| 7 | +unsigned int responseNumber = 0; |
| 8 | + |
| 9 | +String manageRequest(const String &request, ESP8266WiFiMesh &meshInstance); |
| 10 | +transmission_status_t manageResponse(const String &response, ESP8266WiFiMesh &meshInstance); |
| 11 | +void networkFilter(int numberOfNetworks, ESP8266WiFiMesh &meshInstance); |
8 | 12 |
|
9 | 13 | /* Create the mesh node object */
|
10 |
| -ESP8266WiFiMesh mesh_node = ESP8266WiFiMesh(ESP.getChipId(), manageRequest); |
| 14 | +ESP8266WiFiMesh meshNode = ESP8266WiFiMesh(manageRequest, manageResponse, networkFilter, "ChangeThisWiFiPassword_TODO", exampleMeshName, "", true); |
11 | 15 |
|
12 | 16 | /**
|
13 |
| - Callback for when other nodes send you data |
| 17 | + Callback for when other nodes send you a request |
14 | 18 |
|
15 |
| - @request The string received from another node in the mesh |
| 19 | + @param request The request string received from another node in the mesh |
| 20 | + @param meshInstance The ESP8266WiFiMesh instance that called the function. |
16 | 21 | @returns The string to send back to the other node
|
17 | 22 | */
|
18 |
| -String manageRequest(String request) { |
| 23 | +String manageRequest(const String &request, ESP8266WiFiMesh &meshInstance) { |
19 | 24 | /* Print out received message */
|
20 |
| - Serial.print("received: "); |
| 25 | + Serial.print("Request received: "); |
21 | 26 | Serial.println(request);
|
22 | 27 |
|
23 | 28 | /* return a string to send back */
|
24 |
| - char response[60]; |
25 |
| - sprintf(response, "Hello world response #%d from Mesh_Node%d.", response_i++, ESP.getChipId()); |
26 |
| - return response; |
| 29 | + return ("Hello world response #" + String(responseNumber++) + " from " + meshInstance.getMeshName() + meshInstance.getNodeID() + "."); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + Callback used to decide which networks to connect to once a WiFi scan has been completed. |
| 34 | +
|
| 35 | + @param numberOfNetworks The number of networks found in the WiFi scan. |
| 36 | + @param meshInstance The ESP8266WiFiMesh instance that called the function. |
| 37 | +*/ |
| 38 | +void networkFilter(int numberOfNetworks, ESP8266WiFiMesh &meshInstance) { |
| 39 | + for (int i = 0; i < numberOfNetworks; ++i) { |
| 40 | + String currentSSID = WiFi.SSID(i); |
| 41 | + int meshNameIndex = currentSSID.indexOf(meshInstance.getMeshName()); |
| 42 | + |
| 43 | + /* Connect to any _suitable_ APs which contain meshInstance.getMeshName() */ |
| 44 | + if (meshNameIndex >= 0) { |
| 45 | + uint64_t targetNodeID = ESP8266WiFiMesh::stringToUint64(currentSSID.substring(meshNameIndex + meshInstance.getMeshName().length())); |
| 46 | + |
| 47 | + if (targetNodeID < ESP8266WiFiMesh::stringToUint64(meshInstance.getNodeID())) { |
| 48 | + ESP8266WiFiMesh::connectionQueue.push_back(NetworkInfo(i)); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + Callback for when you get a response from other nodes |
| 56 | +
|
| 57 | + @param response The response string received from another node in the mesh |
| 58 | + @param meshInstance The ESP8266WiFiMesh instance that called the function. |
| 59 | + @returns The status code resulting from the response, as an int |
| 60 | +*/ |
| 61 | +transmission_status_t manageResponse(const String &response, ESP8266WiFiMesh &meshInstance) { |
| 62 | + transmission_status_t statusCode = TS_TRANSMISSION_COMPLETE; |
| 63 | + |
| 64 | + /* Print out received message */ |
| 65 | + Serial.print("Request sent: "); |
| 66 | + Serial.println(meshInstance.getMessage()); |
| 67 | + Serial.print("Response received: "); |
| 68 | + Serial.println(response); |
| 69 | + |
| 70 | + // Our last request got a response, so time to create a new request. |
| 71 | + meshInstance.setMessage("Hello world request #" + String(++requestNumber) + " from " + meshInstance.getMeshName() + meshInstance.getNodeID() + "."); |
| 72 | + |
| 73 | + // (void)meshInstance; // This is useful to remove a "unused parameter" compiler warning. Does nothing else. |
| 74 | + return statusCode; |
27 | 75 | }
|
28 | 76 |
|
29 | 77 | void setup() {
|
| 78 | + // Prevents the flash memory from being worn out, see: https://github.com./esp8266/Arduino/issues/1054 . |
| 79 | + // This will however delay node WiFi start-up by about 700 ms. The delay is 900 ms if we otherwise would have stored the WiFi network we want to connect to. |
| 80 | + WiFi.persistent(false); |
| 81 | + |
30 | 82 | Serial.begin(115200);
|
31 |
| - delay(10); |
| 83 | + delay(50); // Wait for Serial. |
| 84 | + |
| 85 | + //yield(); // Use this if you don't want to wait for Serial. |
32 | 86 |
|
33 | 87 | Serial.println();
|
34 | 88 | Serial.println();
|
| 89 | + |
| 90 | + Serial.println("Note that this library can use static IP:s for the nodes to speed up connection times.\n" |
| 91 | + "Use the setStaticIP method as shown in this example to enable this.\n" |
| 92 | + "Ensure that nodes connecting to the same AP have distinct static IP:s.\n" |
| 93 | + "Also, remember to change the default mesh network password!\n\n"); |
| 94 | + |
35 | 95 | Serial.println("Setting up mesh node...");
|
36 | 96 |
|
37 | 97 | /* Initialise the mesh node */
|
38 |
| - mesh_node.begin(); |
| 98 | + meshNode.begin(); |
| 99 | + meshNode.activateAP(); // Each AP requires a separate server port. |
| 100 | + meshNode.setStaticIP(IPAddress(192, 168, 4, 22)); // Activate static IP mode to speed up connection times. |
39 | 101 | }
|
40 | 102 |
|
| 103 | +int32_t timeOfLastScan = -10000; |
41 | 104 | void loop() {
|
42 |
| - /* Accept any incoming connections */ |
43 |
| - mesh_node.acceptRequest(); |
44 |
| - |
45 |
| - /* Scan for other nodes and send them a message */ |
46 |
| - char request[60]; |
47 |
| - sprintf(request, "Hello world request #%d from Mesh_Node%d.", request_i++, ESP.getChipId()); |
48 |
| - mesh_node.attemptScan(request); |
49 |
| - delay(1000); |
| 105 | + if (millis() - timeOfLastScan > 3000 // Give other nodes some time to connect between data transfers. |
| 106 | + || (WiFi.status() != WL_CONNECTED && millis() - timeOfLastScan > 2000)) { // Scan for networks with two second intervals when not already connected. |
| 107 | + String request = "Hello world request #" + String(requestNumber) + " from " + meshNode.getMeshName() + meshNode.getNodeID() + "."; |
| 108 | + meshNode.attemptTransmission(request, false); |
| 109 | + timeOfLastScan = millis(); |
| 110 | + |
| 111 | + if (ESP8266WiFiMesh::latestTransmissionOutcomes.empty()) { |
| 112 | + Serial.println("No mesh AP found."); |
| 113 | + } else { |
| 114 | + for (TransmissionResult &transmissionResult : ESP8266WiFiMesh::latestTransmissionOutcomes) { |
| 115 | + if (transmissionResult.transmissionStatus == TS_TRANSMISSION_FAILED) { |
| 116 | + Serial.println("Transmission failed to mesh AP " + transmissionResult.SSID); |
| 117 | + } else if (transmissionResult.transmissionStatus == TS_CONNECTION_FAILED) { |
| 118 | + Serial.println("Connection failed to mesh AP " + transmissionResult.SSID); |
| 119 | + } else if (transmissionResult.transmissionStatus == TS_TRANSMISSION_COMPLETE) { |
| 120 | + // No need to do anything, transmission was successful. |
| 121 | + } else { |
| 122 | + Serial.println("Invalid transmission status for " + transmissionResult.SSID + "!"); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + Serial.println(); |
| 127 | + } else { |
| 128 | + /* Accept any incoming connections */ |
| 129 | + meshNode.acceptRequest(); |
| 130 | + } |
50 | 131 | }
|
0 commit comments