Skip to content

Commit 533ed3b

Browse files
committed
Merge pull request arduino#115 from bblacey/master
Created a small PingPongSensor that demonstrates node-to-node communi…
2 parents 02b8f75 + ecc0426 commit 533ed3b

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/****
2+
* Formatted logging to the Serial console.
3+
* Compiled in by setting LOGDEBUG
4+
*
5+
* 2015-05-25 Bruce Lacey V1.0
6+
*
7+
* Based upon Arduino Playground prior art and should be moved to
8+
* the MySensors library at some point as a common debug logging facility
9+
*/
10+
#ifndef MYSLog_h
11+
#define MYSLog_h
12+
13+
#define LOGDEBUG 1
14+
15+
#if defined ( LOGDEBUG )
16+
#define LOG(fmt, args... ) log( fmt, ## args );
17+
#else
18+
#define log(fmt, args... )
19+
#endif
20+
21+
void log(const char *fmt, ... ) {
22+
char buff[128];
23+
va_list args;
24+
va_start (args, fmt);
25+
vsnprintf(buff, sizeof(buff), fmt, args);
26+
va_end (args);
27+
buff[sizeof(buff)/sizeof(buff[0])-1]='\0';
28+
Serial.print(buff);
29+
}
30+
31+
void log(const __FlashStringHelper *fmt, ... ){
32+
char buf[128]; // resulting string limited to 128 chars
33+
va_list args;
34+
va_start (args, fmt);
35+
#ifdef __AVR__
36+
vsnprintf_P(buf, sizeof(buf), (const char *)fmt, args); // progmem for AVR
37+
#else
38+
vsnprintf(buf, sizeof(buf), (const char *)fmt, args); // for the rest of the world
39+
#endif
40+
va_end(args);
41+
Serial.print(buf);
42+
}
43+
44+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/***
2+
* This is a simple sketch used to demenstrate and test node-to-node MySensor's communication.
3+
* To use this sketch, assemble MySensors nodes - they need nothing more than a radio
4+
* 1. Flash each node with the same sketch, open the console and type either 0 or 1 to the respective nodes to set thei ID
5+
* 2. You only need to set the node id once, and restart the nodes
6+
* 3. To being a ping-pong test, simply type T in the console for one of the nodes.
7+
*
8+
* 2015-05-25 Bruce Lacey v1.0
9+
*/
10+
11+
#include <MySensor.h>
12+
#include <SPI.h>
13+
#include "MYSLog.h"
14+
15+
#define VSN "v1.0"
16+
17+
// Define two generic nodes with a single child
18+
#define YING 200
19+
#define YANG 201
20+
#define CHILD 1
21+
22+
// Sensor objects
23+
MySensor gw;
24+
25+
MyMessage mPing(CHILD, V_VAR1); //Ping message
26+
MyMessage mPong(CHILD, V_VAR2); //Pong message
27+
28+
void setup() {
29+
30+
Serial.begin(115200);
31+
32+
gw.begin(incomingMessage); // Node ids are written to EEPROM during sensor node configuration by entering a 0 or 1
33+
gw.present(CHILD, S_CUSTOM); //
34+
35+
gw.sendSketchInfo( nodeTypeAsCharRepresentation( gw.getNodeId() ), VSN );
36+
LOG(F("\n%sReady.\n"), nodeTypeAsCharRepresentation(gw.getNodeId()));
37+
}
38+
39+
void loop() {
40+
41+
// Interactive command and control
42+
// Entering a number from 0 or 1 will write the node 200 (YING) or 201 (YANG) to EEPROM
43+
// Entering T on either node will initiatve a ping-pong test.
44+
if (Serial.available()) {
45+
byte inChar = Serial.read();
46+
uint8_t node = gw.getNodeId();
47+
48+
// Manual Test Mode
49+
if (inChar == 'T' || inChar == 't') {
50+
LOG(F("T received - starting test...\n"));
51+
MyMessage msg = mPong;
52+
msg.sender = (node == YING ? YANG : YING);
53+
sendPingOrPongResponse( msg );
54+
}
55+
else if (inChar == '0' or inChar == '1') {
56+
byte nodeID = 200 + (inChar - '0');
57+
setNodeId(nodeID);
58+
}
59+
else {
60+
LOG("Invalid input\n");
61+
}
62+
}
63+
64+
// Process sensor messages
65+
gw.process();
66+
}
67+
68+
void incomingMessage(const MyMessage &message) {
69+
70+
LOG(F("Received %s from %s\n"), msgTypeAsCharRepresentation((mysensor_data)message.type), nodeTypeAsCharRepresentation(message.sender));
71+
72+
delay(250);
73+
sendPingOrPongResponse( message );
74+
}
75+
76+
void sendPingOrPongResponse( MyMessage msg ) {
77+
78+
MyMessage response = (msg.type == V_VAR1 ? mPong : mPing);
79+
80+
LOG(F("Sending %s to %s\n"), msgTypeAsCharRepresentation( (mysensor_data)response.type ), nodeTypeAsCharRepresentation(msg.sender));
81+
82+
// Set payload to current time in millis to ensure each message is unique
83+
response.set( millis() );
84+
response.setDestination(msg.sender);
85+
gw.send(response);
86+
}
87+
88+
void setNodeId(byte nodeID) {
89+
LOG(F("Setting node id to: %i.\n***Please restart the node for changes to take effect.\n"), nodeID);
90+
eeprom_write_byte((uint8_t*)EEPROM_NODE_ID_ADDRESS, (byte)nodeID);
91+
}
92+
93+
const char * msgTypeAsCharRepresentation( mysensor_data mType ) {
94+
return mType == V_VAR1 ? "Ping" : "Pong";
95+
}
96+
97+
const char * nodeTypeAsCharRepresentation( uint8_t node ) {
98+
return node == YING ? "Ying Node" : "Yang Node";
99+
}

0 commit comments

Comments
 (0)