Skip to content

Commit 4fe5a2c

Browse files
committed
init
0 parents  commit 4fe5a2c

File tree

11 files changed

+1007
-0
lines changed

11 files changed

+1007
-0
lines changed

LICENSE

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
## EthernetEspAT library
3+
4+
This library creates standard Arduino Ethernet networking API over ESP8266 or ESP32 AT commands. The Arduino Ethernet networking API was established by Arduino Ethernet library.
5+
6+
EthernetEspAT extends the [WiFiEspAT](https://github.com/JAndrassy/WiFiEspAT) library with Ethernet object and Ethernet named aliases for WiFiClient, WiFiUDP and WiFiServer from WiFiEspAT.
7+
8+
The library is for all Arduino MCU architectures. It enables secure connection (https) over wired network even for 8-bit architecture boards.
9+
10+
### AT firmware with Ethernet support
11+
12+
Standard AT firmware builds by Espressif don't support Ethernet.
13+
14+
ESP32 AT firmware supports Ethernet commands but they have to be [enabled](https://docs.espressif.com/projects/esp-at/en/latest/esp32/Compile_and_Develop/How_to_enable_ESP_AT_Ethernet.html) in build configuration and build from sources.
15+
16+
The ESP32 AT firmware source only supports Ethernet PHY modules: LAN8720, IP101, DP83848 and RTL8201. A modified AT firmware supporting W5500 will be available soon.
17+
18+
ESP8266 AT firmware by Espressif doesn't support Ethernet. Jiri Bilek's [ESP_ATMod firmware](https://github.com/JiriBilek/ESP_ATMod#description) for ESP8266 will support Ethernet with Ethernet chips supported by the ESP8266 Arduino platform.
19+
20+
* [ESP32 AT firmware binaries with Ethernet support](https://github.com/Networking-for-Arduino/EthernetEspAT/wiki/ESP32-firmware)
21+
* [esp8266 ESP_ATMod sketch with Ethernet support](https://github.com/JAndrassy/ESP_ATMod/tree/ethernet_support)
22+
23+
For installing and testing the AT firmware see the WiFiEspAT [documentation](https://github.com/JAndrassy/WiFiEspAT?tab=readme-ov-file#getting-started).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#include <EthernetEspAT.h>
3+
4+
// Emulate Serial1 on pins 6/7 if not present
5+
#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1)
6+
#include <SoftwareSerial.h>
7+
SoftwareSerial Serial1(6, 7); // RX, TX
8+
#define AT_BAUD_RATE 9600
9+
#else
10+
#define AT_BAUD_RATE 115200
11+
#endif
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
while (!Serial);
16+
17+
Serial1.begin(AT_BAUD_RATE);
18+
if (!Ethernet.init(Serial1)) {
19+
Serial.println();
20+
Serial.println("Communication with the network module failed!");
21+
// don't continue
22+
while (true);
23+
}
24+
25+
Ethernet.wifiOff(true);
26+
27+
}
28+
29+
void loop() {
30+
Ethernet.maintain();
31+
}

examples/WebClient/WebClient.ino

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Web client
3+
4+
This sketch connects to a website (http://arduino.cc)
5+
using the Ethernet module.
6+
7+
created 13 July 2010
8+
by dlf (Metodo2 srl)
9+
modified 31 May 2012
10+
by Tom Igoe
11+
modified in Jul 2019 for WiFiEspAT library
12+
modified in Jul 2024 for EthernetEspAT library
13+
by Juraj Andrassy https://github.com/jandrassy
14+
*/
15+
16+
#include <EthernetEspAT.h>
17+
18+
// Emulate Serial1 on pins 6/7 if not present
19+
#if defined(ARDUINO_ARCH_AVR) && !defined(HAVE_HWSERIAL1)
20+
#include <SoftwareSerial.h>
21+
SoftwareSerial Serial1(6, 7); // RX, TX
22+
#define AT_BAUD_RATE 9600
23+
#else
24+
#define AT_BAUD_RATE 115200
25+
#endif
26+
27+
const char* server = "arduino.tips";
28+
29+
// Set the static IP address to use if the DHCP fails to assign
30+
IPAddress ip(192, 168, 0, 177);
31+
32+
EthernetClient client;
33+
34+
void setup() {
35+
Serial.begin(115200);
36+
while (!Serial);
37+
38+
Serial1.begin(AT_BAUD_RATE);
39+
Ethernet.init(Serial1);
40+
41+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
42+
Serial.println();
43+
Serial.println("Communication with the network module failed!");
44+
// don't continue
45+
while (true);
46+
}
47+
48+
// start the Ethernet connection:
49+
Serial.println("Initialize Ethernet with DHCP:");
50+
if (Ethernet.begin() == 0) {
51+
Serial.println("Failed to configure Ethernet using DHCP");
52+
if (Ethernet.linkStatus() == LinkOFF) {
53+
Serial.println("Ethernet cable is not connected.");
54+
}
55+
// try to configure using IP address instead of DHCP:
56+
Ethernet.begin(ip);
57+
} else {
58+
Serial.print(" DHCP assigned IP ");
59+
Serial.println(Ethernet.localIP());
60+
}
61+
62+
Serial.println("Starting connection to server...");
63+
if (client.connect(server, 80)) {
64+
Serial.println("connected to server");
65+
66+
client.println("GET /asciilogo.txt HTTP/1.1");
67+
client.print("Host: ");
68+
client.println(server);
69+
client.println("Connection: close");
70+
client.println();
71+
client.flush();
72+
}
73+
}
74+
75+
void loop() {
76+
77+
// if there are incoming bytes available
78+
// from the server, read them and print them
79+
while (client.available()) {
80+
char c = client.read();
81+
Serial.write(c);
82+
}
83+
84+
// if the server's disconnected, stop the client
85+
if (!client.connected()) {
86+
Serial.println();
87+
Serial.println("disconnecting from server.");
88+
client.stop();
89+
90+
// do nothing forevermore
91+
while (true);
92+
}
93+
}

library.properties

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name=EthernetEspAT
2+
version=1.0.0
3+
author=Juraj Andrassy
4+
maintainer=Juraj Andrassy <[email protected]>
5+
sentence=Enables wired (secure) network connection with esp8266 or esp32 as network adapter on Serial interface.
6+
paragraph=This library creates standard Arduino Ethernet networking API over ESP AT commands. It works with WiFiEspAT library and enables to use WiFi and Ethernet in one sketch. Especially for AVR based boards it allows to use secure layer comunication over wired network. The library requires AT firmware built with Ethernet support.
7+
category=Communication
8+
url=https://github.com/Networking-for-Arduino/EthernetEspAT
9+
architectures=*
10+
includes=EthernetEspAT.h
11+
dot_a_linkage=true
12+
depends=WiFiEspAT

src/Ethernet.cpp

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
This file is part of the EthernetEspAT library for Arduino
3+
https://github.com/Networking-for-Arduino/EthernetEspAT
4+
Copyright 2024 Juraj Andrassy
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include "Ethernet.h"
22+
#include <utility/EspAtDrv.h>
23+
24+
// these statics are removed by compiler, if not used
25+
char EthernetClass::fwVersion[15] = {0};
26+
char EthernetClass::name[33] = {0}; // hostname
27+
28+
EthernetClass::EthernetClass() {
29+
}
30+
31+
bool EthernetClass::init(Stream& serial, int8_t resetPin) {
32+
bool ok = EspAtDrv.init(&serial, resetPin);
33+
if (ok) {
34+
hwStatus = EthernetHardwareFound;
35+
}
36+
return ok;
37+
}
38+
39+
bool EthernetClass::setPersistent(bool persistent) {
40+
return EspAtDrv.sysPersistent(persistent);
41+
}
42+
43+
int EthernetClass::begin(uint8_t *mac, unsigned long timeout) {
44+
if (mac != nullptr && !EspAtDrv.ethSetMac(mac))
45+
return 0;
46+
if (!EspAtDrv.ethEnableDHCP())
47+
return false;
48+
if (timeout) {
49+
const unsigned long start = millis();
50+
while ((millis() - start) < timeout) {
51+
if (localIP() != INADDR_NONE)
52+
return true;
53+
delay(200);
54+
}
55+
}
56+
return (localIP() != INADDR_NONE);
57+
}
58+
59+
bool EthernetClass::begin(uint8_t *mac, IPAddress localIP, IPAddress dnsIP, IPAddress gatewayIP, IPAddress netmask) {
60+
(void) mac;
61+
if (dnsIP == INADDR_NONE) {
62+
dnsIP = localIP;
63+
dnsIP[3] = 1;
64+
}
65+
return EspAtDrv.ethStaticIp(localIP, gatewayIP, netmask) && setDNS(dnsIP);
66+
}
67+
68+
int EthernetClass::begin(unsigned long timeout) {
69+
return begin(nullptr, timeout);
70+
}
71+
72+
bool EthernetClass::begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {
73+
return begin(nullptr, ip, dns, gateway, subnet);
74+
}
75+
76+
int EthernetClass::maintain() {
77+
EspAtDrv.maintain();
78+
return 0;
79+
}
80+
81+
void EthernetClass::end() {
82+
83+
}
84+
85+
EthernetLinkStatus EthernetClass::linkStatus() {
86+
return EspAtDrv.ethStatus() ? LinkON : LinkOFF;
87+
}
88+
89+
EthernetHardwareStatus EthernetClass::hardwareStatus() {
90+
return hwStatus;
91+
}
92+
93+
void EthernetClass::MACAddress(uint8_t *mac) {
94+
macAddress(mac);
95+
}
96+
97+
IPAddress EthernetClass::dnsServerIP() {
98+
return dnsIP();
99+
}
100+
101+
void EthernetClass::setDnsServerIP(const IPAddress dns) {
102+
setDNS(dns);
103+
}
104+
105+
bool EthernetClass::setDNS(IPAddress dns_server1, IPAddress dns_server2) {
106+
return EspAtDrv.setDNS(dns_server1, dns_server2);
107+
}
108+
109+
bool EthernetClass::setHostname(const char* name) {
110+
return EspAtDrv.setEthHostname(name);
111+
}
112+
113+
const char* EthernetClass::hostname(char* buffer) {
114+
if (!EspAtDrv.ethHostnameQuery(buffer)) {
115+
buffer[0] = 0;
116+
}
117+
return buffer;
118+
}
119+
120+
uint8_t* EthernetClass::macAddress(uint8_t* mac) {
121+
if (!EspAtDrv.ethMacQuery(mac))
122+
return nullptr;
123+
return mac;
124+
}
125+
126+
IPAddress EthernetClass::localIP() {
127+
IPAddress ip;
128+
IPAddress gw;
129+
IPAddress mask;
130+
EspAtDrv.ethIpQuery(ip, gw, mask);
131+
return ip;
132+
}
133+
134+
IPAddress EthernetClass::gatewayIP() {
135+
IPAddress ip;
136+
IPAddress gw;
137+
IPAddress mask;
138+
EspAtDrv.ethIpQuery(ip, gw, mask);
139+
return gw;
140+
}
141+
142+
IPAddress EthernetClass::subnetMask() {
143+
IPAddress ip;
144+
IPAddress gw;
145+
IPAddress mask;
146+
EspAtDrv.ethIpQuery(ip, gw, mask);
147+
return mask;
148+
}
149+
150+
IPAddress EthernetClass::dnsIP(int n) {
151+
IPAddress dns1;
152+
IPAddress dns2;
153+
EspAtDrv.dnsQuery(dns1, dns2);
154+
switch (n) {
155+
case 0:
156+
return dns1;
157+
case 1:
158+
return dns2;
159+
}
160+
return IPAddress(0, 0, 0, 0);
161+
}
162+
163+
bool EthernetClass::dhcpIsEnabled() {
164+
bool sta;
165+
bool ap;
166+
bool eth;
167+
if (!EspAtDrv.dhcpStateQuery(sta, ap, eth))
168+
return false;
169+
return eth;
170+
}
171+
172+
173+
bool EthernetClass::startMDNS(const char* hostname, const char* serverName, uint16_t serverPort) {
174+
return EspAtDrv.mDNS(hostname, serverName, serverPort);
175+
}
176+
177+
bool EthernetClass::hostByName(const char* hostname, IPAddress& result) {
178+
return EspAtDrv.resolve(hostname, result);
179+
}
180+
181+
bool EthernetClass::ping(const char* hostname) {
182+
return EspAtDrv.ping(hostname);
183+
}
184+
185+
bool EthernetClass::ping(IPAddress ip) {
186+
char s[16];
187+
EspAtDrv.ip2str(ip, s);
188+
return ping(s);
189+
}
190+
191+
bool EthernetClass::sntp(const char* server1, const char* server2) {
192+
return EspAtDrv.sntpCfg(server1, server2);
193+
}
194+
195+
unsigned long EthernetClass::getTime() {
196+
return EspAtDrv.sntpTime();
197+
}
198+
199+
const char* EthernetClass::firmwareVersion(char* buffer) {
200+
EspAtDrv.firmwareVersion(buffer);
201+
return buffer;
202+
}
203+
204+
EspAtDrvError EthernetClass::getLastDriverError() {
205+
return EspAtDrv.getLastErrorCode();
206+
}
207+
208+
bool EthernetClass::sleepMode(EspAtSleepMode mode) {
209+
return EspAtDrv.sleepMode(mode);
210+
}
211+
212+
bool EthernetClass::deepSleep() {
213+
return EspAtDrv.deepSleep();
214+
}
215+
216+
bool EthernetClass::reset(uint8_t resetPin) {
217+
return EspAtDrv.reset(resetPin);
218+
}
219+
220+
bool EthernetClass::wifiOff(bool save) {
221+
return EspAtDrv.wifiOff(save);
222+
}
223+
224+
225+
EthernetClass Ethernet;

0 commit comments

Comments
 (0)