Advertisement
mudhita_triari

ESP32 Blynk - Modbus + Relay

Apr 24th, 2024 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | Source Code | 0 0
  1. #define BLYNK_TEMPLATE_ID "TMPLQrthTrzE"
  2. #define BLYNK_TEMPLATE_NAME "Sparing"
  3.  
  4. #include <WiFi.h>
  5. #include <WiFiClient.h>
  6. #include <BlynkSimpleEsp32.h>
  7. #include <ModbusMaster.h>
  8. #include <SoftwareSerial.h>
  9.  
  10. #define MAX485_RE_NEG   5
  11. #define MAX485_DE       4
  12. #define SSERIAL_RX_PIN  16
  13. #define SSERIAL_TX_PIN  17
  14.  
  15. #define RELAY_PIN_1     2
  16. #define RELAY_PIN_2     19
  17. #define RELAY_PIN_3     21
  18. #define RELAY_PIN_4     22
  19.  
  20. #define RELAY_VPIN_1    V0
  21. #define RELAY_VPIN_2    V1
  22. #define RELAY_VPIN_3    V2
  23. #define RELAY_VPIN_4    V3
  24.  
  25. #define NUM_SENSORS     14
  26.  
  27. const char* ssid      = "vivo V29";
  28. const char* password  = "Nabila041185";
  29. char auth[]           = "QKtR_TmPYgDGn3cVlbJiffJArdScp3cC";
  30.  
  31. SoftwareSerial mySerial(SSERIAL_RX_PIN, SSERIAL_TX_PIN); // RX, TX
  32. ModbusMaster node;
  33.  
  34. struct Sensor {
  35.   const char* name;
  36.   uint16_t registerAddress;
  37.   uint16_t registerIndex;
  38.   uint8_t vPin;
  39. };
  40.  
  41. Sensor sensors[NUM_SENSORS] = {
  42.   {"PV Input Voltage (V)",         0x3100, 0, V4},
  43.   {"PV Current (A)",               0x3100, 1, V5},
  44.   {"Battery Voltage (V)",          0x3100, 4, V6},
  45.   {"Battery Charge Current (A)",   0x3100, 5, V7},
  46.   {"PV Power (W)",                 0x3100, 2, V8},
  47.   {"Battery Charge Power (W)",     0x3100, 6, V9},
  48.   {"Load Power (W)",               0x310E, 0, V10},
  49.   {"Load Voltage (V)",             0x310C, 0, V11},
  50.   {"Load Current (A)",             0x310D, 0, V12},
  51.   {"Total Generated Energy (kWh)", 0x3312, 0, V13},
  52.   {"Battery SOC Percentage (%)",   0x311A, 0, V14},
  53.   {"Battery Temp (C deg)",         0x3110, 0, V15},
  54.   {"MPPT Inside Case Temp (C deg)",0x3110, 1, V16},
  55.   {"MPPT Component Temp (C deg)",  0x3110, 2, V17}
  56. };
  57.  
  58. unsigned long lastSensorReadTime = 0;
  59. const unsigned long sensorReadInterval = 10000;
  60.  
  61. void preTransmission() {
  62.   digitalWrite(MAX485_RE_NEG, 1);
  63.   digitalWrite(MAX485_DE, 1);
  64. }
  65.  
  66. void postTransmission() {
  67.   digitalWrite(MAX485_RE_NEG, 0);
  68.   digitalWrite(MAX485_DE, 0);
  69. }
  70.  
  71. void setup() {
  72.   pinMode(MAX485_RE_NEG, OUTPUT);
  73.   pinMode(MAX485_DE, OUTPUT);
  74.   digitalWrite(MAX485_RE_NEG, 0);
  75.   digitalWrite(MAX485_DE, 0);
  76.  
  77.   pinMode(RELAY_PIN_1, OUTPUT);
  78.   pinMode(RELAY_PIN_2, OUTPUT);
  79.   pinMode(RELAY_PIN_3, OUTPUT);
  80.   pinMode(RELAY_PIN_4, OUTPUT);
  81.  
  82.   Serial.begin(9600);
  83.   mySerial.begin(9600);
  84.   node.begin(1, mySerial);
  85.   node.preTransmission(preTransmission);
  86.   node.postTransmission(postTransmission);
  87.  
  88.   WiFi.begin(ssid, password);
  89.   while (WiFi.status() != WL_CONNECTED) {
  90.     delay(1000);
  91.     Serial.println("Connecting to WiFi...");
  92.   }
  93.   Serial.println("Connected to WiFi");
  94.  
  95.   Blynk.begin(auth, ssid, password);
  96. }
  97.  
  98. void readSensors() {
  99.   float values[NUM_SENSORS];
  100.   node.clearResponseBuffer();
  101.   for (int i = 0; i < NUM_SENSORS; i++) {
  102.     uint8_t result = node.readInputRegisters(sensors[i].registerAddress + sensors[i].registerIndex, 1);
  103.     if (result == node.ku8MBSuccess) {
  104.       values[i] = node.getResponseBuffer(0x00) / 100.0f;
  105.       Blynk.virtualWrite(sensors[i].vPin, values[i]);
  106.       Serial.print(sensors[i].name);
  107.       Serial.print(": ");
  108.       Serial.println(values[i]);
  109.     } else {
  110.       Serial.print("Error reading ");
  111.       Serial.print(sensors[i].name);
  112.       Serial.print(" : ");
  113.       Serial.println(result);
  114.     }
  115.   }
  116. }
  117.  
  118. BLYNK_WRITE(RELAY_VPIN_1) {
  119.   int state = param.asInt();
  120.   digitalWrite(RELAY_PIN_1, state);
  121. }
  122.  
  123. BLYNK_WRITE(RELAY_VPIN_2) {
  124.   int state = param.asInt();
  125.   digitalWrite(RELAY_PIN_2, state);
  126. }
  127.  
  128. BLYNK_WRITE(RELAY_VPIN_3) {
  129.   int state = param.asInt();
  130.   digitalWrite(RELAY_PIN_3, state);
  131. }
  132.  
  133. BLYNK_WRITE(RELAY_VPIN_4) {
  134.   int state = param.asInt();
  135.   digitalWrite(RELAY_PIN_4, state);
  136. }
  137.  
  138. void loop() {
  139.   Blynk.run();
  140.  
  141.   unsigned long currentMillis = millis();
  142.   if (currentMillis - lastSensorReadTime >= sensorReadInterval) {
  143.     readSensors();
  144.     lastSensorReadTime = currentMillis;
  145.   }
  146. }
  147.  
Tags: #esp32 #IOT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement