Hello everyone, i am working on MODBUS RTU and TCP/IP communication for displaying inner temperature value of pyrometer device, but i am not getting desired output value. However, I am not familiar working with modbus so far and would really like to get advice on my program.
Thanks in advance!
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <ModbusIP_ESP8266.h>
#include <ModbusRTU.h>
#include <Adafruit_SSD1306.h>
#include "Wire.h">
int count=0;
String s="";
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int REG = 02; // Modbus Hreg Offset
IPAddress remote(01); // Address of Modbus Slave device
const int LOOP_COUNT = 10;
ModbusIP mb; //ModbusIP object
void setup() {
Serial.begin(9600);
WiFi.begin("FRITZ!Box 7490", "387123956222");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: 192.168.178.115");
Serial.println(WiFi.localIP());
mb.client();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
// mb.println("SSD1306 allocation ok");
for(;;);
count = 0;
}
uint16_t res[0];
uint8_t show = LOOP_COUNT;
void loop() {
if (mb.isConnected(remote)) { // Check if connection to Modbus Slave is established
mb.readHreg(remote,REG, res); // Initiate Read Coil from Modbus Slave
mb.task(); // Common local Modbus task
}
delay(100); // Pulling interval
if (!show--) { // Display Slave register value one time per second (with default settings)
count++;
s = "("+String(count)+") = ";
s += String ((res[0]));
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Ti: "+s+" C \n");
display.display();
yield();
}
}