/*
* Author: Niklas Menke <www.niklas-menke.de>
*
* Project: power analyser
* Description: This project represent electrical parameters from a multifunction power analyser on a website.
* The electrical parameters are recorded by a Janitza UMG96S multifunction power analyser.
* An Arduino queries the data via MODBUS RTU and sends it to a database.
* The website loads the parameters from the database and updates itself automatically via AJAX.
*
* File: powerAnalyser.ino
* Description: Get the electrical parameters from the Janitza UMG96S with MODBUS RTU and send it to the server.
* Date: 2020/07/09
*
* Version: WORK
* Date: -
*/
// --- Include Libraries ---
#include <ModbusMaster.h>
#include <SPI.h> // Needed for Ethernet.h
#include <Ethernet.h>
// --- ---
// --- Parameter and function declaration ---
// Pins for the state LEDs
uint8_t errorLED = 9; // Error-LED
uint8_t okLED = 8; // OK-LED
uint8_t processLED = 10; // Process-LED
// RS232/TTL-converter
uint8_t ser1Pow = 6; // voltage source for the converter with the DSUB jack
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address
char serverAddress[] = "REPLACED"; // Receiver address
char serverPath[] = "REPLACED"; // Path to the script that processes the request
void errorMessage(uint8_t rep, uint8_t clk); // Send an optical error message
String getParameters(); // Get the electrical parameters from the power analyser
// --- ---
// --- Instantiate objects ---
ModbusMaster powerAnalyser; // Modbus
EthernetClient receiveServer; // Ethernet
// --- ---
// --- Initialization ---
void setup() {
// Pin initialization
pinMode(errorLED,OUTPUT);
pinMode(okLED,OUTPUT);
pinMode(processLED,OUTPUT);
pinMode(ser1Pow,OUTPUT);
// Turn LEDs on to show the initialization process
digitalWrite(errorLED,HIGH);
digitalWrite(okLED,HIGH);
digitalWrite(processLED,HIGH);
// Modbus RTU
// WARNING: The baud rate and the device address have to pass with the setting of the power analyser
Serial1.begin(9600, SERIAL_8N2); // Start serial port for the Modbus (baudrate: 9600, data bits: 8, parity: none, stopp bits: 2) -- Don't change the databits or parity
powerAnalyser.begin(1, Serial1); // Start Modbus over serial port 1 -- Address of the power analyser: 1
// Ethernet
if(!Ethernet.begin(mac)) {
if(Ethernet.hardwareStatus() == EthernetNoHardware) while(1) errorMessage(1,2); // Can't find Etherned-Board
while(1) errorMessage(1,3); // Other fault
}
delay(2000); // Initialization time
digitalWrite(errorLED,LOW);
digitalWrite(processLED,LOW);
} // setup()
// --- Main function ---
void loop() {
digitalWrite(okLED,HIGH); // Turn on OK-LED in case of a previous error
// Connect with the server
if(receiveServer.connect(serverAddress, 80)) { // If successful send POST-request
digitalWrite(processLED,HIGH);
String parameters = getParameters();
receiveServer.print("POST ");
receiveServer.print(serverPath);
receiveServer.println(" HTTP/1.1");
receiveServer.print("Host: ");
receiveServer.println(serverAddress);
receiveServer.println("Authorization: Basic REPLACED");
receiveServer.println("Content-type: application/x-www-form-urlencoded");
receiveServer.print("Content-length: ");
receiveServer.println(parameters.length());
receiveServer.println("Connection: close");
receiveServer.println();
receiveServer.println(parameters);
digitalWrite(processLED,LOW);
}
else { // If fail
errorMessage(1,4);
}
//Serial.println(getParameters());
delay(10000); // Do this ervery 10 seconds
} // loop()
// Send an optical error message
void errorMessage(uint8_t rep, uint8_t clk) {
digitalWrite(okLED,LOW);
digitalWrite(processLED,LOW);
for(uint8_t i = 0; i < rep; i++) {
for(uint8_t j = 0; j < clk; j++) {
digitalWrite(errorLED,HIGH);
delay(200);
digitalWrite(errorLED,LOW);
delay(200);
}
delay(500);
}
} // errorMessage()
// Get the electrical parameters from the power analyser
String getParameters() {
digitalWrite(ser1Pow,HIGH);
digitalWrite(processLED,HIGH);
String data = "values="; //"values=230;231;232;400;401;402;300;301;302;400;401;402;500;501;502;20;21;22;30;31;32;2;3;4;5;6;7;50;40;1;200;600;700;800;";
// Get first data group
if(powerAnalyser.readHoldingRegisters(0xC8,21) == powerAnalyser.ku8MBSuccess) {
for(uint8_t i = 0; i < 21; i++) {
data = data + powerAnalyser.getResponseBuffer(i);
data = data + ";";
}
} else { // Can't get parameters
errorMessage(1,5);
}
// Get second data group
if(powerAnalyser.readHoldingRegisters(0x10D,13) == powerAnalyser.ku8MBSuccess) {
for(uint8_t i = 0; i < 13; i++) {
data = data + powerAnalyser.getResponseBuffer(i);
data = data + ";";
}
} else { // Can't get parameters
errorMessage(1,6);
}
digitalWrite(processLED,LOW);
digitalWrite(ser1Pow,LOW);
return data;
} // getParameters()