MYSENSORS_GatewayClient_WORK_V3.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 <henrik.ekblad@mysensors.org>
9
 * Copyright (C) 2013-2015 Sensnology AB
10
 * Full contributor list: https://github.com/mysensors/Arduino/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
 * REVISION HISTORY
22
 * Version 1.0 - Henrik Ekblad
23
 * 
24
 * DESCRIPTION
25
 * The W5100 MQTT gateway sends radio network (or locally attached sensors) data to your MQTT broker.
26
 * The node also listens to MY_MQTT_TOPIC_PREFIX and sends out those messages to the radio network
27
 *
28
 * LED purposes:
29
 * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
30
 * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
31
 * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
32
 * - ERR (red) - fast blink on error during transmission error or recieve crc error  
33
 * 
34
 * See http://www.mysensors.org/build/esp8266_gateway for wiring instructions.
35
 * nRF24L01+  ESP8266
36
 * VCC        VCC
37
 * CE         GPIO4          
38
 * CSN/CS     GPIO15
39
 * SCK        GPIO14
40
 * MISO       GPIO12
41
 * MOSI       GPIO13
42
 *            
43
 * Not all ESP8266 modules have all pins available on their external interface.
44
 * This code has been tested on an ESP-12 module.
45
 * The ESP8266 requires a certain pin configuration to download code, and another one to run code:
46
 * - Connect REST (reset) via 10K pullup resistor to VCC, and via switch to GND ('reset switch')
47
 * - Connect GPIO15 via 10K pulldown resistor to GND
48
 * - Connect CH_PD via 10K resistor to VCC
49
 * - Connect GPIO2 via 10K resistor to VCC
50
 * - Connect GPIO0 via 10K resistor to VCC, and via switch to GND ('bootload switch')
51
 * 
52
  * Inclusion mode button:
53
 * - Connect GPIO5 via switch to GND ('inclusion switch')
54
 * 
55
 * Hardware SHA204 signing is currently not supported!
56
 *
57
 * Make sure to fill in your ssid and WiFi password below for ssid & pass.
58
 */
59
60
#include <EEPROM.h>
61
#include <SPI.h>
62
63
// Enable debug prints to serial monitor
64
#define MY_DEBUG 
65
66
// Enables and select radio type (if attached)
67
//#define MY_RADIO_NRF24
68
//#define MY_RADIO_RFM69
69
70
#define MY_GATEWAY_MQTT_CLIENT
71
72
// Set this nodes subscripe and publish topic prefix
73
#define MY_MQTT_PUBLISH_TOPIC_PREFIX "mygateway1-out"
74
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in"
75
76
// Set MQTT client id
77
#define MY_MQTT_CLIENT_ID "mysensors-1"
78
79
// W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
80
//#define MY_W5100_SPI_EN 4  
81
82
// Enable Soft SPI for NRF radio (note different radio wiring is required)
83
// The W5100 ethernet module seems to have a hard time co-operate with 
84
// radio on the same spi bus.
85
#if !defined(MY_W5100_SPI_EN)
86
  #define MY_SOFTSPI
87
  #define MY_SOFT_SPI_SCK_PIN 14
88
  #define MY_SOFT_SPI_MISO_PIN 16
89
  #define MY_SOFT_SPI_MOSI_PIN 15
