-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIoT-Crop-Monitoring-System.ino
146 lines (123 loc) · 3.5 KB
/
IoT-Crop-Monitoring-System.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
\file: iotcrpmonitor.ino
\brief: This is source file for the implementation of the IoT crop monitor
\author: César Villarreal @4497cv
\date: 01/04/2020
*/
/****************************************
* Include Libraries
****************************************/
#include "lib/UbidotsESPMQTT.h"
#include "lib/lm35dz.h"
#include "lib/cd4051be.h"
/****************************************
* Define Constants
****************************************/
#define TOKEN "BBFF-sCI4BMLnYxSCDgJwsR5AwQlwIqYsl6" // Your Ubidots TOKEN
#define WIFINAME_CASA "ARRIS-AC62" //Your SSID
#define WIFIPASS_CASA "F3DA7DAD49EFE77B" // Your Wifi Pass
#define WIFINAME "The Coolest Pool Wifi"
#define WIFIPASS "2WC466403315"
#define DEVICE_LABEL "esp8266"
#define PWROUT_VALUE 62
#define TEMP_DELAY 60 //2 min delay
#define LUM_DELAY 100 //5 min delay
#define HUM_DELAY 240 //4 min delay
static uint8_t master_switch_st;
static float temp_delay;
static float lum_delay;
static float sm_delay;
static char* sw_topic = "/v1.6/devices/control/master-switch/lv";
Ubidots client(TOKEN);
void temperature_isr()
{
float sensor_val;
cd4051be_setChannel(CH_0);
sensor_val = LM35DZ_get_current_temperature(celcius);
if((temp_limit_low < sensor_val) &&
(temp_limit_high > sensor_val))
{
client.add("lm35", sensor_val);
client.ubidotsPublish("node-mcu");
}
client.loop();
}
void luminosity_isr()
{
float sensor_val;
sensor_val = cd4051be_ChannelRead(CH_1);
sensor_val = (sensor_val*100.0)/1024.0;
client.add("luminosity", sensor_val);
client.ubidotsPublish("node-mcu");
client.loop();
}
void soil_humidity_isr()
{
float sensor_val;
sensor_val = cd4051be_ChannelRead(CH_2);
sensor_val = (841-sensor_val)/4.05;
client.add("soil-humidity", sensor_val);
client.ubidotsPublish("node-mcu");
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length)
{
int i;
Serial.print(topic);
for (i = 0; i < length ; i++) Serial.print((char)payload[i]);
Serial.println();
if(topic == sw_topic)
{
Serial.printf("MS payload [0] = %f\n", payload[0]);
master_switch_st = payload[0];
}
}
/****************************************
* Main Functions
****************************************/
void setup()
{
lm35dz_t sensor_config;
sensor_config.op_mode = cd4051be;
LM35DZ_init(sensor_config);
/* multiplexer module initialization */
cd4051be_init();
/* establish serial communication */
Serial.begin(115200);
client.ubidotsSetBroker("industrial.api.ubidots.com");
/* activate debugging messages on console */
client.setDebug(true); // Pass a true or false bool value to activate debug messages
/* establish wifi communication */
client.wifiConnection(WIFINAME_CASA, WIFIPASS_CASA);
/* set message callback function */
client.begin(callback);
/* subscribe to a device variable */
client.ubidotsSubscribe("control","master-switch"); //Insert the dataSource and Variable's Labels
}
void loop()
{
channel_e current_channel;
float sensor_val;
if(!client.connected())
{
client.reconnect();
}
current_channel = cd4051be_getCurrentChannel();
switch(current_channel)
{
case CH_0:
temperature_isr();
break;
case CH_1:
luminosity_isr();
break;
case CH_2:
soil_humidity_isr();
break;
default:
break;
}
delay(30000);
cd4051be_SwitchChannel();
client.loop();
}