Sie sind auf Seite 1von 2

#include <Ethernet.

h>
#include "PubSubClient.h"

#include <SoftwareSerial.h>
SoftwareSerial Ser(2, 3);

#define CLIENT_ID "acdc"


#define PUBLISH_DELAY 2000

unsigned int adcnya, kal_1, kal_2;


float data_f1, data_f2;
char in_Chr;
String Str_adc2;

//======================== SETUP IP ==============================


byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 10, 10, 30);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(10, 10, 10, 1);
IPAddress dnServer(8,8,8,8);
//================================================================

EthernetClient ethClient;
PubSubClient mqttClient;

long previousMillis;

void setup() {

pinMode(6, INPUT);
pinMode(5, INPUT);

Serial.begin(9600);
Ser.begin(2400);

Serial.println(F("ACDC Monitoring by CLIP INDONESIA"));


Serial.println();

Ethernet.begin(mac_addr, ip, dnServer, gateway, subnet);


Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
Serial.println();
mqttClient.setClient(ethClient);
mqttClient.setServer( "10.10.10.10", 1883); // IP Server Broker MQTT
Serial.println(F("Ready to sending data"));
previousMillis = millis();

kal_1 = 3226;
kal_2 = 3226;

void loop() {
while(Ser.available()) {
in_Chr = (char)Ser.read();
if (in_Chr == '\r'){
data_f2 = Str_adc2.toFloat();
Str_adc2 = "";
}
else if(in_Chr == '\n'){
}
else{
Str_adc2 += in_Chr;
}
}

if (millis() - previousMillis > PUBLISH_DELAY) {


adcnya = analogRead(A1);
data_f1 = adcnya;
data_f1 = (data_f1 * 1000) / kal_1;
data_f2 = (data_f2 * 1000) / kal_2;
Serial.print("AI_1 : ");
Serial.println(data_f1);
Serial.print("AI_2 : ");
Serial.println(data_f2);
Serial.print("DI_1 : ");

Serial.println((digitalRead(6) == HIGH) ? "OFF" : "ON");


Serial.print("DI_2 : ");
Serial.println((digitalRead(5) == HIGH) ? "OFF" : "ON");
sendData();
previousMillis = millis();
mqttClient.loop();
data_f1 = 0;
data_f2 = 0;

void sendData() {
char msgBuffer[20];
if (mqttClient.connect(CLIENT_ID)) {
mqttClient.publish("CLIP/IED01/AI01", dtostrf(data_f1, 6, 1, msgBuffer));
mqttClient.publish("CLIP/IED01/AI02", dtostrf(data_f2, 6, 1, msgBuffer));
mqttClient.publish("CLIP/IED01/DI01", (digitalRead(6) == HIGH) ? "OFF" : "ON");
mqttClient.publish("CLIP/IED01/DI02", (digitalRead(5) == HIGH) ? "OFF" : "ON");

}
}

void callback(char* topic, byte* payload, unsigned int length) {