|
| 1 | +#include <SPI.h> |
| 2 | +#include <MySensor.h> |
1 | 3 | /*
|
2 |
| - Vera Arduino UVM-30A |
| 4 | + Arduino UVM-30A |
3 | 5 |
|
4 | 6 | connect the sensor as follows :
|
5 | 7 |
|
6 | 8 | + >>> 5V
|
7 | 9 | - >>> GND
|
8 | 10 | out >>> A0
|
9 | 11 |
|
10 |
| - Contribution: epierre, bulldoglowell |
11 |
| - Converted to 1.4 by Henrik Ekblad |
| 12 | + Contribution: epierre, bulldoglowell, gizmocuz |
| 13 | +
|
| 14 | + Index table taken from: http://www.elecrow.com/sensors-c-111/environment-c-111_112/uv-sensor-moduleuvm30a-p-716.html |
| 15 | + Because this table is pretty lineair, we can calculate a UVI with one decimal |
12 | 16 |
|
13 | 17 | License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
|
14 |
| - |
15 | 18 | */
|
16 | 19 |
|
17 | 20 | #include <MySensor.h>
|
18 | 21 | #include <SPI.h>
|
19 | 22 |
|
20 |
| -#define CHILD_ID_UV 0 |
21 | 23 | #define UV_SENSOR_ANALOG_PIN 0
|
| 24 | + |
| 25 | +#define CHILD_ID_UV 0 |
| 26 | + |
22 | 27 | unsigned long SLEEP_TIME = 30*1000; // Sleep time between reads (in milliseconds)
|
23 | 28 |
|
24 | 29 | MySensor gw;
|
25 | 30 | MyMessage uvMsg(CHILD_ID_UV, V_UV);
|
26 |
| -int lastUV = -1; |
27 |
| -int uvIndexValue [13] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170, 3000}; |
28 |
| -int uvIndex; |
| 31 | + |
| 32 | +unsigned long lastSend =0; |
| 33 | +float uvIndex; |
| 34 | +float lastUV = -1; |
| 35 | +int uvIndexValue [12] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170}; |
29 | 36 |
|
30 | 37 | void setup()
|
31 | 38 | {
|
32 | 39 | gw.begin();
|
33 | 40 |
|
34 | 41 | // Send the sketch version information to the gateway and Controller
|
35 |
| - gw.sendSketchInfo("UV Sensor", "1.1"); |
| 42 | + gw.sendSketchInfo("UV Sensor", "1.2"); |
36 | 43 |
|
37 | 44 | // Register all sensors to gateway (they will be created as child devices)
|
38 | 45 | gw.present(CHILD_ID_UV, S_UV);
|
39 |
| - |
40 | 46 | }
|
41 | 47 |
|
42 | 48 | void loop()
|
43 |
| -{ |
44 |
| - uint16_t uv = analogRead(0);// Get UV value |
45 |
| - Serial.print("Uv reading: "); |
46 |
| - Serial.println(uv); |
47 |
| - for (int i = 0; i < 13; i++) |
| 49 | +{ |
| 50 | + unsigned long currentTime = millis(); |
| 51 | + |
| 52 | + uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);// Get UV value |
| 53 | + if (uv>1170) |
| 54 | + uv=1170; |
| 55 | + |
| 56 | + //Serial.print("UV Analog reading: "); |
| 57 | + //Serial.println(uv); |
| 58 | + |
| 59 | + int i; |
| 60 | + for (i = 0; i < 12; i++) |
48 | 61 | {
|
49 | 62 | if (uv <= uvIndexValue[i])
|
50 | 63 | {
|
51 | 64 | uvIndex = i;
|
52 | 65 | break;
|
53 | 66 | }
|
54 | 67 | }
|
55 |
| - Serial.print("Uv index: "); |
56 |
| - Serial.println(uvIndex); |
| 68 | + |
| 69 | + //calculate 1 decimal if possible |
| 70 | + if (i>0) { |
| 71 | + float vRange=uvIndexValue[i]-uvIndexValue[i-1]; |
| 72 | + float vCalc=uv-uvIndexValue[i-1]; |
| 73 | + uvIndex+=(1.0/vRange)*vCalc-1.0; |
| 74 | + } |
| 75 | + |
| 76 | + //Serial.print("UVI: "); |
| 77 | + //Serial.println(uvIndex,2); |
57 | 78 |
|
58 |
| - if (uvIndex != lastUV) { |
59 |
| - gw.send(uvMsg.set(uvIndex)); |
| 79 | + //Send value to gateway if changed, or at least every 5 minutes |
| 80 | + if ((uvIndex != lastUV)||(currentTime-lastSend >= 5*60*1000)) { |
| 81 | + lastSend=currentTime; |
| 82 | + gw.send(uvMsg.set(uvIndex,2)); |
60 | 83 | lastUV = uvIndex;
|
61 | 84 | }
|
62 | 85 |
|
|
0 commit comments