Guest User

Sketch

a guest
Sep 24th, 2019
156
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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-2019 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/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. * DESCRIPTION
  22. * The ArduinoGateway prints data received from sensors on the serial link.
  23. * The gateway accepts input on serial which will be sent out on radio network.
  24. *
  25. * This GW code is designed for Sensebender GateWay / (Arduino Zero variant)
  26. *
  27. * Wire connections (OPTIONAL):
  28. * - Inclusion button should be connected to SW2
  29. *
  30. * LEDs on board (default assignments):
  31. * - Orange: USB RX/TX - Blink when receiving / transmitting on USB CDC device
  32. * - Yellow: RX - Blink fast on radio message received. In inclusion mode will blink fast only on presentation received
  33. * - Green : TX - Blink fast on radio message transmitted. In inclusion mode will blink slowly
  34. * - Red : ERR - Fast blink on error during transmission error or receive crc error
  35. * - Blue : free - (use with LED_BLUE macro)
  36. *
  37. */
  38.  
  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_RADIO_RFM69
  49. #define MY_IS_RFM69HW
  50. #define RFM69_868MH
  51. #define MY_RFM69_NEW_DRIVER
  52.  
  53. // Set LOW transmit power level as default, if you have an amplified NRF-module and
  54. // power your radio separately with a good regulator you can turn up PA level.
  55. #define MY_RF24_PA_LEVEL RF24_PA_HIGH
  56.  
  57. // Enable serial gateway
  58. //#define MY_GATEWAY_SERIAL
  59.  
  60. // Enable gateway ethernet module type
  61. #define MY_GATEWAY_W5100
  62.  
  63. // Enable to UDP
  64. //#define MY_USE_UDP
  65.  
  66. //#define MY_IP_ADDRESS 192,168,3,160 // wpisz adres ip bramki
  67. // Renewal period if using DHCP
  68. //#define MY_IP_RENEWAL_INTERVAL 60000
  69. // The port to keep open on node server mode / or port to contact in client mode
  70. #define MY_PORT 5003 //wpisz numer portu bramki
  71.  
  72. // Controller ip address. Enables client mode (default is "server" mode).
  73. // Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
  74. //#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254
  75.  
  76. // The MAC address can be anything you want but should be unique on your network.
  77. // Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
  78. // Note that most of the Ardunio examples use "DEAD BEEF FEED" for the MAC address.
  79. #define MY_MAC_ADDRESS 0xBC, 0xEA, 0x3F, 0x3F, 0xAB, 0xCD
  80.  
  81. // Define a lower baud rate for Arduinos running on 8 MHz (Arduino Pro Mini 3.3V & Sensebender)
  82. #if F_CPU == 8000000L
  83. #define MY_BAUD_RATE 38400
  84. #endif
  85.  
  86. // Enable inclusion mode
  87. #define MY_INCLUSION_MODE_FEATURE
  88. // Enable Inclusion mode button on gateway
  89. #define MY_INCLUSION_BUTTON_FEATURE
  90.  
  91. // Inverses behavior of inclusion button (if using external pullup)
  92. //#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP
  93.  
  94. // Set inclusion mode duration (in seconds)
  95. #define MY_INCLUSION_MODE_DURATION 60
  96. // Digital pin used for inclusion mode button
  97. //#define MY_INCLUSION_MODE_BUTTON_PIN 3
  98.  
  99. // Set blinking period
  100. #define MY_DEFAULT_LED_BLINK_PERIOD 300
  101.  
  102. // Inverses the behavior of leds
  103. //#define MY_WITH_LEDS_BLINKING_INVERSE
  104.  
  105. // Flash leds on rx/tx/err
  106. // Uncomment to override default HW configurations
  107. //#define MY_DEFAULT_ERR_LED_PIN 4 // Error led pin
  108. //#define MY_DEFAULT_RX_LED_PIN 6 // Receive led pin
  109. //#define MY_DEFAULT_TX_LED_PIN 5 // the PCB, on board LED
  110.  
  111. #include <MySensors.h>
  112. #include <SD.h>
  113. #include <drivers/ATSHA204/ATSHA204.cpp>
  114.  
  115. Sd2Card card;
  116.  
  117. #define EEPROM_VERIFICATION_ADDRESS 0x01
  118.  
  119. static uint8_t num_of_leds = 5;
  120. static uint8_t leds[] = {LED_BLUE, LED_RED, LED_GREEN, LED_YELLOW, LED_ORANGE};
  121.  
  122. void setup()
  123. {
  124. // Setup locally attached sensors
  125. }
  126.  
  127. void presentation()
  128. {
  129. // Present locally attached sensors
  130. }
  131.  
  132. void loop()
  133. {
  134. // Send locally attached sensor data here
  135. }
  136.  
  137.  
  138. void preHwInit()
  139. {
  140.  
  141. pinMode(MY_SWC1, INPUT_PULLUP);
  142. pinMode(MY_SWC2, INPUT_PULLUP);
  143. if (digitalRead(MY_SWC1) && digitalRead(MY_SWC2)) {
  144. return;
  145. }
  146.  
  147. uint8_t tests = 0;
  148.  
  149. for (int i=0; i< num_of_leds; i++) {
  150. pinMode(leds[i], OUTPUT);
  151. }
  152. if (digitalRead(MY_SWC1)) {
  153. uint8_t led_state = 0;
  154. while (!Serial) {
  155. digitalWrite(LED_BLUE, led_state);
  156. led_state ^= 0x01;
  157. delay(500);
  158. } // Wait for USB to be connected, before spewing out data.
  159. }
  160. digitalWrite(LED_BLUE, LOW);
  161. if (Serial) {
  162. Serial.println("Sensebender GateWay test routine");
  163. Serial.print("MySensors core version : ");
  164. Serial.println(MYSENSORS_LIBRARY_VERSION);
  165. Serial.print("GateWay sketch version : ");
  166. Serial.println(SKETCH_VERSION);
  167. Serial.println("----------------------------------");
  168. Serial.println();
  169. }
  170. if (testSha204()) {
  171. digitalWrite(LED_GREEN, HIGH);
  172. tests++;
  173. }
  174. if (testSDCard()) {
  175. digitalWrite(LED_YELLOW, HIGH);
  176. tests++;
  177. }
  178.  
  179. if (testEEProm()) {
  180. digitalWrite(LED_ORANGE, HIGH);
  181. tests++;
  182. }
  183. if (testAnalog()) {
  184. digitalWrite(LED_BLUE, HIGH);
  185. tests++;
  186. }
  187. if (tests == 4) {
  188. while(1) {
  189. for (int i=0; i<num_of_leds; i++) {
  190. digitalWrite(leds[i], HIGH);
  191. delay(200);
  192. digitalWrite(leds[i], LOW);
  193. }
  194. }
  195. } else {
  196. while (1) {
  197. digitalWrite(LED_RED, HIGH);
  198. delay(200);
  199. digitalWrite(LED_RED, LOW);
  200. delay(200);
  201. }
  202. }
  203.  
  204. }
  205.  
  206. bool testSha204()
  207. {
  208. uint8_t rx_buffer[SHA204_RSP_SIZE_MAX];
  209. uint8_t ret_code;
  210. if (Serial) {
  211. Serial.print("- > SHA204 ");
  212. }
  213. atsha204_init(MY_SIGNING_ATSHA204_PIN);
  214. ret_code = atsha204_wakeup(rx_buffer);
  215.  
  216. if (ret_code == SHA204_SUCCESS) {
  217. ret_code = atsha204_getSerialNumber(rx_buffer);
  218. if (ret_code != SHA204_SUCCESS) {
  219. if (Serial) {
  220. Serial.println(F("Failed to obtain device serial number. Response: "));
  221. }
  222. Serial.println(ret_code, HEX);
  223. } else {
  224. if (Serial) {
  225. Serial.print(F("Ok (serial : "));
  226. for (int i=0; i<9; i++) {
  227. if (rx_buffer[i] < 0x10) {
  228. Serial.print('0'); // Because Serial.print does not 0-pad HEX
  229. }
  230. Serial.print(rx_buffer[i], HEX);
  231. }
  232. Serial.println(")");
  233. }
  234. return true;
  235. }
  236. } else {
  237. if (Serial) {
  238. Serial.println(F("Failed to wakeup SHA204"));
  239. }
  240. }
  241. return false;
  242. }
  243.  
  244. bool testSDCard()
  245. {
  246. if (Serial) {
  247. Serial.print("- > SD CARD ");
  248. }
  249. if (!card.init(SPI_HALF_SPEED, MY_SDCARD_CS)) {
  250. if (Serial) {
  251. Serial.println("SD CARD did not initialize!");
  252. }
  253. } else {
  254. if (Serial) {
  255. Serial.print("SD Card initialized correct! - ");
  256. Serial.print("type detected : ");
  257. switch(card.type()) {
  258. case SD_CARD_TYPE_SD1:
  259. Serial.println("SD1");
  260. break;
  261. case SD_CARD_TYPE_SD2:
  262. Serial.println("SD2");
  263. break;
  264. case SD_CARD_TYPE_SDHC:
  265. Serial.println("SDHC");
  266. break;
  267. default:
  268. Serial.println("Unknown");
  269. }
  270. }
  271. return true;
  272. }
  273. return false;
  274. }
  275.  
  276. bool testEEProm()
  277. {
  278. uint8_t eeprom_d1, eeprom_d2;
  279. SerialUSB.print(" -> EEPROM ");
  280. eeprom_d1 = hwReadConfig(EEPROM_VERIFICATION_ADDRESS);
  281. delay(500);
  282. eeprom_d1 = ~eeprom_d1; // invert the bits
  283. hwWriteConfig(EEPROM_VERIFICATION_ADDRESS, eeprom_d1);
  284. delay(500);
  285. eeprom_d2 = hwReadConfig(EEPROM_VERIFICATION_ADDRESS);
  286. if (eeprom_d1 == eeprom_d2) {
  287. SerialUSB.println("PASSED");
  288. hwWriteConfig(EEPROM_VERIFICATION_ADDRESS, ~eeprom_d1);
  289. return true;
  290. }
  291. SerialUSB.println("FAILED!");
  292. return false;
  293. }
  294.  
  295. bool testAnalog()
  296. {
  297. int bat_detect = analogRead(MY_BAT_DETECT);
  298. Serial.print("-> analog : ");
  299. Serial.print(bat_detect);
  300. if (bat_detect < 400 || bat_detect > 650) {
  301. Serial.println(" Failed");
  302. return false;
  303. }
  304. Serial.println(" Passed");
  305. return true;
  306. }
RAW Paste Data