Skip to content

Instantly share code, notes, and snippets.

@caspar
Created October 18, 2018 21:46
Show Gist options
  • Save caspar/c24d3957cec96e786db3a66ff0c68d91 to your computer and use it in GitHub Desktop.
Save caspar/c24d3957cec96e786db3a66ff0c68d91 to your computer and use it in GitHub Desktop.
//************************************************************
// 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 <easyMesh.h>
// some gpio pin that is connected to an LED...
// on my rig, this is 5, change to the right number of your LED.
#define LED 5 // GPIO number of connected LED
#define BLINK_PERIOD 1000000 // microseconds until cycle repeat
#define BLINK_DURATION 100000 // microseconds LED is on for
#define MESH_PREFIX "veggie"
#define MESH_PASSWORD "unseelie"
#define MESH_PORT 5555
easyMesh mesh;
uint32_t sendMessageTime = 0;
void setup() {
Serial.begin(115200);
pinMode( LED, OUTPUT );
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages
mesh.init( MESH_PREFIX, MESH_PASSWORD, MESH_PORT );
mesh.setReceiveCallback( &receivedCallback );
mesh.setNewConnectionCallback( &newConnectionCallback );
randomSeed( analogRead( A0 ) );
}
void loop() {
mesh.update();
setcolor(color);
message = command+color+rate;
mesh.sendBroadcast(message);
// run the blinky
bool onFlag = false;
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 = true;
}
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 && sendMessageTime < mesh.getNodeTime() ){
String msg = "Hello from node ";
msg += mesh.getChipId();
mesh.sendBroadcast( msg );
sendMessageTime = 0;
}
}
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %d msg=%s\n", from, msg.c_str());
}
void newConnectionCallback( bool adopt ) {
Serial.printf("startHere: New Connection, adopt=%d\n", adopt);
}
//************************************************************
// 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 <easyMesh.h>
// some gpio pin that is connected to an LED...
// on my rig, this is 5, change to the right number of your LED.
#define LED 5 // GPIO number of connected LED
#define BLINK_PERIOD 1000000 // microseconds until cycle repeat
#define BLINK_DURATION 100000 // microseconds LED is on for
#define MESH_PREFIX "veggie"
#define MESH_PASSWORD "unseelie"
#define MESH_PORT 5555
easyMesh mesh;
uint32_t sendMessageTime = 0;
String state; //in message format
void setState(String &message){
}
void setup() {
Serial.begin(115200);
pinMode( LED, OUTPUT );
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages
mesh.init( MESH_PREFIX, MESH_PASSWORD, MESH_PORT );
mesh.setReceiveCallback( &receivedCallback );
mesh.setNewConnectionCallback( &newConnectionCallback );
randomSeed( analogRead( A0 ) );
}
void loop() {
mesh.update();
setcolor(color);
message = command+color+rate;
mesh.sendBroadcast(message);
// run the blinky
bool onFlag = false;
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 = true;
}
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 && sendMessageTime < mesh.getNodeTime() ){
String msg = "Hello from node ";
msg += mesh.getChipId();
mesh.sendBroadcast( msg );
sendMessageTime = 0;
}
}
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %d msg=%s\n", from, msg.c_str());
if (msg.c_str().substring(0,1).toInt() == 0){ //just setting color
analogWrite(red, msg.c_str().substring(1,5)toInt());
analogWrite(green, msg.c_str().substring(5,9)toInt());
analogWrite(blue, msg.c_str().substring(9,13)toInt());
}
else if(msg.c_str().substring(0,1).toInt() == 1){ //strobe
strobe(msg.c_str().substring(1,5).toInt());
}
else if(msg.c_str().substring(0,1).toInt() == 2){ //fade
fade(msg.c_str().substring(1,5).toInt(), msg.c_str().substring(5,9).toInt()); //fade(fadeInRate, fadeOutRate)
}
}
void newConnectionCallback( bool adopt ) {
Serial.printf("startHere: New Connection, adopt=%d\n", adopt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment