MySensors Library & Examples  2.3.2
LightSensor.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 <[email protected]>
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  * REVISION HISTORY
22  * Version 1.0 - Henrik EKblad
23  *
24  * DESCRIPTION
25  * Example sketch showing how to measure light level using a LM393 photo-resistor
26  * http://www.mysensors.org/build/light
27  */
28 
29 // Enable debug prints to serial monitor
30 #define MY_DEBUG
31 
32 // Enable and select radio type attached
33 #define MY_RADIO_RF24
34 //#define MY_RADIO_NRF5_ESB
35 //#define MY_RADIO_RFM69
36 //#define MY_RADIO_RFM95
37 
38 #include <MySensors.h>
39 
40 #define CHILD_ID_LIGHT 0
41 #define LIGHT_SENSOR_ANALOG_PIN 0
42 
43 uint32_t SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
44 
45 MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
46 int lastLightLevel;
47 
48 
50 {
51  // Send the sketch version information to the gateway and Controller
52  sendSketchInfo("Light Sensor", "1.0");
53 
54  // Register all sensors to gateway (they will be created as child devices)
55  present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
56 }
57 
58 void loop()
59 {
60  int16_t lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
61  Serial.println(lightLevel);
62  if (lightLevel != lastLightLevel) {
63  send(msg.set(lightLevel));
64  lastLightLevel = lightLevel;
65  }
66  sleep(SLEEP_TIME);
67 }
68 
69 
70 
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
API declaration for MySensors.
bool send(MyMessage &msg, const bool requestEcho=false)
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
void presentation()
Node presentation.
Definition: LightSensor.ino:49
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
void loop()
Main loop.
Definition: LightSensor.ino:58