-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeMesh3.cpp
189 lines (166 loc) · 4.94 KB
/
HomeMesh3.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stddef.h>
#include <Arduino.h>
#ifdef ARDUINO_ARCH_AVR
#include <avr/wdt.h>
#include "printf.h"
#endif
#include "avr_sleep.h"
#include "RF24Network.h"
#include "RF24.h"
#include "nRF24L01.h"
#include "RF24Mesh.h"
#include "SPI.h"
#include "DHT/dht.h"
#include "TimeLib.h"
//# define PSTR(s) (__extension__({static char __c[] PROGMEM = (s); &__c[0];}))
#define LED_PIN A5 // the status LED pin
#define BATTERY_PIN A4 // the battery monitor pin
#define MCU_REF_VOLTAGE 3.3 // the reference voltage of the microcontroller
#define VOLTAGE_DIVIDER 3 // the voltage divider
#if NODE_ID != 0
#define SCHEDULE_PIN 14 // the relay pin
#if NODE_ID >= 10 && NODE_ID < 20
#if NODE_ID >= 15
#define DHT11_PIN 8 // small sensor node 10..19 (RFTempSens kapcsolás)
#else
#define DHT22_PIN 8 // small sensor node 10..19 (RFTempSens kapcsolás)
#endif
#define ONEWIRE_PIN 2
#define ONEWIRE_MAX 5
#define NRF24_CE_PIN 9
#define NRF24_CS_PIN 10
#else
#define DHT22_PIN 15 // define if you want to read DHT sensor
#define NRF24_CE_PIN 9
#define NRF24_CS_PIN 10
#endif
#endif
#if NODE_ID == 0
// Atmega 328
//#define NRF24_CE_PIN 9
//#define NRF24_CS_PIN 10
// ESP8266
#ifdef ESP8266
#define NRF24_CE_PIN 5
#define NRF24_CS_PIN 15
#else
// Mega 2560
#define NRF24_CE_PIN 48
#define NRF24_CS_PIN SS
#endif
#endif
/***** Configure the chosen CE,CS pins *****/
RF24 radio(NRF24_CE_PIN, NRF24_CS_PIN);
RF24Network network(radio);
RF24Mesh mesh(radio,network);
#include "Components/itask.h" // base interface for tasks
#include "Components/time_sync.h" // all node use time syncronization
#if NODE_ID == 0
#include "Components/master.h"
#ifdef ESP8266
#include "Components/esp8266_ntp.h"
#include "Components/esp8266_web.h"
#else
#include "EtherShield/etherShield.h"
#include "Components/ethernet.h"
//#include "Components/wifi.h"
#endif
#else
#ifdef ONEWIRE_PIN
#include "OneWire.h"
#endif
#include "Components/sensors.h"
#ifdef SCHEDULE_PIN
#include "Components/schedule.h"
#endif
#endif
uint8_t taskCount = 0;
ITask* tasks[10];
void addTask(ITask *task) {
if (taskCount < sizeof(tasks))
{
tasks[taskCount++] = task;
printf_P(PSTR("Added task: %s\r\n"), task->name());
}
}
void setup() {
#ifdef ARDUINO_ARCH_AVR
sleep_setup(); // if you want to use sleep, or use the optiboot watchdog
#endif
//wdt_disable();
Serial.begin(115200);
#ifdef ARDUINO_ARCH_AVR
printf_begin();
#endif
printf_P(PSTR("HomeMesh 1.0\r\n"));
printf_P(PSTR("(c) koverg70 %s %s\r\n"), __DATE__, __TIME__);
/*
pinMode(LED_PIN, OUTPUT);
for (int i=1; i<3; ++i) {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
*/
printf_P(PSTR("NodeID: %d, CE pin: %d, CS pin: %d\r\n"), NODE_ID, NRF24_CE_PIN, NRF24_CS_PIN);
// a very simple RF24 radio test
delay(500);
mesh.setNodeID(NODE_ID);
printf_P(PSTR("NodeID: %d, CE pin: %d, CS pin: %d\r\n"), NODE_ID, NRF24_CE_PIN, NRF24_CS_PIN);
mesh.begin();
printf_P(PSTR("NodeID: %d, CE pin: %d, CS pin: %d\r\n"), NODE_ID, NRF24_CE_PIN, NRF24_CS_PIN);
// --- now we initialize the tasks the are regularily called with new messages and to process information ----
addTask(new TimeSync(60000, 0)); // sync frequency and target node to require time from
#if NODE_ID == 0
Master *master = new Master();
addTask(master); // TODO: where to store received data
#ifdef ESP8266
addTask(new ESP8266Ntp());
addTask(new ESP8266Web(master->getSensors()));
#else
// addTask(new Wifi(master->getSensors()));
addTask(new Ethernet(master->getSensors()));
#endif
#else
addTask(new Sensors(0)); // the target node to send sensor data to
#endif
#ifdef SCHEDULE_PIN
addTask(new Schedule(SCHEDULE_PIN)); // sync frequency and target node to require time from
#endif
// ------------------------------------------------------------------------------------------------------------
for (int i = 0; i < taskCount; ++i) {
tasks[i]->begin();
printf_P(PSTR("Task started: %s\r\n"), tasks[i]->name());
}
}
void loop(void) {
// Call mesh.update to keep the network updated
mesh.update();
#if NODE_ID == 0
// In addition, keep the 'DHCP service' running on the master node so addresses will
// be assigned to the sensor nodes
mesh.DHCP();
#endif
// Check for incoming data from the sensors
while (network.available()) {
RF24NetworkHeader header;
uint8_t payload[40];
//network.peek(header);
boolean ok = network.read(header, &payload, sizeof(payload));
if (ok) {
for (int i = 0; i < taskCount; ++i) {
ok |= tasks[i]->receiveMessage(header, payload);
}
if (!ok) {
printf_P(PSTR("Received unknown message: %s\r\n"), header.toString());
}
}
else {
printf_P(PSTR("Error reading network."));
}
}
for (int i = 0; i < taskCount; ++i) {
tasks[i]->updateState();
}
}