-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartMeter.ino
172 lines (144 loc) · 4.04 KB
/
SmartMeter.ino
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <avr/pgmspace.h>
#include "voltage.h"
#include "RF24.h"
#include "RF24Network.h"
#include "RF24Mesh.h"
#include <SPI.h>
#include <EEPROM.h>
#define nodeID 1
uint32_t displayTimer = 0;
RF24 radio(7, 8);
RF24Network network(radio);
RF24Mesh mesh(radio, network);
//unsigned long time = millis();
//Sets the sample rate in ms, lowest is 5 and other set functions are a multiple of 5 seconds
#define Sample_Speed 1
//Set sim speed, 1 is real time
#define Sim_Speed 1
//samp 120 and sim 600 is a good rate for quick test
//set max and min
#define Max 5
#define Min 1
int index = 0;
struct data_types {
//Effect in watt
uint16_t Effect;
//Effect in watt hours
uint16_t Effect_Hour;
//Voltage in milli volt
uint16_t Voltage;
//Amp in amp
uint16_t Ampere;
//Time in ms
uint16_t Time_Stamp;
//Smart Meter ID
uint8_t ID = nodeID;
//Safty check
uint8_t check = 29;
};
data_types emu_data;
struct data_types enc_xor(int ID, struct data_types ukrypt) {
uint16_t key_array[] = {123, 124, 125, 126, 127, 128, 129, 130, 131};
ukrypt.Effect ^= key_array[ID-1];
ukrypt.Effect_Hour ^= key_array[ID-1];
ukrypt.Voltage ^= key_array[ID-1];
ukrypt.Ampere ^= key_array[ID-1];
ukrypt.Time_Stamp ^= key_array[ID-1];
ukrypt.check ^= key_array[ID-1];
return ukrypt;
}
/*struct payload_t {
unsigned long ms;
unsigned long counter;
};*/
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//Random seed for true random
randomSeed(analogRead(0));
// Set the nodeID manually
mesh.setNodeID(nodeID);
// Connect to the mesh
//Serial.println(F("Connecting to the mesh..."));
mesh.begin();
/*Serial.print("Sample rate at: ");
Serial.println(Sample_Speed);
Serial.print("Simulator speed at: ");
Serial.println(Sim_Speed);
Serial.print("Minimum at: ");
Serial.println(Min);
Serial.print("Maximum at: ");
Serial.println(Max);
*/
}
struct data_types emulator(unsigned long time) {
float volt_temp;
float amp_temp;
float effect_temp;
if (index == 8640) {
index = 0;
}
//for calcs
data_types temp;
//for database comp
//real life time stamps not affected by sim speed
//Serial.print("Sample at: ");
//Serial.println(time / 1000);
//save time
temp.Time_Stamp = (time * Sim_Speed) / 1000;
//12 hours cycle
temp.Voltage = pgm_read_word_near(volt_array + index);
//Serial.println(temp.Voltage);
index++;
volt_temp = temp.Voltage;
//10 times bigger
//Serial.println(volt_temp/10,1);
//12 hours cycle
temp.Ampere = (((sin(((temp.Time_Stamp) * 3.14159) / 21600) * ((Max - Min) / 2)) + ((Max + Min) / 2)) * 1000);//+random(0,500);
//Serial.println(temp.Ampere);
amp_temp = temp.Ampere;
//1000 times bigger
//effects
temp.Effect = ((amp_temp * volt_temp) * 5) / 10000;
//Serial.println(temp.Effect);
temp.Effect_Hour = (temp.Effect / ((3.6) / Sample_Speed));
//Serial.println(temp.Effect_Hour);
//1000 bigger than what it really is
effect_temp = temp.Effect_Hour;
//info print
/*Serial.print("Emulated effect: ");
Serial.print(effect_temp / 1000, 3);
Serial.print(" Wh at :");
Serial.println(temp.Time_Stamp);
*/
return temp;
}
void loop() {
mesh.update();
if (displayTimer + ((Sample_Speed * 5000) / Sim_Speed) <= millis()) {
displayTimer = millis();
emu_data = emulator(displayTimer);
emu_data = enc_xor(nodeID,emu_data);
if (!mesh.write(&emu_data, 'M', sizeof(emu_data))) {
// If a write fails, check connectivity to the mesh network
if ( ! mesh.checkConnection() ) {
//refresh the network address
// Serial.println("Renewing Address");
mesh.renewAddress();
} //else {
//Serial.println("Send fail, Test OK");
//}
} //else {
//Serial.print("Send OK: "); Serial.println(displayTimer);
//}
}
/*while (network.available()) {
RF24NetworkHeader header;
payload_t payload;
network.read(header, &payload, sizeof(payload));
Serial.print("Received packet #");
Serial.print(payload.counter);
Serial.print(" at ");
Serial.println(payload.ms);
}*/
}