MySensors Library & Examples  2.3.2-36-g2e00bf6
SensebenderGatewaySerial.ino
1 
39 #define SKETCH_VERSION "0.2"
40 // Enable debug prints to serial monitor
41 #define MY_DEBUG
42 
43 // Enable and select radio type attached
44 #define MY_RADIO_RF24
45 //#define MY_RADIO_NRF5_ESB
46 //#define MY_RADIO_RFM69
47 //#define MY_RADIO_RFM95
48 //#define MY_PJON
49 
50 // Set LOW transmit power level as default, if you have an amplified NRF-module and
51 // power your radio separately with a good regulator you can turn up PA level.
52 #define MY_RF24_PA_LEVEL RF24_PA_HIGH
53 
54 // Enable serial gateway
55 #define MY_GATEWAY_SERIAL
56 
57 // Define a lower baud rate for Arduinos running on 8 MHz (Arduino Pro Mini 3.3V & Sensebender)
58 #if F_CPU == 8000000L
59 #define MY_BAUD_RATE 38400
60 #endif
61 
62 // Enable inclusion mode
63 #define MY_INCLUSION_MODE_FEATURE
64 // Enable Inclusion mode button on gateway
65 #define MY_INCLUSION_BUTTON_FEATURE
66 
67 // Inverses behavior of inclusion button (if using external pullup)
68 //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
69 
70 // Set inclusion mode duration (in seconds)
71 #define MY_INCLUSION_MODE_DURATION 60
72 // Digital pin used for inclusion mode button
73 //#define MY_INCLUSION_MODE_BUTTON_PIN 3
74 
75 // Set blinking period
76 #define MY_DEFAULT_LED_BLINK_PERIOD 300
77 
78 // Inverses the behavior of leds
79 //#define MY_WITH_LEDS_BLINKING_INVERSE
80 
81 // Flash leds on rx/tx/err
82 // Uncomment to override default HW configurations
83 //#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
84 //#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
85 //#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED
86 
87 #include <MySensors.h>
88 #include <SD.h>
89 #include <drivers/ATSHA204/ATSHA204.cpp>
90 
91 Sd2Card card;
92 
93 #define EEPROM_VERIFICATION_ADDRESS 0x01
94 
95 static uint8_t num_of_leds = 5;
96 static uint8_t leds[] = {LED_BLUE, LED_RED, LED_GREEN, LED_YELLOW, LED_ORANGE};
97 
98 void setup()
99 {
100  // Setup locally attached sensors
101 }
102 
104 {
105  // Present locally attached sensors
106 }
107 
108 void loop()
109 {
110  // Send locally attached sensor data here
111 }
112 
113 
114 void preHwInit()
115 {
116 
117  pinMode(MY_SWC1, INPUT_PULLUP);
118  pinMode(MY_SWC2, INPUT_PULLUP);
119  if (digitalRead(MY_SWC1) && digitalRead(MY_SWC2)) {
120  return;
121  }
122 
123  uint8_t tests = 0;
124 
125  for (int i=0; i< num_of_leds; i++) {
126  pinMode(leds[i], OUTPUT);
127  }
128  if (digitalRead(MY_SWC1)) {
129  uint8_t led_state = 0;
130  while (!Serial) {
131  digitalWrite(LED_BLUE, led_state);
132  led_state ^= 0x01;
133  delay(500);
134  } // Wait for USB to be connected, before spewing out data.
135  }
136  digitalWrite(LED_BLUE, LOW);
137  if (Serial) {
138  Serial.println("Sensebender GateWay test routine");
139  Serial.print("MySensors core version : ");
140  Serial.println(MYSENSORS_LIBRARY_VERSION);
141  Serial.print("GateWay sketch version : ");
142  Serial.println(SKETCH_VERSION);
143  Serial.println("----------------------------------");
144  Serial.println();
145  }
146  if (testSha204()) {
147  digitalWrite(LED_GREEN, HIGH);
148  tests++;
149  }
150  if (testSDCard()) {
151  digitalWrite(LED_YELLOW, HIGH);
152  tests++;
153  }
154 
155  if (testEEProm()) {
156  digitalWrite(LED_ORANGE, HIGH);
157  tests++;
158  }
159  if (testAnalog()) {
160  digitalWrite(LED_BLUE, HIGH);
161  tests++;
162  }
163  if (tests == 4) {
164  while(1) {
165  for (int i=0; i<num_of_leds; i++) {
166  digitalWrite(leds[i], HIGH);
167  delay(200);
168  digitalWrite(leds[i], LOW);
169  }
170  }
171  } else {
172  while (1) {
173  digitalWrite(LED_RED, HIGH);
174  delay(200);
175  digitalWrite(LED_RED, LOW);
176  delay(200);
177  }
178  }
179 
180 }
181 
182 bool testSha204()
183 {
184  uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
185  uint8_t ret_code;
186  if (Serial) {
187  Serial.print("- > SHA204 ");
188  }
189  atsha204_init(MY_SIGNING_ATSHA204_PIN);
190  ret_code = atsha204_wakeup(rx_buffer);
191 
192  if (ret_code == SHA204_SUCCESS) {
193  ret_code = atsha204_getSerialNumber(rx_buffer);
194  if (ret_code != SHA204_SUCCESS) {
195  if (Serial) {
196  Serial.println(F("Failed to obtain device serial number. Response: "));
197  }
198  Serial.println(ret_code, HEX);
199  } else {
200  if (Serial) {
201  Serial.print(F("Ok (serial : "));
202  for (int i=0; i<9; i++) {
203  if (rx_buffer[i] < 0x10) {
204  Serial.print('0'); // Because Serial.print does not 0-pad HEX
205  }
206  Serial.print(rx_buffer[i], HEX);
207  }
208  Serial.println(")");
209  }
210  return true;
211  }
212  } else {
213  if (Serial) {
214  Serial.println(F("Failed to wakeup SHA204"));
215  }
216  }
217  return false;
218 }
219 
220 bool testSDCard()
221 {
222  if (Serial) {
223  Serial.print("- > SD CARD ");
224  }
225  if (!card.init(SPI_HALF_SPEED, MY_SDCARD_CS)) {
226  if (Serial) {
227  Serial.println("SD CARD did not initialize!");
228  }
229  } else {
230  if (Serial) {
231  Serial.print("SD Card initialized correct! - ");
232  Serial.print("type detected : ");
233  switch(card.type()) {
234  case SD_CARD_TYPE_SD1:
235  Serial.println("SD1");
236  break;
237  case SD_CARD_TYPE_SD2:
238  Serial.println("SD2");
239  break;
240  case SD_CARD_TYPE_SDHC:
241  Serial.println("SDHC");
242  break;
243  default:
244  Serial.println("Unknown");
245  }
246  }
247  return true;
248  }
249  return false;
250 }
251 
252 bool testEEProm()
253 {
254  uint8_t eeprom_d1, eeprom_d2;
255  SerialUSB.print(" -> EEPROM ");
256  eeprom_d1 = hwReadConfig(EEPROM_VERIFICATION_ADDRESS);
257  delay(500);
258  eeprom_d1 = ~eeprom_d1; // invert the bits
259  hwWriteConfig(EEPROM_VERIFICATION_ADDRESS, eeprom_d1);
260  delay(500);
261  eeprom_d2 = hwReadConfig(EEPROM_VERIFICATION_ADDRESS);
262  if (eeprom_d1 == eeprom_d2) {
263  SerialUSB.println("PASSED");
264  hwWriteConfig(EEPROM_VERIFICATION_ADDRESS, ~eeprom_d1);
265  return true;
266  }
267  SerialUSB.println("FAILED!");
268  return false;
269 }
270 
271 bool testAnalog()
272 {
273  int bat_detect = analogRead(MY_BAT_DETECT);
274  Serial.print("-> analog : ");
275  Serial.print(bat_detect);
276  if (bat_detect < 400 || bat_detect > 650) {
277  Serial.println(" Failed");
278  return false;
279  }
280  Serial.println(" Passed");
281  return true;
282 }
SHA204_SUCCESS
#define SHA204_SUCCESS
Function succeeded.
Definition: sha204_lib_return_codes.h:27
MYSENSORS_LIBRARY_VERSION
#define MYSENSORS_LIBRARY_VERSION
pre-release versioning
Definition: Version.h:57
HIGH
#define HIGH
Definition: bcm2835.h:572
loop
void loop()
Main loop.
Definition: SensebenderGatewaySerial.ino:108
LOW
#define LOW
Definition: bcm2835.h:574
presentation
void presentation()
Node presentation.
Definition: SensebenderGatewaySerial.ino:103
setup
void setup()
Called after node initialises but before main loop.
Definition: SensebenderGatewaySerial.ino:98
MySensors.h
API declaration for MySensors.
MY_SIGNING_ATSHA204_PIN
#define MY_SIGNING_ATSHA204_PIN
A3 - pin where ATSHA204 is attached.
Definition: SecureActuator.ino:69
preHwInit
void preHwInit()
Called before any hardware initialisation is done.
Definition: SensebenderGatewaySerial.ino:114