MySensors Library & Examples  2.3.2-38-gaa76d26
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-2020 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 //#define MY_PJON
48 
49 #include <MySensors.h>
50 
51 #define UV_SENSOR_ANALOG_PIN 0
52 
53 #define CHILD_ID_UV 0
54 
55 uint32_t SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
56 
57 MyMessage uvMsg(CHILD_ID_UV, V_UV);
58 
59 uint32_t lastSend =0;
60 float uvIndex;
61 float lastUV = -1;
62 uint16_t uvIndexValue [12] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170};
63 
64 
66 {
67  // Send the sketch version information to the gateway and Controller
68  sendSketchInfo("UV Sensor", "1.2");
69 
70  // Register all sensors to gateway (they will be created as child devices)
71  present(CHILD_ID_UV, S_UV);
72 }
73 
74 void loop()
75 {
76  uint32_t currentTime = millis();
77 
78  uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);// Get UV value
79  if (uv>1170) {
80  uv=1170;
81  }
82 
83  //Serial.print("UV Analog reading: ");
84  //Serial.println(uv);
85 
86  int i;
87  for (i = 0; i < 12; i++) {
88  if (uv <= uvIndexValue[i]) {
89  uvIndex = i;
90  break;
91  }
92  }
93 
94  if(i==12) {
95  i--;
96  }
97 
98  //calculate 1 decimal if possible
99  if (i>0) {
100  float vRange=uvIndexValue[i]-uvIndexValue[i-1];
101  float vCalc=uv-uvIndexValue[i-1];
102  uvIndex+=(1.0/vRange)*vCalc-1.0;
103  }
104 
105  //Serial.print("UVI: ");
106  //Serial.println(uvIndex,2);
107 
108  //Send value to gateway if changed, or at least every 5 minutes
109  if ((uvIndex != lastUV)||(currentTime-lastSend >= 5UL*60UL*1000UL)) {
110  lastSend=currentTime;
111  send(uvMsg.set(uvIndex,2));
112  lastUV = uvIndex;
113  }
114 
115  sleep(SLEEP_TIME);
116 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
loop
void loop()
Main loop.
Definition: UVSensor.ino:74
presentation
void presentation()
Node presentation.
Definition: UVSensor.ino:65
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