Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLYNK_TEMPLATE_ID "TMPLQrthTrzE"
- #define BLYNK_TEMPLATE_NAME "Sparing"
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h>
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- #define MAX485_RE_NEG 5
- #define MAX485_DE 4
- #define SSERIAL_RX_PIN 16
- #define SSERIAL_TX_PIN 17
- #define RELAY_PIN_1 2
- #define RELAY_PIN_2 19
- #define RELAY_PIN_3 21
- #define RELAY_PIN_4 22
- #define RELAY_VPIN_1 V0
- #define RELAY_VPIN_2 V1
- #define RELAY_VPIN_3 V2
- #define RELAY_VPIN_4 V3
- #define NUM_SENSORS 14
- const char* ssid = "vivo V29";
- const char* password = "Nabila041185";
- char auth[] = "QKtR_TmPYgDGn3cVlbJiffJArdScp3cC";
- SoftwareSerial mySerial(SSERIAL_RX_PIN, SSERIAL_TX_PIN); // RX, TX
- ModbusMaster node;
- struct Sensor {
- const char* name;
- uint16_t registerAddress;
- uint16_t registerIndex;
- uint8_t vPin;
- };
- Sensor sensors[NUM_SENSORS] = {
- {"PV Input Voltage (V)", 0x3100, 0, V4},
- {"PV Current (A)", 0x3100, 1, V5},
- {"Battery Voltage (V)", 0x3100, 4, V6},
- {"Battery Charge Current (A)", 0x3100, 5, V7},
- {"PV Power (W)", 0x3100, 2, V8},
- {"Battery Charge Power (W)", 0x3100, 6, V9},
- {"Load Power (W)", 0x310E, 0, V10},
- {"Load Voltage (V)", 0x310C, 0, V11},
- {"Load Current (A)", 0x310D, 0, V12},
- {"Total Generated Energy (kWh)", 0x3312, 0, V13},
- {"Battery SOC Percentage (%)", 0x311A, 0, V14},
- {"Battery Temp (C deg)", 0x3110, 0, V15},
- {"MPPT Inside Case Temp (C deg)",0x3110, 1, V16},
- {"MPPT Component Temp (C deg)", 0x3110, 2, V17}
- };
- unsigned long lastSensorReadTime = 0;
- const unsigned long sensorReadInterval = 10000;
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- pinMode(RELAY_PIN_1, OUTPUT);
- pinMode(RELAY_PIN_2, OUTPUT);
- pinMode(RELAY_PIN_3, OUTPUT);
- pinMode(RELAY_PIN_4, OUTPUT);
- Serial.begin(9600);
- mySerial.begin(9600);
- node.begin(1, mySerial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- Blynk.begin(auth, ssid, password);
- }
- void readSensors() {
- float values[NUM_SENSORS];
- node.clearResponseBuffer();
- for (int i = 0; i < NUM_SENSORS; i++) {
- uint8_t result = node.readInputRegisters(sensors[i].registerAddress + sensors[i].registerIndex, 1);
- if (result == node.ku8MBSuccess) {
- values[i] = node.getResponseBuffer(0x00) / 100.0f;
- Blynk.virtualWrite(sensors[i].vPin, values[i]);
- Serial.print(sensors[i].name);
- Serial.print(": ");
- Serial.println(values[i]);
- } else {
- Serial.print("Error reading ");
- Serial.print(sensors[i].name);
- Serial.print(" : ");
- Serial.println(result);
- }
- }
- }
- BLYNK_WRITE(RELAY_VPIN_1) {
- int state = param.asInt();
- digitalWrite(RELAY_PIN_1, state);
- }
- BLYNK_WRITE(RELAY_VPIN_2) {
- int state = param.asInt();
- digitalWrite(RELAY_PIN_2, state);
- }
- BLYNK_WRITE(RELAY_VPIN_3) {
- int state = param.asInt();
- digitalWrite(RELAY_PIN_3, state);
- }
- BLYNK_WRITE(RELAY_VPIN_4) {
- int state = param.asInt();
- digitalWrite(RELAY_PIN_4, state);
- }
- void loop() {
- Blynk.run();
- unsigned long currentMillis = millis();
- if (currentMillis - lastSensorReadTime >= sensorReadInterval) {
- readSensors();
- lastSensorReadTime = currentMillis;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement