bangnaga

Arduino Uno Eth Mqtt ACS

Jun 25th, 2022 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. #include "ACS712.h"
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <PubSubClient.h>
  5.  
  6. // Update these with values suitable for your network.
  7. byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
  8. IPAddress ip(192, 168, 43, 200);
  9. //IPAddress server(172, 16, 0, 2);
  10. const char* server = "broker.hivemq.com";
  11.  
  12. const char* mqtt_server = "broker.hivemq.com";
  13. const char* mqtt_user = "";
  14. const char* mqtt_pass= "";
  15. EthernetClient ethClient;
  16. PubSubClient client(ethClient);
  17. long lastReconnectAttempt = 0;
  18. /*
  19. Hasil Kalibrasi (Lampu 1 - Lampu 5)
  20. 168  280 243 261 523
  21. 168  280 261 243 392
  22. 168  280 280 299 504
  23. 168  261 187 243 448
  24. 168  261 224 243 485
  25. 187  336 243 224 541
  26. 187  261 280 280 429
  27. */
  28. int penyesuaian1 = 10;
  29. int penyesuaian2 = 300;
  30. int penyesuaian3 = 150;
  31. int penyesuaian4 = 150;
  32. int penyesuaian5 = 200;
  33.  
  34. int lampu1;
  35. int lampu2;
  36. int lampu3;
  37. int lampu4;
  38. int lampu5;
  39.  
  40. float v1;
  41. float v2;
  42. float v3;
  43. float v4;
  44. float v5;
  45.  
  46. float i1;
  47. float i2;
  48. float i3;
  49. float i4;
  50. float i5;
  51.  
  52. ACS712  ACS1(A1, 5.0 , 512, 185);
  53. ACS712  ACS2(A2, 5.0, 512, 185);
  54. ACS712  ACS3(A3, 5.0, 512, 185);
  55. ACS712  ACS4(A4, 5.0, 512, 185);
  56. ACS712  ACS5(A5, 5.0, 512, 185);
  57.  
  58.  
  59.  
  60.  
  61. void setup()
  62. {
  63.   Serial.begin(115200);
  64.   client.setServer(mqtt_server, 1883);
  65.   Ethernet.begin(mac, ip);
  66.   // Allow the hardware to sort itself out
  67.   delay(1500);
  68.   lastReconnectAttempt = 0;
  69.  
  70.  
  71.   Serial.print("IP Address Micro: ");
  72.   Serial.println(Ethernet.localIP());
  73.  
  74.  
  75.  
  76.   Serial.println("Mulai");
  77. }
  78.  
  79.  
  80. void loop()
  81. {
  82.   client.loop();
  83.   int hasil1 = ACS1.mA_AC();
  84.   int hasil2 = ACS2.mA_AC();
  85.   int hasil3 = ACS3.mA_AC();
  86.   int hasil4 = ACS4.mA_AC();
  87.   int hasil5 = ACS5.mA_AC();
  88.  
  89.   Serial.print(hasil1); Serial.print("\t");
  90.   Serial.print(hasil2); Serial.print("\t");
  91.   Serial.print(hasil3); Serial.print("\t");
  92.   Serial.print(hasil4); Serial.print("\t");
  93.   Serial.print(hasil5); Serial.println("\t");
  94.  
  95.  
  96.  
  97.   hasil1 = hasil1 + penyesuaian1;
  98.   hasil2 = hasil2 + penyesuaian2;
  99.   hasil3 = hasil3 + penyesuaian3;
  100.   hasil4 = hasil4 + penyesuaian4;
  101.   hasil5 = hasil5 + penyesuaian5;
  102.  
  103.  
  104.   v1 = (hasil1 * 5)/1024;
  105.   v2 = (hasil2 * 5)/1024;
  106.   v3 = (hasil3 * 5)/1024;
  107.   v4 = (hasil4 * 5)/1024;
  108.   v5 = (hasil5 * 5)/1024;
  109.  
  110.   i1 = v1/185;
  111.   i2 = v2/185;
  112.   i3 = v3/185;
  113.   i4 = v4/185;
  114.   i5 = v5/185;
  115.  
  116.   if (hasil1 < 0) hasil1 = 0;
  117.   if (hasil2 < 0) hasil2 = 0;
  118.   if (hasil3 < 0) hasil3 = 0;
  119.   if (hasil4 < 0) hasil4 = 0;
  120.   if (hasil5 < 0) hasil5 = 0;
  121.  
  122.  
  123.  
  124.   if (hasil1 > 900) {
  125.      lampu1 = 1;
  126.   }else {
  127.     lampu1 = 0;
  128.     i1=0;
  129.   }
  130.  
  131.   if (hasil2 > 900) {
  132.      lampu2 = 1;
  133.   }else {
  134.     lampu2 = 0;
  135.     i2=0;
  136.   }
  137.  
  138.   if (hasil3 > 900) {
  139.      lampu3 = 1;
  140.   }else {
  141.     lampu3 = 0;
  142.     i3=0;
  143.   }
  144.  
  145.   if (hasil4 > 900) {
  146.      lampu4 = 1;
  147.   }else {
  148.     lampu4 = 0;
  149.     i4=0;
  150.   }
  151.  
  152.   if (hasil5 > 900) {
  153.      lampu5 = 1;
  154.   }else {
  155.     lampu5 = 0;
  156.     i5=0;
  157.   }
  158.  
  159.  
  160.  
  161.  
  162.   Serial.print(lampu1); Serial.print("\t");
  163.   Serial.print(lampu2); Serial.print("\t");
  164.   Serial.print(lampu3); Serial.print("\t");
  165.   Serial.print(lampu4); Serial.print("\t");
  166.   Serial.print(lampu5); Serial.println("\t");
  167.  
  168.   delay(300);
  169.   if (!client.connected()) {
  170.     long now = millis();
  171.     if (now - lastReconnectAttempt > 5000) {
  172.       lastReconnectAttempt = now;
  173.       // Attempt to reconnect
  174.       if (reconnect()) {
  175.         lastReconnectAttempt = 0;
  176.       }
  177.     }
  178.   } else {
  179.     // Alat berhasil terhubung ke broker
  180.  
  181.     if (!client.connected()) {
  182.       reconnect();
  183.     }
  184.     //Serial.println ("Kirim data ke MQTT Broker");
  185.     client.loop();
  186.     client.publish("approach1/lampu1", String(lampu1).c_str(), true);
  187.     client.publish("approach1/lampu2", String(lampu2).c_str(), true);
  188.     client.publish("approach1/lampu3", String(lampu3).c_str(), true);
  189.     client.publish("approach1/lampu4", String(lampu4).c_str(), true);
  190.     client.publish("approach1/lampu5", String(lampu5).c_str(), true);
  191.  
  192.   client.publish("approach1/arus1", String(i1).c_str(), true);
  193.   client.publish("approach1/arus2", String(i2).c_str(), true);
  194.   client.publish("approach1/arus3", String(i3).c_str(), true);
  195.   client.publish("approach1/arus4", String(i4).c_str(), true);
  196.   client.publish("approach1/arus5", String(i5).c_str(), true);
  197.  
  198.     client.publish("approach1/adc1", String(hasil1).c_str(), true);
  199.     client.publish("approach1/adc2", String(hasil2).c_str(), true);
  200.     client.publish("approach1/adc3", String(hasil3).c_str(), true);
  201.     client.publish("approach1/adc4", String(hasil4).c_str(), true);
  202.     client.publish("approach1/adc5", String(hasil5).c_str(), true);
  203.     delay(100);
  204.    
  205.   }
  206. }
  207. boolean reconnect() {
  208.   if (client.connect("lampu1")) {
  209.     // Ketika Terhubung Kirim Status Lampu Awal = 0
  210.     Serial.println ("Berhasil terhubung ke Broker");  
  211.   }
  212.   return client.connected();
  213. }
Advertisement
Add Comment
Please, Sign In to add comment