Fogyasztásmérő olvasása RS485 segítségével. SW-t igénylő kód Arduinohoz.
Videós tartalom: https://youtu.be/a1_RJxchlig
Kód, ami az előző képen látható mérővel megy (pirossajljelöltem a képen a típust)
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define TFT_SZELSEG 128
#define TFT_MAGASSAG 160
#define MODBUS_DIR_PIN 5 // connect DR, RE pin of MAX485
#include <ModbusMaster.h>
uint16_t addr=0x0000;
uint16_t addr_b=0x0000;
int i=0;
float x=0;
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
ModbusMaster node;
void modbusPreTransmission()
{
delay(500);
digitalWrite(MODBUS_DIR_PIN, HIGH);
}
// Pin 4 made low for Modbus receive mode
void modbusPostTransmission()
{
digitalWrite(MODBUS_DIR_PIN, LOW);
delay(500);
}
void setup()
{
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST7735_WHITE);
tft.setTextSize(2);
tft.setTextColor(ST7735_BLUE);
pinMode(LED_BUILTIN, OUTPUT);
// use Serial (port 0); initialize Modbus communication baud rate
Serial.begin(9600);
//Serial.begin(9600, SERIAL_8E1);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(MODBUS_DIR_PIN, OUTPUT);
digitalWrite(MODBUS_DIR_PIN, LOW);
// communicate with Modbus slave ID 2 over Serial (port 0)
node.begin(1, Serial);
node.preTransmission(modbusPreTransmission);
node.postTransmission(modbusPostTransmission);
}
void loop()
{
uint8_t result;
uint16_t data[6];
// Read 24 registers starting at addr)
result = node.readInputRegisters(addr, 24);
if (result == node.ku8MBSuccess)
{
tft.setTextColor(ST7735_BLUE);
tft.setTextSize(2);
tft.fillRect(0, 0, TFT_SZELSEG, TFT_MAGASSAG, ST7735_WHITE);
tft.setCursor(15, 5);
x=node.getResponseBuffer(0x0000);
tft.print(x/10 , 1);
tft.print(" V");
tft.setCursor(15, 30);
x=node.getResponseBuffer(0x0003);
tft.print(x/100 , 3);
tft.print(" A");
tft.setCursor(15, 55);
tft.print(node.getResponseBuffer(0x0007));
tft.print(" VA");
tft.setCursor(15, 80);
tft.print(node.getResponseBuffer(0x0008));
tft.print(" W");
tft.setCursor(5, 105);
tft.setTextSize(1);
tft.setTextColor(ST7735_RED);
x=node.getResponseBuffer(0x0014);
tft.print("F: ");
tft.print(x/1000 , 3);
result = node.readInputRegisters(addr+26, 24);
if (result == node.ku8MBSuccess)
{
//tft.setCursor(5, 130);
tft.print(" ");
x=node.getResponseBuffer(0x0000);
tft.print(x/100 , 1);
tft.print(" Hz");
tft.setCursor(5, 119);
tft.print("Total: ");
x=node.getResponseBuffer(0x0004);
tft.print(x/100 , 2);
tft.print(" kWh");
tft.setCursor(5, 133);
tft.print("Export: ");
x=node.getResponseBuffer(0x0000+14);
tft.print(x/100 , 2);
tft.print(" kWh");
result = node.readInputRegisters(0x0032, 1);
if (result == node.ku8MBSuccess)
{
tft.setCursor(5, 148);
tft.print("Import: ");
x=node.getResponseBuffer(0x0000);
tft.print(x/100 , 2);
tft.print(" kWh");
}
}
}
delay(10000);
}