MySensors Library & Examples  2.3.2
PingPongSensor.ino
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2019 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  *******************************
20  *
21  * This is a simple sketch used to demonstrate and test node-to-node MySensors communication.
22  * To use this sketch, assemble MySensors nodes - they need nothing more than a radio
23  * 1. Flash each node with the same sketch, open the console and type either 0 or 1 to the respective nodes to set their ID
24  * 2. You only need to set the node id once, and restart the nodes
25  * 3. To being a ping-pong test, simply type T in the console for one of the nodes.
26  *
27  * 2015-05-25 Bruce Lacey v1.0
28  */
29 
30 // Enable debug prints to serial monitor
31 #define MY_DEBUG
32 
33 // Enable and select radio type attached
34 #define MY_RADIO_RF24
35 //#define MY_RADIO_NRF5_ESB
36 //#define MY_RADIO_RFM69
37 //#define MY_RADIO_RFM95
38 
39 #include <MySensors.h>
40 #include "MYSLog.h"
41 
42 #define VSN "v1.0"
43 
44 // Define two generic nodes with a single child
45 #define YING 200
46 #define YANG 201
47 #define CHILD 1
48 
49 MyMessage mPing(CHILD, V_VAR1); //Ping message
50 MyMessage mPong(CHILD, V_VAR2); //Pong message
51 
52 void setup()
53 {
54 }
55 
57 {
58  present(CHILD, S_CUSTOM); //
59 
60  sendSketchInfo( nodeTypeAsCharRepresentation( getNodeId() ), VSN );
61  LOG(F("\n%sReady.\n"), nodeTypeAsCharRepresentation(getNodeId()));
62 }
63 
64 void loop()
65 {
66 
67  // Interactive command and control
68  // Entering a number from 0 or 1 will write the node 200 (YING) or 201 (YANG) to EEPROM
69  // Entering T on either node will initiate a ping-pong test.
70  if (Serial.available()) {
71  byte inChar = Serial.read();
72  uint8_t node = getNodeId();
73 
74  // Manual Test Mode
75  if (inChar == 'T' || inChar == 't') {
76  LOG(F("T received - starting test...\n"));
77  MyMessage msg = mPong;
78  msg.sender = (node == YING ? YANG : YING);
79  sendPingOrPongResponse( msg );
80  } else if (inChar == '0' or inChar == '1') {
81  byte nodeID = 200 + (inChar - '0');
82  setNodeId(nodeID);
83  } else {
84  LOG("Invalid input\n");
85  }
86  }
87 }
88 
89 void receive(const MyMessage &message)
90 {
91 
92  LOG(F("Received %s from %s\n"), msgTypeAsCharRepresentation((mysensors_data_t)message.getType()),
93  nodeTypeAsCharRepresentation(message.sender));
94 
95  delay(250);
96  sendPingOrPongResponse( message );
97 }
98 
99 void sendPingOrPongResponse( MyMessage msg )
100 {
101 
102  MyMessage response = (msg.getType() == V_VAR1 ? mPong : mPing);
103 
104  LOG(F("Sending %s to %s\n"), msgTypeAsCharRepresentation( (mysensors_data_t)response.getType() ),
105  nodeTypeAsCharRepresentation(msg.sender));
106 
107  // Set payload to current time in millis to ensure each message is unique
108  response.set( (uint32_t)millis() );
109  response.setDestination(msg.sender);
110  send(response);
111 }
112 
113 void setNodeId(byte nodeID)
114 {
115  LOG(F("Setting node id to: %i.\n***Please restart the node for changes to take effect.\n"), nodeID);
116  hwWriteConfig(EEPROM_NODE_ID_ADDRESS, (byte)nodeID);
117 }
118 
119 const char * msgTypeAsCharRepresentation( mysensors_data_t mType )
120 {
121  return mType == V_VAR1 ? "Ping" : "Pong";
122 }
123 
124 const char * nodeTypeAsCharRepresentation( uint8_t node )
125 {
126  return node == YING ? "Ying Node" : "Yang Node";
127 }
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
API declaration for MySensors.
MyMessage & setDestination(const uint8_t destinationId)
Set final destination node id for this message.
int available()
This function does nothing.
bool send(MyMessage &msg, const bool requestEcho=false)
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
uint8_t getNodeId(void)
uint8_t sender
8 bit - Id of sender node (origin)
Definition: MyMessage.h:624
void setup()
Called after node initialises but before main loop.
void receive(const MyMessage &message)
Callback for incoming messages.
void presentation()
Node presentation.
uint8_t getType(void) const
Get message type.
#define EEPROM_NODE_ID_ADDRESS
Address node ID.
int read()
Reads 1 key pressed from the keyboard.
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
void loop()
Main loop.