Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ACS712.h"
- #include <SPI.h>
- #include <Ethernet.h>
- #include <PubSubClient.h>
- // Update these with values suitable for your network.
- byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
- IPAddress ip(192, 168, 43, 200);
- //IPAddress server(172, 16, 0, 2);
- const char* server = "broker.hivemq.com";
- const char* mqtt_server = "broker.hivemq.com";
- const char* mqtt_user = "";
- const char* mqtt_pass= "";
- EthernetClient ethClient;
- PubSubClient client(ethClient);
- long lastReconnectAttempt = 0;
- /*
- Hasil Kalibrasi (Lampu 1 - Lampu 5)
- 168 280 243 261 523
- 168 280 261 243 392
- 168 280 280 299 504
- 168 261 187 243 448
- 168 261 224 243 485
- 187 336 243 224 541
- 187 261 280 280 429
- */
- int penyesuaian1 = 10;
- int penyesuaian2 = 300;
- int penyesuaian3 = 150;
- int penyesuaian4 = 150;
- int penyesuaian5 = 200;
- int lampu1;
- int lampu2;
- int lampu3;
- int lampu4;
- int lampu5;
- float v1;
- float v2;
- float v3;
- float v4;
- float v5;
- float i1;
- float i2;
- float i3;
- float i4;
- float i5;
- ACS712 ACS1(A1, 5.0 , 512, 185);
- ACS712 ACS2(A2, 5.0, 512, 185);
- ACS712 ACS3(A3, 5.0, 512, 185);
- ACS712 ACS4(A4, 5.0, 512, 185);
- ACS712 ACS5(A5, 5.0, 512, 185);
- void setup()
- {
- Serial.begin(115200);
- client.setServer(mqtt_server, 1883);
- Ethernet.begin(mac, ip);
- // Allow the hardware to sort itself out
- delay(1500);
- lastReconnectAttempt = 0;
- Serial.print("IP Address Micro: ");
- Serial.println(Ethernet.localIP());
- Serial.println("Mulai");
- }
- void loop()
- {
- client.loop();
- int hasil1 = ACS1.mA_AC();
- int hasil2 = ACS2.mA_AC();
- int hasil3 = ACS3.mA_AC();
- int hasil4 = ACS4.mA_AC();
- int hasil5 = ACS5.mA_AC();
- Serial.print(hasil1); Serial.print("\t");
- Serial.print(hasil2); Serial.print("\t");
- Serial.print(hasil3); Serial.print("\t");
- Serial.print(hasil4); Serial.print("\t");
- Serial.print(hasil5); Serial.println("\t");
- hasil1 = hasil1 + penyesuaian1;
- hasil2 = hasil2 + penyesuaian2;
- hasil3 = hasil3 + penyesuaian3;
- hasil4 = hasil4 + penyesuaian4;
- hasil5 = hasil5 + penyesuaian5;
- v1 = (hasil1 * 5)/1024;
- v2 = (hasil2 * 5)/1024;
- v3 = (hasil3 * 5)/1024;
- v4 = (hasil4 * 5)/1024;
- v5 = (hasil5 * 5)/1024;
- i1 = v1/185;
- i2 = v2/185;
- i3 = v3/185;
- i4 = v4/185;
- i5 = v5/185;
- if (hasil1 < 0) hasil1 = 0;
- if (hasil2 < 0) hasil2 = 0;
- if (hasil3 < 0) hasil3 = 0;
- if (hasil4 < 0) hasil4 = 0;
- if (hasil5 < 0) hasil5 = 0;
- if (hasil1 > 900) {
- lampu1 = 1;
- }else {
- lampu1 = 0;
- i1=0;
- }
- if (hasil2 > 900) {
- lampu2 = 1;
- }else {
- lampu2 = 0;
- i2=0;
- }
- if (hasil3 > 900) {
- lampu3 = 1;
- }else {
- lampu3 = 0;
- i3=0;
- }
- if (hasil4 > 900) {
- lampu4 = 1;
- }else {
- lampu4 = 0;
- i4=0;
- }
- if (hasil5 > 900) {
- lampu5 = 1;
- }else {
- lampu5 = 0;
- i5=0;
- }
- Serial.print(lampu1); Serial.print("\t");
- Serial.print(lampu2); Serial.print("\t");
- Serial.print(lampu3); Serial.print("\t");
- Serial.print(lampu4); Serial.print("\t");
- Serial.print(lampu5); Serial.println("\t");
- delay(300);
- if (!client.connected()) {
- long now = millis();
- if (now - lastReconnectAttempt > 5000) {
- lastReconnectAttempt = now;
- // Attempt to reconnect
- if (reconnect()) {
- lastReconnectAttempt = 0;
- }
- }
- } else {
- // Alat berhasil terhubung ke broker
- if (!client.connected()) {
- reconnect();
- }
- //Serial.println ("Kirim data ke MQTT Broker");
- client.loop();
- client.publish("approach1/lampu1", String(lampu1).c_str(), true);
- client.publish("approach1/lampu2", String(lampu2).c_str(), true);
- client.publish("approach1/lampu3", String(lampu3).c_str(), true);
- client.publish("approach1/lampu4", String(lampu4).c_str(), true);
- client.publish("approach1/lampu5", String(lampu5).c_str(), true);
- client.publish("approach1/arus1", String(i1).c_str(), true);
- client.publish("approach1/arus2", String(i2).c_str(), true);
- client.publish("approach1/arus3", String(i3).c_str(), true);
- client.publish("approach1/arus4", String(i4).c_str(), true);
- client.publish("approach1/arus5", String(i5).c_str(), true);
- client.publish("approach1/adc1", String(hasil1).c_str(), true);
- client.publish("approach1/adc2", String(hasil2).c_str(), true);
- client.publish("approach1/adc3", String(hasil3).c_str(), true);
- client.publish("approach1/adc4", String(hasil4).c_str(), true);
- client.publish("approach1/adc5", String(hasil5).c_str(), true);
- delay(100);
- }
- }
- boolean reconnect() {
- if (client.connect("lampu1")) {
- // Ketika Terhubung Kirim Status Lampu Awal = 0
- Serial.println ("Berhasil terhubung ke Broker");
- }
- return client.connected();
- }
Advertisement
Add Comment
Please, Sign In to add comment