-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1
199 lines (188 loc) · 6.01 KB
/
v1
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// UNIVERSIDAD POLITECNICA SALESIANA
// CARRERA DE INGENIERÍA ELECTRÓNICA
// PROYECTO IOT DE ACUICULTURA
//LIBRERIAS
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <SimpleDHT.h> //DHT22
#include <OneWire.h> //DS18B20
#include <DallasTemperature.h> //DS18B20
#include <Wire.h> //Level_Sensor PH_Sensor TDS_Sensor GAS_Sensors
#include <Adafruit_ADS1015.h> //Level_Sensor PH_Sensor TDS_Sensor
GAS_Sensors
//Sensor DS18B20
#define oneWireBus 2 //GPIO16
//GAS_Sensors
#define nBits sizeof(Select_pins)/sizeof(Select_pins[0])
#define VALVE 10
const char *ssid = "Acuicultura_UPS"; // replace with your wifi ssid and wpa2 key
const char *pass = "UPS.2020*_*";
unsigned long myChannelNumber = 1244877; // Thingspeak número de canal
const char * myWriteAPIKey = "1LSP5WCJ41PFC6WB"; // ThingSpeak write API
Key
//SENSOR DE TEMPERATURA Y HUMEDAD DHT22
int pinDHT22 = 16; // Puerto D0 entrada de datos
SimpleDHT22 dht22; // Modelo de sensor
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
//Flow_Sensor
#define PIN_FLOW 14
volatile int flow_frequency; // Measures flow sensor pulses
unsigned long l_hour; // Calculated litres/hour
unsigned long currentTime;
unsigned long cloopTime;
ICACHE_RAM_ATTR void flow () // Interrupt function
{
flow_frequency++;
}
71
//Level_Sensor PH_Sensor TDS_Sensor GAS_Sensors
Adafruit_ADS1115 ads(0x49);
float Voltage = 0.0; //Level_Sensor
float Voltage1 = 0.0; //PH_Sensor
float Voltage2 = 0.0; //TDS_Sensor
int Select_pins[] = { 12, 13}; //Mux ADC
float Voltages[] = {0.0, 0.0, 0.0, 0.0};
int16_t Gases_adc_Value[] = {0, 0, 0, 0};
void ADC_selector(byte n){
for (byte i=0; i<nBits; i++){
digitalWrite(Select_pins[i], n & 1);
n /= 2;
}
}
WiFiClient client;
void setup(){
Serial.begin(115200); // Velocidad del puerto serie
WiFi.mode(WIFI_STA); //Establece el módulo como cliente wifi
WiFi.disconnect(); //Se desconecta de cualquier WiFi conectado
delay(100);
pinMode(pinDHT22,INPUT); //DHT22
sensors.begin(); //DS18B20
ads.begin(); // Level_Sensor PDH_Sensor TDS_Sensor GAS_Sensors
pinMode(VALVE, OUTPUT); //Valvula_Relé
//Flow_Sensor
pinMode(PIN_FLOW, INPUT);
attachInterrupt(digitalPinToInterrupt(PIN_FLOW), flow, FALLING); // Setup
Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
//Gases_Sensors
for (byte i=0; i<nBits; i++){
pinMode(Select_pins[i], OUTPUT);
}
WiFi.begin(ssid, pass); // Inicia WIFI
Serial.println("Conectando a "); // Conexión WIFI
72
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED){ // wait for WiFi
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connectado");
ThingSpeak.begin(client); // Inicia WIFI
}
void loop(){
//Hacemos pausa de dos segundos antes de cada nueva medición
//al sensor le cuesta 200ms leer estos datos
delay(2000);
//Leer datos de los sensores
float t = 0;
float h = 0;
if (dht22.read2(pinDHT22, &t, &h, NULL)){
Serial.println("Error en la lectura.");
return;
}
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
float temperatureF = sensors.getTempFByIndex(0);
int16_t adc0;
adc0 = ads.readADC_SingleEnded(0);
Voltage = adc0*(0.1875/1000); //V
int16_t adc1;
adc1 = ads.readADC_SingleEnded(1);
Voltage1 = adc1*(0.1875/1000); //V
int16_t adc2;
adc2 = ads.readADC_SingleEnded(2);
Voltage2 = adc2*(0.1875/1000); //V
//Flow_Sensor
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
73
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q =
flowrate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour); // Print litres/hour
Serial.println(" [L/Hour]");
}
//GAS_Sensors
for (byte i=0; i<=3; i++){
ADC_selector(i);
Gases_adc_Value[i] = ads.readADC_SingleEnded(3);
Voltages[i] = Gases_adc_Value[i]*(0.1875/1000); //V
delay(100);
}
for (byte j=0; j<=3; j++){
Serial.print("ADC"+(String)j+": ");
Serial.print(Gases_adc_Value[j]);
Serial.print("\tV"+(String)j+": ");
Serial.println(Voltages[j], 7);
}
Serial.println("****");
Serial.println();
delay(1000);
//Muestra por el puerto serie las medidas obtenidas
Serial.print("Humedad relativa: ");
Serial.print((float)h);
Serial.print(" %\t");
Serial.print("Temperatura Ambiente: ");
Serial.print((float)t);
Serial.println(" *C");
Serial.print((float)temperatureC);
Serial.println("ºC Temperatura del agua en Centígrados");
Serial.print((float)temperatureF);
Serial.println("ºF Temperatura del agua en Farenheit");
Serial.print("ADC: ");
Serial.print(adc0);
Serial.print("\tV: ");
Serial.println(Voltage, 7);
Serial.println();
Serial.print("ADC: ");
Serial.print(adc1);
Serial.print("\tV: ");
74
Serial.println(Voltage1, 7);
Serial.println();
Serial.print("ADC: ");
Serial.print(adc2);
Serial.print("\tV: ");
Serial.println(Voltage2, 7);
Serial.println();
//ThingSpeak
//Carga los valores a enviar
ThingSpeak.setField(1, (float)t); //Temperatura Ambiente
ThingSpeak.setField(2, (float)h); // Humedad relativa
ThingSpeak.setField(3, (float)temperatureC); //Temperatura del agua
ThingSpeak.setField(4, (float)Voltage); //Level_Sensor
ThingSpeak.setField(5, (float)Voltage1); //PH_Sensor
ThingSpeak.setField(6, (float)Voltage2); //TDS_Sensor
ThingSpeak.setField(7, (float)Voltages[0]); //Gas1
ThingSpeak.setField(8, (float)Voltages[1]); //Gas2
ThingSpeak.setField(9, (float)Voltages[2]); //Gas3
ThingSpeak.setField(10, (float)Voltages[3]); //Gas4
ThingSpeak.setField(11, (float)l_hour); //Flow_Sensor
//Escribe todos los campos a la vez.
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("¡Datos enviados a ThingSpeak!");
//Valvula_Relé
digitalWrite(VALVE, HIGH);
delay(1000);
digitalWrite(VALVE, LOW);
delay(1000);
delay(10000);
}