Components and supplies
3D printed Enclosure
Arduino MKR 485 Shield
ArduiBox MKR
Finder TYPE 7M.24
DIN rail panel mount power supply
Arduino MKR WiFi 1010
Project description
Code
Energy Meter
arduino
1#include "thingProperties.h" 2#include "ArduinoRS485.h" 3#include "ArduinoModbus.h" 4 5#undef ON 6#undef OFF 7 8float current; 9float power; 10float energy; 11 12unsigned long rate = 10000; // Default refresh rate in ms 13unsigned long lastMillis = 0; 14 15void setup() { 16 // Initialize serial and wait for port to open: 17 Serial.begin(9600); 18 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found 19 delay(1500); 20 21 // Defined in thingProperties.h 22 initProperties(); 23 24 // Connect to Arduino IoT Cloud 25 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 26 27 /* 28 The following function allows you to obtain more information 29 related to the state of network and IoT Cloud connection and errors 30 the higher number the more granular information you’ll get. 31 The default is 0 (only errors). 32 Maximum is 4 33 */ 34 setDebugMessageLevel(2); 35 ArduinoCloud.printDebugInfo(); 36 37 // Start Modbus RTU client 38 if (!ModbusRTUClient.begin(19200)) { 39 Serial.println("- Failed to start Modbus RTU Client!"); 40 while (1); 41 } 42 43} 44 45void loop() { 46 ArduinoCloud.update(); 47// Update energy meter data and show it via the Serial Monitor 48 if (millis() - lastMillis > rate) { 49 lastMillis = millis(); 50 51 current = readCurrent(); 52 delay(1200); 53 power = readPower(); 54 delay(1200); 55 energy = readEnergy(); 56 57 Serial.println(String(current) + "A " + String(power) + "watt " + String(energy) + "kWh "); 58 delay(100); 59 60 } 61 62 currentDisplay = current; 63 powerDisplay = power; 64 energyDisplay = energy; 65 66} 67 68/* Functions to read Finder energy meter holding registers 69 * For more information: https://gfinder.findernet.com/public/attachments/7E/EN/PRT_Modbus_7E_64_68_78_86EN.pdf 70 */ 71 72 73float readCurrent() { 74 float current = 0.; 75 // Send reading request over RS485 76 if (!ModbusRTUClient.requestFrom(0x21, INPUT_REGISTERS, 0x7E, 2)) { 77 // Error handling 78 Serial.print("- Failed to read the current! "); 79 Serial.println(ModbusRTUClient.lastError()); 80 } else { 81 // Response handler 82 uint16_t word1 = ModbusRTUClient.read(); // Read word1 from buffer 83 uint16_t word2 = ModbusRTUClient.read(); // Read word2 from buffer 84 //uint32_t millivolt = 1word2 << 16 | word1; // Join word1 and word2 to retrieve voltage value in millivolts 85 current = word2/1000.0; // Convert to volts 86 //Serial.println(word1); 87 //Serial.println(word2 / 10); 88 } 89 90 return current; 91} 92 93float readPower() { 94 float power = 0.; 95 // Send reading request over RS485 96 if (!ModbusRTUClient.requestFrom(0x21, INPUT_REGISTERS, 0x8C, 2)) { 97 // Error handling 98 Serial.print("- Failed to read the power! "); 99 Serial.println(ModbusRTUClient.lastError()); 100 } else { 101 // Response handler 102 int16_t word1 = ModbusRTUClient.read(); // Read word1 from buffer 103 int16_t word2 = ModbusRTUClient.read(); // Read word2 from buffer 104 //uint32_t millivolt = 1word2 << 16 | word1; // Join word1 and word2 to retrieve voltage value in millivolts 105 power = word2/10.0; // Convert to volts 106 //Serial.println(word1); 107 //Serial.println(word2 / 10); 108 } 109 110 return power; 111} 112 113float readEnergy() { 114 float energy = 0.; 115 // Send reading request over RS485 116 if (!ModbusRTUClient.requestFrom(0x21, INPUT_REGISTERS, 0xAC0, 2)) { 117 // Error handling 118 Serial.print("- Failed to read the energy! "); 119 Serial.println(ModbusRTUClient.lastError()); 120 } else { 121 // Response handler 122 //short word1 = ModbusRTUClient.read(); // Read word1 from buffer 123 float word2 = ModbusRTUClient.read(); // Read word2 from buffer 124 //uint32_t millivolt = 1word2 << 16 | word1; // Join word1 and word2 to retrieve voltage value in millivolts 125 energy = word2/10.0; // Convert to volts 126 //Serial.println(word1); 127 //Serial.println(word2 / 10); 128 } 129 130 return energy / 10000; 131}
Downloadable files
Setup
Setup
Documentation
MKR DIN Case
MKR DIN Case
Comments
Only logged in users can leave comments