Facebook
From Grzegorz, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 333
  1. #include <EEPROM.h>
  2. #include <SPI.h>
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5.  
  6. #define MY_DEBUG
  7. #define MY_BAUD_RATE 9600
  8.  
  9. // Enables and select radio type (if attached)
  10. //#define MY_RADIO_NRF24
  11. //#define MY_RADIO_RFM69
  12.  
  13. #define MY_GATEWAY_ESP8266
  14. #define MY_ESP8266_SSID "XXXX XXXXX XXXXX"
  15. #define MY_ESP8266_PASSWORD "XXXXXXX"
  16.  
  17. #define MY_ESP8266_HOSTNAME "sensor-gateway"
  18.  
  19. #define MY_IP_GATEWAY_ADDRESS 192,168,1,157
  20. #define MY_IP_SUBNET_ADDRESS 255,255,255,0
  21. #define MY_PORT 5003      
  22.  
  23. #define MY_GATEWAY_MAX_CLIENTS 10
  24.  
  25.  
  26. #define MY_INCLUSION_MODE_FEATURE
  27. #define MY_INCLUSION_MODE_DURATION 60
  28. #define MY_INCLUSION_MODE_BUTTON_PIN  3
  29.  
  30. #define MY_DEFAULT_ERR_LED_PIN 16  // Error led
  31. #define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
  32. #define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
  33.  
  34. #if defined(MY_USE_UDP)
  35.   #include <WiFiUDP.h>
  36. #else
  37.   #include <ESP8266WiFi.h>
  38. #endif
  39.  
  40. #define MY_NODE_ID 5
  41.  
  42. #include <MySensors.h>
  43.  
  44. #define CHILD_D4 2   // Temperature D4 is Internal GPIO2
  45. #define TEMP_D4 2  
  46.  
  47. #define RELAY_1 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
  48. #define RELAY_2 5
  49. //#define RELAY_3
  50. #define NUMBER_OF_RELAYS 2 // Total number of attached relays
  51. #define RELAY_ON 1  // GPIO value to write to turn on attached relay
  52. #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
  53.  
  54.  
  55. OneWire ourWire(TEMP_D4);
  56. DallasTemperature sensors(&ourWire);
  57.  
  58. MyMessage msgTemperature1(CHILD_D4, V_TEMP);
  59.  
  60. void before()
  61. {
  62.     for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
  63.         // Then set relay pins in output mode
  64.         pinMode(pin, OUTPUT);
  65.         // Set relay to last known state (using eeprom storage)
  66.         digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
  67.     }
  68. }
  69.  
  70. void setup() {
  71.   present(255, 18); //The number 18 signifies S_ARDUINO_REPEATER_NODE. This is suggested in some forums.
  72.   delay(100);
  73.   Serial.begin(9600);
  74.   sensors.begin(); //Se inician los sensore
  75. }
  76.  
  77. void presentation() {
  78.   // Present locally attached sensors here
  79.   sendSketchInfo("Cuartito", "1.0");
  80.   present(CHILD_D4, S_TEMP);  //Temperature
  81.   sleep(100);  
  82.  
  83.  
  84.     // Send the sketch version information to the gateway and Controller
  85.     sendSketchInfo("Relay", "1.0");
  86.  
  87.     for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
  88.         // Register all sensors to gw (they will be created as child devices)
  89.         present(sensor, S_BINARY);
  90.     }
  91. }
  92. void loop() {
  93.   sensors.requestTemperatures();
  94.   Serial.print(sensors.getTempCByIndex(0));
  95.   Serial.println(" grados Centigrados");
  96.   float temperature = static_cast<float>(static_cast<int>(sensors.getTempCByIndex(0) * 10.)) / 10.;
  97.   send(msgTemperature1.set(temperature,1));
  98.   delay(100); //Se provoca un lapso de 1 segundo antes de la pr√≥xima lectura
  99. }
  100.  
  101. void receive(const MyMessage &message)
  102. {
  103.     // We only expect one type of message from controller. But we better check anyway.
  104.     if (message.type==V_STATUS) {
  105.         // Change relay state
  106.         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
  107.         // Store state in eeprom
  108.         saveState(message.sensor, message.getBool());
  109.         // Write some debug info
  110.         Serial.print("Incoming change for sensor:");
  111.         Serial.print(message.sensor);
  112.         Serial.print(", New status: ");
  113.         Serial.println(message.getBool());
  114.     }
  115. }
  116.