-
Notifications
You must be signed in to change notification settings - Fork 2
/
3CurrentSensorsMQTT.ino
139 lines (109 loc) · 5.13 KB
/
3CurrentSensorsMQTT.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
// Codigo para medir corriente usando tres SCT-013 y un ESP32
// Board seleccionado: NODEMCU-32S
// 20 Marzo 2021
// Incluir Emon Library
#include "EmonLib.h"
// Mills Function
const unsigned long event_interval = 300000; // 5 minute interval, 300 second
unsigned long previous_time = 0;
// Incluir Conexion a MQTT
#include "EspMQTTClient.h"
#define ADC_INPUT_1 34 // Sensor 1
#define ADC_INPUT_2 32 // Sensor 2
#define ADC_INPUT_3 35 // Sensor 3
#define WIFI_SSID "<WiFi_username_here>" // WiFi Username
#define WIFI_PASS "<WiFi_password_here>" // Wifi Password
#define BROKER_IP "<local_ip_address>" // IP adress of MQTT broker
#define BROKER_USERNAME "<broker_username>" // Broker username
#define BROKER_PASSWORD "<broker_password>" // Broker password
#define CLIENT_NAME "<MQTT_name_to_identify_device>" // Mqtt client name to identify the device
#define BROKER_PORT 1883 // MQTT Port, 1883 is default
#define lastwill_topic "<last_will_topic>" // MQTT topic to report lastwill and testament.
#define lastwill_text "Current sensor has gone offline unexpextedly." // MQTT message to report lastwill and testament.
String client_name = CLIENT_NAME; // MQTT Topic to report initial value
String startup_topic = "<startup_topic>"; // MQTT Topic to report startup
String medidor_corriente1_topic = "<topic_to_report_sensor_1>"; // MQTT topic to report values
String medidor_corriente2_topic = "<topic_to_report_sensor_2>"; // MQTT topic to report values
String medidor_corriente3_topic = "<topic_to_report_sensor_3>"; // MQTT topic to report values
// Voltaje de red eléctrica
float voltajeRed = 120.0;
// Crear tres instancias de EnergyMonitor
EnergyMonitor energyMonitor1;
EnergyMonitor energyMonitor2;
EnergyMonitor energyMonitor3;
// Function to connect to MQTT
EspMQTTClient client(
WIFI_SSID,
WIFI_PASS,
BROKER_IP, // MQTT Broker server IP
BROKER_USERNAME, // Can be omitted if not needed
BROKER_PASSWORD, // Can be omitted if not needed
CLIENT_NAME, // Client name that uniquely identify your device
BROKER_PORT // The MQTT port, default to 1883. this line can be omitted
);
void setup()
{
Serial.begin(115200);
// Enable debugging messages sent to serial output
client.enableDebuggingMessages();
// Enable the web updater.
client.enableHTTPWebUpdater();
// MQTT Last Will & Testament
client.enableLastWillMessage( lastwill_topic , lastwill_text);
// Número de pin: donde tenemos conectado el SCT-013
// Valor de calibración: valor obtenido de la calibración teórica
energyMonitor1.current(ADC_INPUT_1, 0.145);
energyMonitor2.current(ADC_INPUT_2, 0.145);
energyMonitor3.current(ADC_INPUT_3, 0.145);
}
//MQTT Innitial Connection
void onConnectionEstablished()
{
client.publish(startup_topic, String(client_name + " is now online."));
}
void loop()
{
// MQTT Loop: Must be called once per loop.
client.loop();
// Mills Loop
unsigned long current_time = millis();
// Mills If Statment
if(current_time - previous_time >= event_interval) {
// Pasamos el número de muestras que queremos tomar
double Irms1 = energyMonitor1.calcIrms(1484);
// Calculamos la potencia aparente
double potencia1 = Irms1 * voltajeRed;
// Pasamos el número de muestras que queremos tomar
double Irms2 = energyMonitor2.calcIrms(1484);
// Calculamos la potencia aparente
double potencia2 = Irms2 * voltajeRed;
// Pasamos el número de muestras que queremos tomar
double Irms3 = energyMonitor3.calcIrms(1484);
// Calculamos la potencia aparente
double potencia3 = Irms3 * voltajeRed;
// Mostramos la información por el monitor serie
Serial.print("Potencia 1 = ");
Serial.print(potencia1);
Serial.print(" Irms 1 = ");
Serial.println(Irms1);
// Mostramos la información por el monitor serie
Serial.print("Potencia 2 = ");
Serial.print(potencia2);
Serial.print(" Irms 2 = ");
Serial.println(Irms2);
// Mostramos la información por el monitor serie
Serial.print("Potencia 3 = ");
Serial.print(potencia3);
Serial.print(" Irms 3 = ");
Serial.println(Irms3);
Serial.print('\n');
Serial.print('\n');
// MQTT Client Publisher
client.publish(medidor_corriente1_topic, String(potencia1));
client.publish(medidor_corriente2_topic, String(potencia2));
client.publish(medidor_corriente3_topic, String(potencia3));
Serial.print('\n');
// Update timing for next time
previous_time = current_time;
}
}