90
#endif  
91
92
// When W5100 is connected we have to move CE/CSN pins for NRF radio
93
#define MY_RF24_CE_PIN 5
94
#define MY_RF24_CS_PIN 6
95
96
// Enable these if your MQTT broker requires usenrame/password
97
//#define MY_MQTT_USER "username"
98
//#define MY_MQTT_PASSWORD "password"
99
100
// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
101
#define MY_IP_ADDRESS 192,168,111,99
102
103
// If using static ip you need to define Gateway and Subnet address as well
104
#define MY_IP_GATEWAY_ADDRESS 192,168,111,1
105
#define MY_IP_SUBNET_ADDRESS 255,255,255,0
106
107
108
// MQTT broker ip address.  
109
#define MY_CONTROLLER_IP_ADDRESS 192,168,111,97
110
111
// The MQTT broker port to to open 
112
#define MY_PORT 1883
113
114
#define ANZAHL_KOMPONENTEM 10 //SUMME ALLER Konmponenten, die angschlossen sin (z.B. 8xRelais+2xTemp=10
115
116
#define RELAY_1 22  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
117
#define NUMBER_OF_RELAYS 1 // Total number of attached relays
118
#define RELAY_ON 1  // GPIO value to write to turn on attached relay
119
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
120
121
#define KTYPE 30  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
122
#define NUMBER_OF_KTYPE 2 // Total number of attached relays
123
uint8_t doPin = 31; // SO pin of MAX6675
124
uint8_t clPin = 30; // CL pin of MAX6675
125
float temperature = 0.0; // Temperature output variable
126
float lastTemperature;
127
float mittelwert_ktype[NUMBER_OF_KTYPE];
128
const float delta_Ktype = 0.0; //in °C
129
float last_value_D[NUMBER_OF_KTYPE];
130
const int ktype_TPFilter[NUMBER_OF_KTYPE+1]= {2,2}; //1-10 bedeutet 10 bis 100% Einfluss
131
const double ktype_offset[NUMBER_OF_KTYPE+1]={2,2}; // SensorOFFSET !!!!!!!!!!!!!
132
boolean metric = true;
133
boolean complete_loop = 1; // wenn 1, dann einmal komplett durchlaufen
134
unsigned long delta_time = 3600000;
135
unsigned long next_full_loop = 0;
136
137
//#############################################################################################
138
//#############################################################################################
139
//I/O-Kennung: hier wird die Funktion aller verwendbaren IO´s mit einer Kennziffer festgelegt
140
//dabei haben alle IO´s die Standardfunktionen plus spez. Sonderfunktionen
141
//     Standardfunktionen sind:
142
//     '0' =andere Nutzg; '1' =dig_in; '2' =dig_out; '3' =1wire '4' =DHTxx; '5' =U_Schall
143
144
#include <stddef.h>
145
#include <stdint.h>
146
147
typedef struct s_iomodus {
148
  uint8_t sensorType;
149
  uint8_t set_req;
150
  String description;
151
} iomodus_t;
152
153
iomodus_t iomodus[] = { 
154
/*
155
{presentation, set/req, description}
156
*/
157
{0,0,"free"},
158
{0,0,"free"}, 
159
{0,0,"free"}, //D2 :      Std-fkt; '15' = IR_Rx??  '6' =ImpCount; '31' =tft;   <<user IO-Shield20<<
160
{0,0,"free"}, //D3 :      Std-fkt; '7' = 433_Rx??  '6' =ImpCount; '31' =tft;   <<user IO-Shield20<<
161
{0,0,"free"}, //D4 :      Std-fkt; '7' = 433_Tx??  '30' =lcd;     '31' =tft;   <<user IO-Shield20<<
162
{0,0,"free"}, //D5 :      Std-fkt;                 '30' =lcd;     '31' =tft;   <<user IO-Shield20<<
163
{0,0,"free"}, //D6 :      Std-fkt; '9' = buzzer    '30' =lcd;     '31' =tft;   <<user IO-Shield20<<
164
{0,0,"free"}, //D7 :      Std-fkt;                 '30' =lcd;     '31' =tft;   <<user IO-Shield20<<
165
{0,0,"free"}, //D8 :      Std-fkt;                 '30' =lcd;     '31' =tft;   <<user IO-Shield20<<
166
{0,0,"free"}, //D9 :      Std-fkt; '16' = IR_Tx??  '30' =lcd;     '31' =tft;   <<user IO-Shield20<<
167
{0,0,"free"}, //D10 :     Std-fkt; '20' = W5100 SS-Pin;
168
{0,0,"free"}, //D11 :     Std-fkt;
169
{0,0,"free"}, //D12 :     Std-fkt;
170
{S_BINARY, V_STATUS, "test switch"}, //D13 :     Std-fkt;
171
{0,0,"free"}, //D14/TX3 : Std-fkt; '0' =ESP8266;   '12' = rfid3;                <<user IO-Shield-Plus<<
172
{0,0,"free"}, //D15/RX3 : Std-fkt; '0' =ESP8266;   '12' = rfid3;                <<user IO-Shield-Plus<<
173
{0,0,"free"}, //D16/TX2 : Std-fkt; '0' =ESP8266;   '12' = rfid2;                <<user IO-Shield-Plus<<
174
{0,0,"free"}, //D17/RX2 : Std-fkt; '0' =ESP8266;   '12' = rfid2;                <<user IO-Shield-Plus<<
175
{0,0,"free"}, //D18/TX1 : Std-fkt; '6' =ImpCount;  '21' =CC3000                 <<user IO-Shield-Plus<<
176
{0,0,"free"}, //D19/RX1 : Std-fkt; '6' =ImpCount;                               <<user IO-Shield-Plus<<
177
{0,0,"free"}, //D20/SDA : Std-fkt; '6' =ImpCount;  '8' =I2C;                    <<user IO-Shield-Plus<<
178
{0,0,"free"}, //D21/SCL : Std-fkt; '6' =ImpCount;  '8' =I2C;                    <<user IO-Shield-Plus<<
179
{0,0,"free"}, //D22 :     Std-fkt; '12' = rfid3-oeffner;                        <<user IO-Shield-Plus<<
180
{0,0,"free"}, //D23 :     Std-fkt; '12' = rfid2-oeffner;                        <<user IO-Shield-Plus<<
181
{0,0,"free"}, //D24 :     Std-fkt;                                              <<user IO-Shield-Plus<<
182
{0,0,"free"}, //D25 :     Std-fkt;                                              <<user IO-Shield-Plus<<
183
{0,0,"free"}, //D26 :     Std-fkt;                                              <<user IO-Shield-Plus<<
184
{0,0,"free"}, //D27 :     Std-fkt;                                              <<user IO-Shield-Plus<<
185
{0,0,"free"}, //D28 :     Std-fkt;                                              <<user IO-Shield-Plus<<
186
{0,0,"free"}, //D29 :     Std-fkt;                                              <<user IO-Shield-Plus<<
187
{0,0,"free"}, //D30 :   {0,0,"free"}, // '0' =andere Nutzg; CL
188
{0,0,"free"}, //D31 :   {0,0,"free"}, // '0' =andere Nutzg; SO
189
{0,0,"free"}, //D32 :     Std-fkt; NEU SOLL CS1 Read Sensor Nr 1 Ktype
190
{0,0,"free"}, //D33 :     Std-fkt; NEU SOLL CS2 Read Sensor Nr 2 Ktype
191
{0,0,"free"}, //D34
192
{0,0,"free"}, //D35
193
{0,0,"free"}, //D36
194
{0,0,"free"}, //D37
195
{0,0,"free"}, //D38
196
{0,0,"free"}, //D39
197
{0,0,"free"}, //D40
198
{0,0,"free"}, //D41
199
{0,0,"free"}, //D42
200
{0,0,"free"}, //D43
201
{0,0,"free"}, //D44
202
{0,0,"free"}, //D45
203
{0,0,"free"}, //D46
204
{0,0,"free"}, //D47
205
{0,0,"free"}, //D48
206
{0,0,"free"}, //D49
207
{0,0,"free"}, //D50 MISO      '20' =W5100;  '21' =CC3000;   ICSP-Stecker
208
{0,0,"free"}, //D51 MOSI      '20' =W5100;  '21' =CC3000;   ICSP-Stecker
209
{0,0,"free"}, //D52 SCK       '20' =W5100;  '21' =CC3000;   ICSP-Stecker
210
{0,0,"free"}, //D53SS                      '21' =CC3000;
211
{0,0,"free"}, //D54 A0 :  Std-fkt; '10' =analog; '11' =NTC; '31' =tft; '30' =lcd; ser IO-Shield-20<<
212
{0,0,"free"}, //D55 A1 :  Std-fkt; '10' =analog; '11' =NTC; '31' =tft;         <<user IO-Shield-20<<
213
{0,0,"free"}, //D56 A2 :  Std-fkt; '10' =analog; '11' =NTC; '31' =tft;         <<user IO-Shield-20<<
214
{0,0,"free"}, //D57 A3 :  Std-fkt; '10' =analog; '11' =NTC; '31' =tft;         <<user IO-Shield-20<<
215
{0,0,"free"}, //D58 A4 :  Std-fkt; '10' =analog; '11' =NTC; '31' =tft;         <<user IO-Shield-20<<
216
{0,0,"free"}, //D59 A5 :  Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-20<<
217
{0,0,"free"}, //D60 A6 :
218
{0,0,"free"}, //D61 A7 :
219
{0,0,"free"}, //D62 A8 :  Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
220
{0,0,"free"}, //D63 A9 :  Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
221
{0,0,"free"}, //D64 A10 : Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
222
{0,0,"free"}, //D65 A11 : Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
223
{0,0,"free"}, //D66 A12 : Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
224
{0,0,"free"}, //D67 A13 : Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
225
{0,0,"free"}, //D68 A14 : Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
226
{0,0,"free"}, //D69 A15 : Std-fkt; '10' =analog; '11' =NTC;                    <<user IO-Shield-Plus<<
227
{0,0,"free"}, // '0' =andere Nutzg;    '8' =I2C;                               <<user IO-Shield-Plus<<
228
{0,0,"free"}, // '0' =andere Nutzg;    '8' =I2C;                               <<user IO-Shield-Plus<<
229
{0,0,"free"}, // '0' =andere Nutzg;    '8' =I2C;                               <<user IO-Shield-Plus<<
230
{0,0,"free"}, // '0' =andere Nutzg;    '8' =I2C;                               <<user IO-Shield-Plus<<
231
{0,0,"free"}, // '0' =andere Nutzg;                                            <<user IO-Shield-Plus<<
232
{0,0,"free"}, // '0' =andere Nutzg;                                            <<user IO-Shield-Plus<<
233
{0,0,"free"}, // '0' =andere Nutzg;                                            <<user IO-Shield-Plus<<
234
{0,0,"free"}, // '0' =andere Nutzg;                                            <<user IO-Shield-Plus<<
235
{0,0,"free"}, // '0' =andere Nutzg;                                            <<user IO-Shield-Plus<<
236
{0,0,"free"} // '0' =andere Nutzg;                                            <<user IO-Shield-Plus<<
237
};
238
const size_t iomodus_count = sizeof(iomodus)/sizeof(*iomodus);
239
//#############################################################################################
240
241
// Flash leds on rx/tx/err
242
#define MY_LEDS_BLINKING_FEATURE
243
// Set blinking period
244
#define MY_DEFAULT_LED_BLINK_PERIOD 300
245
246
// Enable inclusion mode
247
#define MY_INCLUSION_MODE_FEATURE
248
// Enable Inclusion mode button on gateway
249
#define MY_INCLUSION_BUTTON_FEATURE
250
// Set inclusion mode duration (in seconds)
251
#define MY_INCLUSION_MODE_DURATION 60 
252
// Digital pin used for inclusion mode button
253
#define MY_INCLUSION_MODE_BUTTON_PIN  3 
254
255
#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
256
#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
257
#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
258
259
260
#include <MAX31855.h>
261
#include <Ethernet.h>
262
#include <MySensor.h>
263
264
#include <stdlib.h>
265
#include <stdio.h>
266
#include <string.h>
267
268
void setup()  
269
{ 
270
/*  for (int relay=1, pin=RELAY_1; relay<=NUMBER_OF_RELAYS;relay++, pin++) {
271
    // Then set relay pins in output mode
272
    pinMode(pin, OUTPUT);   
273
    // Set relay to last known state (using eeprom storage) 
274
    digitalWrite(pin, loadState(relay)?RELAY_ON:RELAY_OFF);
275
  }
276
/*
277
  for (int ktype=1, pin=RELAY_1; ktype<=NUMBER_OF_RELAYS;ktype++, pin++) {
278
    // Then set relay pins in output mode
279
    pinMode(pin, OUTPUT);   
280
    // Set relay to last known state (using eeprom storage) 
281
    digitalWrite(pin, loadState(ktype)?RELAY_ON:RELAY_OFF);
282
  }
283
  */
284
}
285
286
void presentation()  
287
{   
288
  // Send the sketch version information to the gateway and Controller
289
  sendSketchInfo("Relay+Max31855", "1.0");
290
291
//all Ports
292
for (int i = 0; i<iomodus_count; i++){
293
  //present(uint8_t childSensorId, uint8_t sensorType, const char *description, bool enableAck)
294
  Serial.println(iomodus.sensorType);
295
  present(i, iomodus.sensorType, iomodus.description, true);
296
}
297
298
}
299
300
void loop() 
301
{}