|
| 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