-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSensor433.cpp
More file actions
152 lines (116 loc) · 3 KB
/
Sensor433.cpp
File metadata and controls
152 lines (116 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Sensor433.h"
#include "Arduino.h"
#include "RCSwitch.h"
namespace Sensor433
{
//
// Transmitter class
//
Transmitter::Transmitter(byte transmitterPin) : seq(0)
{
rc.enableTransmit(transmitterPin);
rc.setRepeatTransmit(25);
}
void Transmitter::sendWord(byte sensorId, word data)
{
unsigned long dataToSend = encode32BitsToSend(sensorId, seq, data);
// Send the data twice to reduce risk of receiver getting noise
rc.send(dataToSend, 32);
delay(1000);
rc.send(dataToSend, 32);
seq++;
if (seq > 15)
{
seq = 0;
}
}
void Transmitter::sendFloat(byte sensorId, float data)
{
word twoBytes = encodeFloatToTwoBytes(data);
sendWord(sensorId, twoBytes);
}
unsigned long Transmitter::encode32BitsToSend(byte sensorId, byte seq, word data)
{
byte checkSum = sensorId + seq + data;
unsigned long byte3 = ((0x0F & sensorId) << 4) + (0x0F & seq);
unsigned long byte2_and_byte_1 = 0xFFFF & data;
byte byte0 = 0xFF & checkSum;
unsigned long dataToSend = (byte3 << 24) + (byte2_and_byte_1 << 8) + byte0;
return dataToSend;
}
word Transmitter::encodeFloatToTwoBytes(float floatValue)
{
bool sign = false;
if (floatValue < 0)
sign=true;
int times100 = (100*fabs(floatValue));
unsigned int twoBytes = times100 & 0X7FFF;
if (sign)
twoBytes |= 1 << 15;
return twoBytes;
}
//
// Receiver class
//
Receiver::Receiver(byte receiverInterruptNumber)
{
rc.enableReceive(receiverInterruptNumber);
}
ReceivedMessage receivedMessage = { 0, 0, 0.0 };
ReceivedMessage Receiver::getData()
{
return receivedMessage;
}
unsigned long prevValue = 0;
int numIdenticalInRow = 0;
bool Receiver::hasNewData()
{
if (!rc.available())
return false;
unsigned long newValue = rc.getReceivedValue();
if (newValue == 0)
return false;
// Get the different parts of the 32-bit / 4-byte value
// that has been read over 433MHz
byte checksum = newValue & 0x000000FF;
word sensordata = (newValue >> 8) & 0x0000FFFF;
byte byte3 = (newValue >> 24) & 0x000000FF;
byte seq = byte3 & 0x0F;
byte sensorId = (byte3 & 0xF0) >> 4;
byte calculatedCheckSum = 0xFF & (sensorId + seq + sensordata);
if ((calculatedCheckSum == checksum) && (seq <= 15))
{
if (newValue == prevValue)
{
numIdenticalInRow++;
}
else
{
numIdenticalInRow = 0;
}
prevValue = newValue;
// Require at least two readings of the same value in a row
// to reduce risk of reading noise. Ignore any further duplicated
// values
if (numIdenticalInRow == 2)
{
receivedMessage.sensorId = sensorId;
receivedMessage.dataAsWord = sensordata;
receivedMessage.dataAsFloat = decodeTwoBytesToFloat(sensordata);
return true;
}
}
rc.resetAvailable();
return false;
}
float Receiver::decodeTwoBytesToFloat(word twoBytes)
{
bool sign = false;
if ((twoBytes & 0x8000) == 0x8000)
sign=true;
float fl = (twoBytes & 0x7FFF) / 100.0;
if (sign)
fl = -fl;
return fl;
}
}