我将 ESP-01 板作为 STA MODE(站)连接到路由器 WIFI(接入点)。
我在互联网上发现了有趣的库 (set
timino.h) 允许我们连接 ESP 和 PLC。
我通过以太网电缆将 S7-1200 连接到此路由器,并在 PLC 端启用了可访问选项 PUT-GET
通信。
当 ESP 尝试调用 DB 读取函数时,ESP 复位。我不知道为什么。有人有同样的问题吗?
我在下面展示了所有代码和 ESP 调试器的一些屏幕。问题是什么?!
代码:
全选/*
ESP8266 Blink by Simon Peter
Blink the blue LED on the ESP-01 module
This example code is in the public domain
The blue LED on the ESP-01 module is connected to GPIO1
(which is also the TXD pin; so we cannot use Serial.print() at the same time)
Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/
// Wifi -> #define S7WIFI
// Cable -> #define S7WIRED
#define S7WIFI
#include
#include "Settimino.h"
// byte mac[] = { 0xE0, 0xDC, 0xA0, 0xE3, 0xF3, 0x3C };
char* ssid = "SteviaAutomation_2.4G";
char* password = "Stevia\"Automation\"!";
IPAddress ip(192,168,1,99); // Local Address
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
IPAddress PLC(192,168,1,199); // PLC Address
byte Buffer[512];
S7Client * Client = new S7Client();
unsigned long Elapsed; // To calc the execution time
void setup()
{
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Start the WiFi Library
WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
bool Connect()
{
int Result=Client->ConnectTo(PLC,
0, // Rack (see the doc.)
0); // Slot (see the doc.)
Serial.print("Connecting to ");Serial.println(PLC);
if (Result==0)
{
Serial.print("Connected ! PDU Length = ");
Serial.println(Client->GetPDULength());
}
else
Serial.println("Connection error");
return Result==0;
}
void CheckError(int ErrNo)
{
Serial.print("Error No. 0x");
Serial.println(ErrNo, HEX);
// Checks if it's a Server Error => we need to disconnect
if (ErrNo & 0x00FF)
{
Serial.println("SERVER ERROR, disconnecting.");
Client->Disconnect();
}
}
byte ReadDB(int StartAddress) // get specific bytes from SIMATIC DB block
{
int Result;
byte Receive;
// Connecting...
while (!Client->Connected)
{
if (!Connect())
delay(500);
}
// call read area from SIMATIC
Result=Client->ReadArea(S7AreaDB, // DB identifier
2, // DB Number = 53
StartAddress, // Start Address
1, // Number of bytes to read
&Receive); // Pointer to Destination Area
// data overwriting (for simulation purposes)
//Result = 0;
//Receive = 0x00;
if (Result==0)
{
return Receive;
}
else
CheckError(Result);
}
void loop()
{
Serial.print("Data from the PLC: ");
Serial.println(ReadDB(2)); // Read one byte from the 2.0 start address
Serial.println("");
delay(500);
}
0