Arduino#ESP-WROOM-02 with Modbus TCP Slave

This time I will use ESP-WROOM-02 modules to connect with Arduino, and then communicate with my pc by using ModbusTCP/IP.

Before

Please install the Arduino ESP Library using the below link:

Arduino ESP8266 library

https://github.com/itead/ITEADLIB_Arduino_WeeESP8266/blob/master/README.md

Do not forget to install the Modbus TCP/IP library also:)

https://github.com/andresarmento/modbus-arduino

I will use Arduino this time, So SoftwareSerial is my option.

Because we are using SoftwareSerial, you need to comment out a line inside the ESP8266.h.

just like this:

ESP-WROOM-02 Parametres Setup

Change to Station Mode(Client)

AT+CWMODE_CUR=1

OK

Connection Test to My AP

AT+CWJAP_CUR=”myAP”,”mypassword”

WIFI CONNECTED

WIFI GOT IP

OK

refer link

https://trac.switch-science.com/wiki/ESP-WROOM-02_AT

Arduino Side

#include <SoftwareSerial.h>
#include “ESP8266.h”
#include <Modbus.h>
#include <ModbusIP_ESP8266AT.h>

SoftwareSerial mySerial(2,3); // RX, TX
ESP8266 wifi(mySerial, 115200);

//Modbus Registers Offsets (0-9999)
const int R1 = 100;
const int R2 = 101;
//Used Pins
const int sensorPin = A0;

//ModbusIP object
ModbusIP mb;


void setup()

{
   
    //Config Modbus IP
    mb.config(wifi, “MYPC1”, “Password”);
    //Set ledPin mode
    mb.addIreg(R1);
    mb.addIreg(R2);
}

void loop()
{

  //Call once inside loop() – all magic here
  mb.task();
  mb.Ireg(R1,100);
  mb.Ireg(R2,200);

}

While setting is matched with  mb.config(wifi, “MYPC1”, “Password”) to your AP, your module will automatically connect to the AP.

Python Side

We are using this library.

https://pypi.org/project/pyModbusTCP/

from pyModbusTCP.client import ModbusClient
import time

SERVER_HOST = “192.168.137.5”
SERVER_PORT = 502
SERVER_U_ID = 2

c = ModbusClient()
c.open()
c.is_open()
time.sleep(1)
regs = c.read_input_registers(100,2)
print(regs)
c.close()

Result

[100, 200]

Sample Code

https://github.com/soup01Threes

Footer_Basic

Please Support some devices for my blog

Amazon Gift List

Find ME

Twitter:@3threes2
Email:soup01threes*gmail.com (* to @)
YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

シェアする

  • このエントリーをはてなブックマークに追加

フォローする