MySensors Library & Examples  2.3.2-36-g2e00bf6
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-2020 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 //#define MY_PJON
39 
40 #include <MySensors.h>
41 #include "MYSLog.h"
42 
43 #define VSN "v1.0"
44 
45 // Define two generic nodes with a single child
46 #define YING 200
47 #define YANG 201
48 #define CHILD 1
49 
50 MyMessage mPing(CHILD, V_VAR1); //Ping message
51 MyMessage mPong(CHILD, V_VAR2); //Pong message
52 
53 void setup()
54 {
55 }
56 
58 {
59  present(CHILD, S_CUSTOM); //
60 
61  sendSketchInfo( nodeTypeAsCharRepresentation( getNodeId() ), VSN );
62  LOG(F("\n%sReady.\n"), nodeTypeAsCharRepresentation(getNodeId()));
63 }
64 
65 void loop()
66 {
67 
68  // Interactive command and control
69  // Entering a number from 0 or 1 will write the node 200 (YING) or 201 (YANG) to EEPROM
70  // Entering T on either node will initiate a ping-pong test.
71  if (Serial.available()) {
72  byte inChar = Serial.read();
73  uint8_t node = getNodeId();
74 
75  // Manual Test Mode
76  if (inChar == 'T' || inChar == 't') {
77  LOG(F("T received - starting test...\n"));
78  MyMessage msg = mPong;
79  msg.sender = (node == YING ? YANG : YING);
80  sendPingOrPongResponse( msg );
81  } else if (inChar == '0' or inChar == '1') {
82  byte nodeID = 200 + (inChar - '0');
83  setNodeId(nodeID);
84  } else {
85  LOG("Invalid input\n");
86  }
87  }
88 }
89 
90 void receive(const MyMessage &message)
91 {
92 
93  LOG(F("Received %s from %s\n"), msgTypeAsCharRepresentation((mysensors_data_t)message.getType()),
94  nodeTypeAsCharRepresentation(message.sender));
95 
96  delay(250);
97  sendPingOrPongResponse( message );
98 }
99 
100 void sendPingOrPongResponse( MyMessage msg )
101 {
102 
103  MyMessage response = (msg.getType() == V_VAR1 ? mPong : mPing);
104 
105  LOG(F("Sending %s to %s\n"), msgTypeAsCharRepresentation( (mysensors_data_t)response.getType() ),
106  nodeTypeAsCharRepresentation(msg.sender));
107 
108  // Set payload to current time in millis to ensure each message is unique
109  response.set( (uint32_t)millis() );
110  response.setDestination(msg.sender);
111  send(response);
112 }
113 
114 void setNodeId(byte nodeID)
115 {
116  LOG(F("Setting node id to: %i.\n***Please restart the node for changes to take effect.\n"), nodeID);
117  hwWriteConfig(EEPROM_NODE_ID_ADDRESS, (byte)nodeID);
118 }
119 
120 const char * msgTypeAsCharRepresentation( mysensors_data_t mType )
121 {
122  return mType == V_VAR1 ? "Ping" : "Pong";
123 }
124 
125 const char * nodeTypeAsCharRepresentation( uint8_t node )
126 {
127  return node == YING ? "Ying Node" : "Yang Node";
128 }
MyMessage::sender
uint8_t sender
8 bit - Id of sender node (origin)
Definition: MyMessage.h:624
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
receive
void receive(const MyMessage &message)
Callback for incoming messages.
Definition: PingPongSensor.ino:90
loop
void loop()
Main loop.
Definition: PingPongSensor.ino:65
presentation
void presentation()
Node presentation.
Definition: PingPongSensor.ino:57
MyMessage::set
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
send
bool send(MyMessage &msg, const bool requestEcho=false)
MyMessage::getType
uint8_t getType(void) const
Get message type.
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
getNodeId
uint8_t getNodeId(void)
setup
void setup()
Called after node initialises but before main loop.
Definition: PingPongSensor.ino:53
MyMessage::setDestination
MyMessage & setDestination(const uint8_t destinationId)
Set final destination node id for this message.
EEPROM_NODE_ID_ADDRESS
#define EEPROM_NODE_ID_ADDRESS
Address node ID.
Definition: MyEepromAddresses.h:56
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289