-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathE03_ESP_LDR.ino
95 lines (74 loc) · 2.91 KB
/
E03_ESP_LDR.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Configure the framework
#include "bconf/MCU_ESP8266.h" // Load the code directly on the ESP8266
#include "conf/Gateway.h" // The main node is the Gateway, we have just one node
#include "conf/IPBroadcast.h"
// Define the WiFi name and password
#define WIFICONF_INSKETCH
#define WiFi_SSID "SSID"
#define WiFi_Password "PASSWORD"
// Include framework code and libraries
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include "Souliss.h"
// This identify the number of the LED logic
#define LDR 0
#define SensorLDR_pin A0
// Light calibration data
// out[] holds the values wanted in lux/10
#define sizeofarray 9 // Number of items on out and in arrays
static const unsigned int out[] = { 7, 30, 45, 65, 150, 300, 450, 2100, 13000}; // x10 //ULTIMO VALOR REFERENCIA
static const unsigned int in[] = { 100, 350, 430, 500, 680, 780, 950, 1005, 1024 }; // 0 - 1024
#define Debug Serial //Change to Serial1 if you want to use the GPIO2 to TX
void setup()
{
Initialize();
Debug.begin(115200);
// Connect to the WiFi network and get an address from DHCP
GetIPAddress();
SetAsGateway(myvNet_dhcp);
// This is the vNet address for this node, used to communicate with other
// nodes in your Souliss network
SetAddress(0xAB01, 0xFF00, 0x0000);
//Example of Peer Definition
//SetAsPeerNode(0xAB02, 1);
// This node will serve all the others in the network providing an address
Set_T54(LDR);
}
void loop()
{
EXECUTEFAST() {
UPDATEFAST();
FAST_2110ms()
{
Logic_T54(LDR);
}
FAST_7110ms()
{
float ldr_read = get_lux(in, out, sizeofarray)/100.0; //ORIGINAL
if (ldr_read == 0) ldr_read = 0.01;
Souliss_ImportAnalog(memory_map, LDR, &ldr_read);
}
// Here we handle here the communication with Android
FAST_GatewayComms();
}
}
//////////////////////////////////////////////////////////////////////////////
// Calculate lux based on rawADC reading from LDR returns value in lux/10
//////////////////////////////////////////////////////////////////////////////
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
{
// take care the value is within range
// val = constrain(val, _in[0], _in[size-1]);
int val = analogRead(A0);
Debug.print("AnalogRead: ");
Debug.println(val);
if (val <= _in[0]) return _out[0];
if (val >= _in[size-1]) return _out[size-1];
// search right interval
byte pos = 1; // _in[0] allready tested
while(val > _in[pos]) pos++;
// this will handle all exact "points" in the _in array
if (val == _in[pos]) return _out[pos];
// interpolate in the right segment for the rest
return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}