MySensors Library & Examples  2.3.2
UVSensor.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 - epierre
23  * Contribution: bulldoglowell, gizmocuz
24  *
25  * DESCRIPTION
26  * Arduino UVM-30A
27  * Index table taken from: http://www.elecrow.com/sensors-c-111/environment-c-111_112/uv-sensor-moduleuvm30a-p-716.html
28  * Because this table is pretty lineair, we can calculate a UVI with one decimal
29  *
30  * Connect sensor:
31  *
32  * + >>> 5V
33  * - >>> GND
34  * out >>> A0
35  *
36  * License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
37  */
38 
39 // Enable debug prints to serial monitor
40 #define MY_DEBUG
41 
42 // Enable and select radio type attached
43 #define MY_RADIO_RF24
44 //#define MY_RADIO_NRF5_ESB
45 //#define MY_RADIO_RFM69
46 //#define MY_RADIO_RFM95
47 
48 #include <MySensors.h>
49 
50 #define UV_SENSOR_ANALOG_PIN 0
51 
52 #define CHILD_ID_UV 0
53 
54 uint32_t SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
55 
56 MyMessage uvMsg(CHILD_ID_UV, V_UV);
57 
58 uint32_t lastSend =0;
59 float uvIndex;
60 float lastUV = -1;
61 uint16_t uvIndexValue [12] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170};
62 
63 
65 {
66  // Send the sketch version information to the gateway and Controller
67  sendSketchInfo("UV Sensor", "1.2");
68 
69  // Register all sensors to gateway (they will be created as child devices)
70  present(CHILD_ID_UV, S_UV);
71 }
72 
73 void loop()
74 {
75  uint32_t currentTime = millis();
76 
77  uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);// Get UV value
78  if (uv>1170) {
79  uv=1170;
80  }
81 
82  //Serial.print("UV Analog reading: ");
83  //Serial.println(uv);
84 
85  int i;
86  for (i = 0; i < 12; i++) {
87  if (uv <= uvIndexValue[i]) {
88  uvIndex = i;
89  break;
90  }
91  }
92 
93  //calculate 1 decimal if possible
94  if (i>0) {
95  float vRange=uvIndexValue[i]-uvIndexValue[i-1];
96  float vCalc=uv-uvIndexValue[i-1];
97  uvIndex+=(1.0/vRange)*vCalc-1.0;
98  }
99 
100  //Serial.print("UVI: ");
101  //Serial.println(uvIndex,2);
102 
103  //Send value to gateway if changed, or at least every 5 minutes
104  if ((uvIndex != lastUV)||(currentTime-lastSend >= 5UL*60UL*1000UL)) {
105  lastSend=currentTime;
106  send(uvMsg.set(uvIndex,2));
107  lastUV = uvIndex;
108  }
109 
110  sleep(SLEEP_TIME);
111 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: UVSensor.ino:73
presentation
void presentation()
Node presentation.
Definition: UVSensor.ino:64
MyMessage::set
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
send
bool send(MyMessage &msg, const bool requestEcho=false)
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
sleep
int8_t sleep(const uint32_t sleepingMS, const bool smartSleep=false)
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289