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