'IoT_ESP8266'에 해당되는 글 18건

IoT 할려고 하다가 관심을 가진 Modbus ...

ESP8266 용 Modbus 꾸며서 테스트 하기

 

1. 프로그램 ...

 - 라이브러리 받기 :

https://github.com/emelianov/modbus-esp8266

 

2. HW 구성

3. Modbus 테스트 하기

 - Simulation SW : https://sourceforge.net/projects/qmodmaster/

 

QModMaster

Download QModMaster for free. Modbus TCP,RTU Master Software. QModMaster is a free Qt-based implementation of a ModBus master application. A graphical user interface allows easy communication with ModBus RTU and TCP slaves.

sourceforge.net

- Modbus 서버 IP  주소 설정하기  : Option -> ModbusTCP

-  Base Address 설정하기 : ESP8266 Modbus 라이브러리는 "0" 부터 시작임

   Option -> Settings

 - 이제 Connect 누루고 실행하면 됨

 ==> 또는 Address = "100" 의 뜻은 

        1 부터 시작하는 시스템인 경우 101 번째이고

        0 부터 시작하는 시스템인 경우 100 번째임!!

 

4. ESM Modbus 서버 실행하기 아두이노 SW

// ============================================================================================
// ESP8266 보드 설치하기
// https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/installing-the-esp8266-arduino-addon
// To begin, we’ll need to update the board manager with a custom URL. Open up Arduino, 
// then go to the Preferences (File > Preferences). Then, towards the bottom of the window, 
// copy this URL into the “Additional Board Manager URLs” text box:
//  ==> http://arduino.esp8266.com/stable/package_esp8266com_index.json
// Hit OK. Then navigate to the Board Manager by going to Tools > Boards > Boards Manager. 
// There should be a couple new entries in addition to the standard Arduino boards. 
// Look for esp8266. Click on that entry, then select Install.
//
// 'LIB' should be copied into under 'C:\Users\turbocrazy\Documents\Arduino\libraries'
//
// SSD1306 Lib for ESP8266
// https://randomnerdtutorials.com/esp8266-0-96-inch-oled-display-with-arduino-ide/
// https://github.com/ThingPulse/esp8266-oled-ssd1306   (below is same LIB)
// https://github.com/squix78/esp8266-oled-ssd1306/archive/master.zip
// unzip to : C:\Users\turbocrazy\Documents\Arduino\libraries
// other lib not tested
//
// Note : esp8266 board generic unknown error
// https://arduino-esp8266.readthedocs.io/en/latest/faq/a04-board-generic-is-unknown.html#how-to-fix-it
// 아래 폴더 아래에 있는 오래된 것 삭제 후 재시작
// => C:\Users\turbocrazy\AppData\Local\Arduino15\packages\esp8266\hardware 
//  The blue LED on the ESP-01 - GPIO1 : BLUE = TxD --> GPIO 1, RxD = GPIO 3
//  The blue LED on the Wemos D1 mini ESP-12F - GPIO2
//  10-bit analog ADC. The ADC range is from 0V to 1.0V (0 ... 1023)
// Timer & Ticker Exam : 
//   https://circuits4you.com/2018/01/02/esp8266-timer-ticker-example/
//   'timer 0' is for WiFi, only 'timer 1' can be used
//   However 'Ticker' is prefered due to 'crash'
// Internal SD LIB is used no need to install
// SD exam only : https://www.instructables.com/id/SD-Card-Module-With-ESP8266/
// https://github.com/G6EJD/ESP8266-SD-Card-Reading-Writing/blob/master/ESP8266_D1_MicroSD_Test.ino
// ============================================================================================
// WeMOS D1 Pin connection
// A0 ADC0                TX  /GPIO1
// D0 GPIO16              RS  GPIO3
// D5 GPIO14 SCK          D1  GPIO5 SCL 5        <-- pin no = GPIO no
// D6 GPIO12 MISO         D2  GPIO4 SDA 4
// D7 GPIO13 MOSI         D3  GPIO0     0
// D8 GPIO15 SS           D4  GPIO2     2
// ============================================================================================
//  OLED 연결 : 0x3C address of the OLED, SDA=GPIO0(D3), SCL=GPIO2(D4)
//  SD카드 연결 : SCK,MOSI,MISO,SS 연결 (GPIO15 = SS)
//  Run ESP8266 as server(slave), connect with client(master)
//  Check & confirm starting address! Register offset (base start from 0 or 1)
// ============================================================================================
// 2022.04.23 : OLED/SD/MODBUS
// ============================================================================================
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <ModbusIP_ESP8266.h>     // Open 'Library Manager' and install
#include "SSD1306.h"              // alias for #include "SSD1306Wire.h"
SSD1306  display(0x3c, 0, 2);     // 0x3C address of the OLED, SDA=GPIO 0, SCL=GPIO 2
int BuiltInLED = 2;               // ESP-12 built in BLUE LED at GPIO2
boolean LEDON = false;
#define CS_PIN  15                // GPIO15 = SS
#define LOM     3                 // GPIO 3 = leads off detection LO -
#define LOP     0                 // GPIO 0 = leads off detection LO +
// Allocate Modbus address for the registers - '100' means start address
// "Base address" = 0, so set as this in the Modbus moniter software
// Use qModMaster as a moniter ...
const int SENSOR_IREG = 100;      // Modbus input Registers Offsets + 30000
const int LED_COIL = 100;	        // Modbus Coil Registers Offsets + 00000
const int SWITCH_ISTS = 100;      // Modbus Digital input Registers Offsets + 10000
const int TEST_HREG = 100;        // Modbus Holding Registers Offsets + 40000
const int ledPin = 12;            // GPIO6(D6) = pin no 12
const int switchPin = 15;         // GPIO5(D8) = pin no 15
int swVal = 0;
ModbusIP mb;                     // ModbusIP object 
long ts;
unsigned int aInput;
String aInputVal, stValue;
// -------------------------------------------------------------------------------------
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);      // LED
  pinMode(switchPin, INPUT);      // Switch

  Serial.begin(115200);          // Arduino monitor
  display.init();                 // OLED stuff : Initialise the display.
  display.setFont(ArialMT_Plain_10);

  WiFi.begin("SSID", "비밀번호");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  
  display.drawString(0, 0, WiFi.localIP().toString());
  display.display();        // write the buffer to the display  

  mb.server();              // Start Modbus IP & allocate : Use addIreg() for analog Inputs
  mb.addIreg(SENSOR_IREG);  // Add SENSOR_IREG register - for Analog input
  mb.addCoil(LED_COIL);     // Add Coil 
  mb.addIsts(SWITCH_ISTS);  // Use addIsts() for digital inputs
  mb.addHreg(TEST_HREG, 0xABCD);    // Holding register
  ts = millis();  
  Serial.print("End Setup function ");
}
// -------------------------------------------------------------------------------------
// This ESP is working as server and answer to the client, do what is asked to do 
// Use any Modbus client to interact with
// -------------------------------------------------------------------------------------
void loop() {
  
   mb.task();                     // Call once inside loop() - all magic here
   if (millis() > ts + 2000) {    // Read every two seconds
      ts = millis();
      aInput = analogRead(A0);
      aInputVal = String(aInput);
      // Write value at the address : SENSOR_IREG / LED_COIL / SWITCH_ISTS
      // return the value/info to the client, 
      mb.Ireg(SENSOR_IREG, aInput);           // Setting raw value (0-1024)
      swVal = digitalRead(switchPin);
      mb.Ists(SWITCH_ISTS, swVal);            // update SWITCH_ISTS register with switchPin 
      // The client can write value at 'Coil register'(address = LED_COIL)
      digitalWrite(ledPin, mb.Coil(LED_COIL));// Control ON/OFF ledPin by LED_COIL register value
      if(swVal == 1)
        digitalWrite(ledPin, HIGH);
      else
        digitalWrite(ledPin, LOW);
      // Write status to the OLED display
      display.clear();
      display.setColor(BLACK);
      display.fillRect(0, 0, 127, 63);
      display.display();
      display.setColor(WHITE);
      display.drawString(15, 10, WiFi.localIP().toString());
      display.drawString(15, 20, aInputVal);
      display.drawString(15, 30, "SW is : " + String(swVal));
      display.display();   
      // send back to Arduino IDE
      Serial.print(aInputVal + " -- : ");
      Serial.println("IP : " + WiFi.localIP().toString());
   }
   delay(50);  
}
// ============================================================================================

'IoT_ESP8266' 카테고리의 다른 글

ESP8266 AD8232 - ECG 로거 제작  (0) 2020.02.23
ESP-12 D1 mini OLED SD card  (0) 2020.01.22
ESp-01 : 두개의 다이얼 제어  (0) 2019.01.16
ESP-01 : 서버와 데이터 주고 받기 정리  (0) 2019.01.14
jQuery 사용하기  (0) 2019.01.13
블로그 이미지

DIYworld

,