Skip to content

Commit a643efa

Browse files
committed
Example for Ethernet added
1 parent 7742ce5 commit a643efa

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

examples/Ethernet/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Example with Ethernet (no WIFI)
2+
3+
LAN8720 is used as Ethernet PHY.
4+
Please adjust the "ETH_xxxx" defines to fit for your hardware

examples/Ethernet/main.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <ETH.h>
2+
#include <EspAdsLib.h>
3+
4+
// The SrcAmsAddr can be set here. But we will change it to the current
5+
// IP with the function SrcAmsAddr.Change() in the setup() function
6+
ADS::AmsAddr SrcAmsAddr((char*)"1.1.1.1.1.1", 43609); // Dummy
7+
8+
// Set AMS Addr of the TwinCAT machine here
9+
ADS::AmsAddr DestAmsAddr((char*)"5.16.3.178.1.1", AMSPORT_R0_PLC_TC3);
10+
ADS::Ads Ads;
11+
12+
// Set IP/Name of your PLC
13+
char IpOfPlc[] = "192.168.0.42";
14+
15+
// Set ethernet phy configuration for your board
16+
#define ETH_ADDR 1
17+
#define ETH_RESET 12
18+
#define ETH_MDC_PIN 23
19+
#define ETH_MDIO_PIN 18
20+
#define ETH_TYPE ETH_PHY_LAN8720
21+
#define ETH_CLOCK_MODE ETH_CLOCK_GPIO17_OUT
22+
23+
void setup()
24+
{
25+
String PlcIp;
26+
String Ip;
27+
28+
// Serial
29+
Serial.begin(115200);
30+
Serial.println("Starting up ...");
31+
32+
// Ethernet
33+
ETH.begin(ETH_ADDR, ETH_RESET, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLOCK_MODE);
34+
ETH.setHostname("Hostname: ESP_ADS");
35+
//ETH.config(local_ip, gateway, subnet, dns1, dns2); // Static IP, leave without this line to get IP via DHCP
36+
while(!((uint32_t)ETH.localIP())){}; // Waiting for IP from DHCP
37+
38+
Ip = ETH.localIP().toString();
39+
Serial.println("IP: " + Ip);
40+
41+
Ip = Ip + ".1.1";
42+
SrcAmsAddr.Change((char*)Ip.c_str(), 43609);
43+
Serial.println("SrcAmsAdr: " + Ip);
44+
45+
Ads.SetAddr(&SrcAmsAddr, &DestAmsAddr, IpOfPlc);
46+
Ads.Connect();
47+
Serial.println("ADS connected");
48+
}
49+
50+
51+
uint16_t u16MyVar = 0;
52+
void loop()
53+
{
54+
Serial.println("Write PLC variable: " + String(u16MyVar));
55+
Ads.WritePlcVarByName("Main.u16MyVar", &u16MyVar, sizeof(uint16_t));
56+
57+
Ads.ReadPlcVarByName("Main.u16MyVar", &u16MyVar, sizeof(uint16_t));
58+
Serial.println("Read PLC variable: " + String(u16MyVar));
59+
60+
u16MyVar++;
61+
delay(2000);
62+
}

0 commit comments

Comments
 (0)