Last active
February 1, 2017 12:31
-
-
Save RudyFiero/014a52d52f87f32c07d6c86ecc4271e5 to your computer and use it in GitHub Desktop.
Modified startHere.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//************************************************************ | |
// this is a simple example that uses the easyMesh library | |
// | |
// 1. blinks led once for every node on the mesh | |
// 2. blink cycle repeats every BLINK_PERIOD | |
// 3. sends a silly message to every node on the mesh at a random time betweew 1 and 5 seconds | |
// 4. prints anything it recieves to Serial.print | |
// | |
// | |
//************************************************************ | |
#include <ESP8266WiFi.h> | |
#include <painlessMesh.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <Wire.h> | |
LiquidCrystal_I2C lcd(0x27, 20, 4); | |
extern "C" { | |
#include "user_interface.h" | |
} | |
#define LED 15 // GPIO number of connected LED, ON ESP-12 IS GPIO2 | |
#define BLINK_PERIOD 3000000 // microseconds until cycle repeat | |
#define BLINK_DURATION 100000 // microseconds LED is on for | |
#define MESH_SSID "ESPmesh" | |
#define MESH_PASSWORD "123456789" | |
#define MESH_PORT 5555 | |
painlessMesh mesh; | |
bool calc_delay = false; | |
SimpleList<uint32_t> nodes; | |
uint32_t sendMessageTime = 0; | |
#include <Time.h> | |
#include <Ticker.h> | |
Ticker timeTick; //ticker for a repeated 1sec time update | |
//--------------------------------------------- | |
int newConnectionCount = 0; // used to update lcd on connection count change | |
int oldConnectionCount = 0; | |
//---------------------- serial handler ------ | |
const byte numChars = 32; | |
char receivedChars[numChars]; // an array to store the received data | |
boolean newData = false; | |
//-------------------------------------------- | |
//------------------------------------------------- | |
String whoIs="-"; // friendly name rather than MAC address | |
String Me=" "; // used for serial print | |
//-------------------------------------------------- | |
void secondTick() // update lcd even if no messages are received. | |
{ | |
lcd.setCursor(0, 3); | |
lcd.print(mesh.getNodeTime(),HEX); | |
lcd.setCursor(9, 3); | |
lcd.print("H"); | |
lcd.print(ESP.getFreeHeap()); | |
} | |
//--------------------------------------------------- | |
void clearLCDline(int row) { // clear lcd line then set cursor to 1st char position | |
lcd.setCursor(0, row); // column, row | |
lcd.print(" "); | |
lcd.setCursor(0, row); | |
} | |
//--------------------------------------------------- | |
void showConnections() { | |
newConnectionCount = (mesh.connectionCount()); | |
if (newConnectionCount != oldConnectionCount) { | |
oldConnectionCount = newConnectionCount; | |
lcd.setCursor(19, 3); | |
lcd.print(newConnectionCount + 1); | |
// int currentRSSI = WiFi.RSSI(); | |
//int32_t channel (void) | |
} | |
} | |
//============================================================= | |
void setup() { | |
Serial.begin(115200); | |
pinMode(LED, OUTPUT); | |
lcd.begin(0, 2); // In ESP8266-01, SDA=0, SCL=2 | |
lcd.backlight(); | |
lcd.home(); // At column=0, row=0 | |
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on | |
mesh.setDebugMsgTypes(ERROR | DEBUG); // set before init() so that you can see startup messages | |
mesh.init(MESH_SSID, MESH_PASSWORD, MESH_PORT); | |
mesh.onReceive(&receivedCallback); | |
mesh.onNewConnection(&newConnectionCallback); | |
mesh.onChangedConnections(&changedConnectionCallback); | |
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback); | |
mesh.onNodeDelayReceived(&delayReceivedCallback); | |
randomSeed(analogRead(A0)); | |
WiFi.setOutputPower(0); // this sets wifi to lowest power | |
timeTick.attach(1, secondTick); | |
txtFrom(mesh.getNodeId()); // get short name | |
Me=whoIs; | |
Serial.print("wifi_station_get_hostname: ------ "); | |
Serial.println(wifi_station_get_hostname()); | |
system_phy_set_max_tpw(5); // power setting 82=max | |
} | |
//============================================================= | |
void loop() { | |
bool error; | |
mesh.update(); | |
// run the blinky | |
bool onFlag = true; | |
uint32_t cycleTime = mesh.getNodeTime() % BLINK_PERIOD; | |
for (uint8_t i = 0; i < (mesh.connectionCount() + 1); i++) { | |
uint32_t onTime = BLINK_DURATION * i * 2; | |
if (cycleTime > onTime && cycleTime < onTime + BLINK_DURATION) | |
onFlag = false; | |
} | |
digitalWrite(LED, onFlag); | |
// get next random time for send message | |
if (sendMessageTime == 0) { | |
sendMessageTime = mesh.getNodeTime() + random(1000000, 5000000); | |
} | |
// if the time is ripe, send everyone a message! | |
if (sendMessageTime != 0 && | |
(int) sendMessageTime - (int) mesh.getNodeTime() < 0) { // Cast to int in case of time rollover | |
String msg = "Hi from "; | |
// msg += mesh.getNodeId(); | |
txtFrom(mesh.getNodeId()); // get short name | |
msg += whoIs; | |
error = mesh.sendBroadcast(msg + " Heap" + String(ESP.getFreeHeap())); | |
sendMessageTime = 0; | |
if (calc_delay) { | |
SimpleList<uint32_t>::iterator node = nodes.begin(); | |
while (node != nodes.end()) { | |
mesh.startDelayMeas(*node); | |
node++; | |
} | |
calc_delay = false; | |
} | |
} | |
showConnections(); // nodes connected to, updates when changed | |
recvWithEndMarker(); // Serial input until \n | |
showNewData(); // Serial.print and broadcast | |
} | |
//============================================================= | |
void receivedCallback(uint32_t from, String &msg) { | |
// Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str()); | |
txtFrom(from); | |
Serial.print("I am "); | |
Serial.print(Me); | |
Serial.printf(" : Received from %s msg=%s", whoIs.c_str(), msg.c_str()); | |
Serial.print(" "); | |
Serial.print(mesh.getNodeTime(),HEX); | |
Serial.print(" My Heap "); | |
Serial.print(ESP.getFreeHeap()); | |
Serial.print(" RSSI= "); | |
Serial.print(WiFi.RSSI()); | |
Serial.print(" Channel "); | |
Serial.println(WiFi.channel()); | |
clearLCDline(2); | |
lcd.print("Ch"); | |
lcd.print(WiFi.channel()); | |
lcd.print(" rssi "); | |
lcd.print(WiFi.RSSI()); | |
// lcd.print(" rssi "); | |
// lcd.print(WiFi.RSSI()); | |
clearLCDline(0); | |
lcd.print("Got from "); | |
txtFrom(from); // find out who from is. fills whoIs string | |
lcd.print(whoIs); | |
lcd.print(" "); | |
clearLCDline(1); | |
lcd.print(msg.c_str()); | |
} | |
void newConnectionCallback(uint32_t nodeId) { | |
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); | |
} | |
void changedConnectionCallback() { | |
Serial.printf("Changed connections %s\n", mesh.subConnectionJson().c_str()); | |
nodes = mesh.getNodeList(); | |
Serial.printf("Num nodes: %d\n", nodes.size()); | |
Serial.printf("Connection list:"); | |
SimpleList<uint32_t>::iterator node = nodes.begin(); | |
while (node != nodes.end()) { | |
Serial.printf(" %u", *node); | |
node++; | |
} | |
Serial.println(); | |
calc_delay = true; | |
} | |
void nodeTimeAdjustedCallback(int32_t offset) { | |
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset); | |
} | |
void delayReceivedCallback(uint32_t from, int32_t delay) { | |
Serial.printf("Delay to node %u is %d us\n", from, delay); | |
} | |
//============================================================= | |
void recvWithEndMarker() { // serial input fill buffer | |
static byte ndx = 0; | |
char endMarker = '\n'; | |
char rc; | |
while (Serial.available() > 0 && newData == false) { | |
rc = Serial.read(); | |
if (rc != endMarker) { | |
receivedChars[ndx] = rc; | |
ndx++; | |
if (ndx >= numChars) { | |
ndx = numChars - 1; | |
} | |
} | |
else { | |
receivedChars[ndx] = '\0'; // terminate the string | |
ndx = 0; | |
newData = true; | |
} | |
} | |
} | |
//------------------------------------------------- | |
void showNewData() { // | |
if (newData == true) { | |
Serial.print("Serial Send ... "); | |
Serial.println(receivedChars); | |
newData = false; | |
String str(receivedChars); // broadcast serial | |
mesh.sendBroadcast(str); | |
} | |
} | |
//--------------------------------------------------- | |
void txtFrom(uint32_t var) { // MAC address to last number of IP address typically assigned on my system | |
switch (var) { | |
case 888274678: // 34F1:FEF6 | |
whoIs = "04"; | |
break; | |
case 2131077732: // 7F05:AA64 | |
whoIs = "02"; | |
break; | |
case 886613302: // 34D8:A536 | |
whoIs = "03"; | |
break; | |
case 888453468: // 34F4:B95C | |
whoIs = "07"; | |
break; | |
case 2131077688: // 7F05:AA38 | |
whoIs = "06"; | |
break; | |
case 886288527: // 34D3:B08F | |
whoIs = "10"; | |
break; | |
case 888275717: // 34F2:0305 | |
whoIs = "11"; | |
break; | |
default: | |
whoIs = var; | |
break; | |
} | |
} | |
//---------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment