├── EthernetStatusCallBack ├── EthernetStatusCallBack.ino └── README.md ├── LICENSE ├── NTPClient ├── NTPClient.ino └── README.md ├── README.md ├── TCP_Client ├── README.md └── TCP_Client.ino ├── TCP_Server ├── README.md └── TCP_Server.ino ├── UdpBroadcastReceive ├── README.md └── UdpBroadcastReceive.ino ├── UdpBroadcastSend ├── README.md └── UdpBroadcastSend.ino ├── configTzTime ├── README.md └── configTzTime.ino ├── connectEthernet ├── README.md └── connectEthernet.ino ├── connectEthernetAndWiFi ├── README.md └── connectEthernetAndWiFi.ino ├── mqtt_pub ├── README.md └── mqtt_pub.ino └── mqtt_sub ├── README.md └── mqtt_sub.ino /EthernetStatusCallBack/EthernetStatusCallBack.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //#include 5 | //#include 6 | #include "ESP8266TimerInterrupt.h" 7 | 8 | #define CSPIN 16 9 | 10 | Wiznet5500lwIP eth(CSPIN); 11 | //Wiznet5100lwIP eth(CSPIN); 12 | //ENC28J60lwIP eth(CSPIN); 13 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 14 | 15 | ESP8266Timer ITimer; 16 | 17 | void timer_task() { 18 | Serial.print("eth.status="); 19 | Serial.println(eth.status()); 20 | } 21 | 22 | void setup() { 23 | delay(1000); 24 | Serial.begin(115200); 25 | Serial.println(); 26 | Serial.print(ESP.getFullVersion()); 27 | Serial.println(); 28 | 29 | SPI.begin(); 30 | SPI.setBitOrder(MSBFIRST); 31 | SPI.setDataMode(SPI_MODE0); 32 | SPI.setFrequency(4000000); 33 | 34 | eth.setDefault(); // use ethernet for default route 35 | int present = eth.begin(mac); 36 | if (!present) { 37 | Serial.println("no ethernet hardware present"); 38 | while(1); 39 | } 40 | 41 | Serial.print("connecting ethernet"); 42 | while (!eth.connected()) { 43 | Serial.print("."); 44 | delay(1000); 45 | } 46 | Serial.println(); 47 | Serial.print("ethernet ip address: "); 48 | Serial.println(eth.localIP()); 49 | Serial.print("ethernet subnetMask: "); 50 | Serial.println(eth.subnetMask()); 51 | Serial.print("ethernet gateway: "); 52 | Serial.println(eth.gatewayIP()); 53 | 54 | // 1 second interval timer 55 | ITimer.attachInterruptInterval(1000*1000, timer_task); 56 | } 57 | 58 | void loop() { 59 | 60 | } 61 | -------------------------------------------------------------------------------- /EthernetStatusCallBack/README.md: -------------------------------------------------------------------------------- 1 | # EthernetStatusCallBack 2 | Get current Ethernet status using interrupt. 3 | 4 | This project uses ESP8266TimerInterrupt. 5 | https://www.arduino.cc/reference/en/libraries/esp8266timerinterrupt/ 6 | 7 | 8 | # Screen Shot 9 | status=3:Connected 10 | status=7:Not Connected 11 | 12 | ``` 13 | SDK:2.2.2-dev(38a443e)/Core:3.1.2=30102000/lwIP:STABLE-2_1_3_RELEASE/glue:1.2-65-g06164fb/BearSSL:b024386 14 | connecting ethernet... 15 | ethernet ip address: 192.168.10.113 16 | ethernet subnetMask: 255.255.255.0 17 | ethernet gateway: 192.168.10.1 18 | eth.status=3 19 | eth.status=3 20 | eth.status=3 21 | eth.status=3 22 | eth.status=3 23 | eth.status=3 24 | eth.status=3 25 | eth.status=3 26 | eth.status=7 27 | eth.status=7 28 | eth.status=7 29 | eth.status=7 30 | eth.status=7 31 | eth.status=3 32 | eth.status=3 33 | eth.status=3 34 | eth.status=3 35 | eth.status=3 36 | ``` 37 | 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 nopnop2002 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NTPClient/NTPClient.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include 7 | //#include 8 | 9 | #define CSPIN 16 10 | 11 | Wiznet5500lwIP eth(CSPIN); 12 | //Wiznet5100lwIP eth(CSPIN); 13 | //ENC28J60lwIP eth(CSPIN); 14 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 15 | 16 | // A UDP instance to let us send and receive packets over UDP 17 | WiFiUDP ntpUDP; 18 | 19 | // Define NTP Client to get time 20 | NTPClient timeClient(ntpUDP, "pool.ntp.org"); 21 | 22 | //Week Days 23 | String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 24 | 25 | //Month names 26 | String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 27 | 28 | void setup(){ 29 | Serial.begin(115200); 30 | Serial.println(); 31 | Serial.print(ESP.getFullVersion()); 32 | Serial.println(); 33 | 34 | SPI.begin(); 35 | SPI.setBitOrder(MSBFIRST); 36 | SPI.setDataMode(SPI_MODE0); 37 | SPI.setFrequency(40000000); 38 | 39 | eth.setDefault(); // use ethernet for default route 40 | int present = eth.begin(mac); 41 | if (!present) { 42 | Serial.println("no ethernet hardware present"); 43 | while(1); 44 | } 45 | 46 | Serial.print("connecting ethernet"); 47 | while (!eth.connected()) { 48 | Serial.print("."); 49 | delay(1000); 50 | } 51 | Serial.println(); 52 | Serial.print("ethernet ip address: "); 53 | Serial.println(eth.localIP()); 54 | Serial.print("ethernet subnetMask: "); 55 | Serial.println(eth.subnetMask()); 56 | Serial.print("ethernet gateway: "); 57 | Serial.println(eth.gatewayIP()); 58 | 59 | // Initialize a NTPClient to get time 60 | Serial.println("Starting NTP Cient"); 61 | timeClient.begin(); 62 | // Set offset time in seconds to adjust for your timezone, for example: 63 | // GMT +1 = 3600 64 | // GMT +8 = 28800 65 | // GMT -1 = -3600 66 | // GMT 0 = 0 67 | int timeOffset = 9 * 3600; // JST-9 68 | timeClient.setTimeOffset(timeOffset); 69 | } 70 | 71 | void loop() { 72 | timeClient.update(); 73 | 74 | time_t epochTime = timeClient.getEpochTime(); 75 | Serial.print("Epoch Time: "); 76 | Serial.println(epochTime); 77 | 78 | String formattedTime = timeClient.getFormattedTime(); 79 | Serial.print("Formatted Time: "); 80 | Serial.println(formattedTime); 81 | 82 | int currentHour = timeClient.getHours(); 83 | Serial.print("Hour: "); 84 | Serial.println(currentHour); 85 | 86 | int currentMinute = timeClient.getMinutes(); 87 | Serial.print("Minutes: "); 88 | Serial.println(currentMinute); 89 | 90 | int currentSecond = timeClient.getSeconds(); 91 | Serial.print("Seconds: "); 92 | Serial.println(currentSecond); 93 | 94 | String weekDay = weekDays[timeClient.getDay()]; 95 | Serial.print("Week Day: "); 96 | Serial.println(weekDay); 97 | 98 | //Get a time structure 99 | struct tm *ptm = gmtime ((time_t *)&epochTime); 100 | 101 | int monthDay = ptm->tm_mday; 102 | Serial.print("Month day: "); 103 | Serial.println(monthDay); 104 | 105 | int currentMonth = ptm->tm_mon+1; 106 | Serial.print("Month: "); 107 | Serial.println(currentMonth); 108 | 109 | String currentMonthName = months[currentMonth-1]; 110 | Serial.print("Month name: "); 111 | Serial.println(currentMonthName); 112 | 113 | int currentYear = ptm->tm_year+1900; 114 | Serial.print("Year: "); 115 | Serial.println(currentYear); 116 | 117 | //Print complete date: 118 | String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay); 119 | Serial.print("Current date: "); 120 | Serial.println(currentDate); 121 | 122 | Serial.println(""); 123 | 124 | delay(2000); 125 | } 126 | -------------------------------------------------------------------------------- /NTPClient/README.md: -------------------------------------------------------------------------------- 1 | # NTPClient 2 | Get time from a NTP server and keep it in sync. 3 | This project uses NTPClient. 4 | https://www.arduino.cc/reference/en/libraries/ntpclient/ 5 | 6 | You need to adapt this to your environment. 7 | ``` 8 | int timeOffset = 9 * 3600; // JST-9 9 | ``` 10 | 11 | # Screen Shot 12 | ``` 13 | ethernet ip address: 192.168.10.113 14 | ethernet subnetMask: 255.255.255.0 15 | ethernet gateway: 192.168.10.1 16 | Starting NTP Cient 17 | sendNTPPacket 18 | Update from NTP Server 19 | Epoch Time: 1720482357 20 | Formatted Time: 23:45:57 21 | Hour: 23 22 | Minutes: 45 23 | Seconds: 57 24 | Week Day: Monday 25 | Month day: 8 26 | Month: 7 27 | Month name: July 28 | Year: 2024 29 | Current date: 2024-7-8 30 | ``` 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp8266_ethernet 2 | Example of using SPI Ethernet module with esp8266. 3 | Arduino core for ESP8266 uses [lwip](https://savannah.nongnu.org/projects/lwip/) for TCP/IP protocol stack. 4 | The hierarchy of programs that use WiFi is as follows: 5 | 6 | ``` 7 | +----------------+ 8 | | Application | 9 | +----------------+ 10 | | Network Object | 11 | +----------------+ 12 | | lwip | 13 | +----------------+ 14 | | WiFi Driver | 15 | +----------------+ 16 | ``` 17 | 18 | Arduino Core for ESP8266 Ver3.0.2 officially supports SPI External Ethernet. 19 | __You don't need any libraries.__ 20 | The hierarchy of programs that use Ethernet is as follows: 21 | 22 | ``` 23 | +----------------+ 24 | | Application | 25 | +----------------+ 26 | | Network Object | 27 | +----------------+ 28 | | lwip | 29 | +----------------+ 30 | | Ethernet Driver| 31 | +----------------+ 32 | ``` 33 | 34 | # Hardware requiment 35 | SPI Ethernet module using this chip. 36 | - ENC28J60 37 | - W5100 38 | - W5500 39 | 40 | # Software requiment 41 | - Arduino core for ESP8266 __V3.0.2__ or later 42 | ![](https://img.shields.io/badge/_IMPORTANT-important) 43 | You can check the Core version using ESP.getFullVersion() or ESP.getCoreVersion(). 44 | This is mine: 45 | ``` 46 | ESP.getFullVersion=SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635 47 | ESP.getCoreVersion=3.0.2 48 | ``` 49 | 50 | 51 | # Wireing 52 | 53 | |SPI PHY|---|ESP8266| 54 | |:-:|:-:|:-:| 55 | |SCK|---|GPIO14(D5)| 56 | |MISO|---|GPIO12(D6)| 57 | |MOSI|---|GPIO13(D7)| 58 | |CS|---|GPIO16(D0)(*)| 59 | |RST|---|RESET| 60 | |3.3V|---|3.3V| 61 | |GND|---|GND| 62 | 63 | (*) You can change it to any GPIO. 64 | 65 | # Selecting the Ethernet module 66 | 67 | - Using W5500: 68 | ``` 69 | #include 70 | Wiznet5500lwIP eth(CSPIN); 71 | ``` 72 | ![W5500](https://user-images.githubusercontent.com/6020549/83312712-524abd00-a24e-11ea-9c15-c5ad85022854.JPG) 73 | 74 | - Using W5100: 75 | ``` 76 | #include 77 | Wiznet5100lwIP eth(CSPIN); 78 | ``` 79 | ![W5100](https://user-images.githubusercontent.com/6020549/83312708-4f4fcc80-a24e-11ea-923d-409ddeeee855.JPG) 80 | 81 | - Using ENC28J60: 82 | ``` 83 | #include 84 | ENC28J60lwIP eth(CSPIN); 85 | ``` 86 | ![ENC28J60](https://user-images.githubusercontent.com/6020549/83312722-57a80780-a24e-11ea-8ae5-f878071a8a3d.JPG) 87 | 88 | 89 | # Using PlatformIO 90 | ``` 91 | $ git clone https://github.com/nopnop2002/esp8266_ethernet 92 | 93 | $ cd esp8266_ethernet/connectEthernet/ 94 | 95 | $ pio init -b d1_mini 96 | 97 | $ cp connectEthernet.ino src/ 98 | 99 | $ pio run -t upload && pio device monitor -b 115200 100 | 101 | ==================================== [SUCCESS] Took 25.50 seconds ==================================== 102 | --- Available filters and text transformations: colorize, debug, default, direct, esp8266_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time 103 | --- More details at https://bit.ly/pio-monitor-filters 104 | --- Miniterm on /dev/ttyUSB0 115200,8,N,1 --- 105 | --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- 106 | 107 | SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635 108 | connecting ethernet.. 109 | ethernet ip address: 192.168.10.128 110 | ethernet subnetMask: 255.255.255.0 111 | ethernet gateway: 192.168.10.1 112 | ``` 113 | 114 | # How to convert from WiFi to Ethernet 115 | 116 | - WiFi 117 | ``` 118 | #include 119 | 120 | const char* SSID = "ssid_of_your_router"; 121 | const char* PASSWORD = "password_of_your_router"; 122 | 123 | void setup() { 124 | delay(1000); 125 | Serial.begin(115200); 126 | Serial.println(); 127 | Serial.print(ESP.getFullVersion()); 128 | Serial.println(); 129 | 130 | Serial.print("connecting wifi"); 131 | WiFi.mode(WIFI_STA); 132 | WiFi.begin(SSID, PASSWORD); 133 | while (1) { 134 | uint8_t status = WiFi.status(); 135 | Serial.print("status="); 136 | Serial.println(status); 137 | if (status == WL_CONNECTED) break; 138 | Serial.print("."); 139 | delay(500); 140 | } 141 | Serial.println(); 142 | Serial.print("WiFi ip address: "); 143 | Serial.println(WiFi.localIP()); 144 | Serial.print("Wifi subnetMask: "); 145 | Serial.println(WiFi.subnetMask()); 146 | Serial.print("WiFi.gatewayIP: "); 147 | Serial.println(WiFi.gatewayIP()); 148 | Serial.print("WiFi.dnsIP: "); 149 | Serial.println(WiFi.dnsIP()); 150 | 151 | } 152 | 153 | void loop() { 154 | } 155 | ``` 156 | 157 | - Ethernet 158 | ``` 159 | #include 160 | #include 161 | #include 162 | //#include 163 | //#include 164 | 165 | #define CSPIN 16 166 | 167 | Wiznet5500lwIP eth(CSPIN); 168 | //Wiznet5100lwIP eth(CSPIN); 169 | //ENC28J60lwIP eth(CSPIN); 170 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 171 | 172 | void setup() { 173 | delay(1000); 174 | Serial.begin(115200); 175 | Serial.println(); 176 | Serial.print(ESP.getFullVersion()); 177 | Serial.println(); 178 | 179 | SPI.begin(); 180 | SPI.setBitOrder(MSBFIRST); 181 | SPI.setDataMode(SPI_MODE0); 182 | SPI.setFrequency(4000000); 183 | 184 | eth.setDefault(); // use ethernet for default route 185 | int present = eth.begin(mac); 186 | if (!present) { 187 | Serial.println("no ethernet hardware present"); 188 | while(1); 189 | } 190 | 191 | Serial.print("connecting ethernet"); 192 | while (!eth.connected()) { 193 | Serial.print("."); 194 | delay(1000); 195 | } 196 | Serial.println(); 197 | Serial.print("ethernet ip address: "); 198 | Serial.println(eth.localIP()); 199 | Serial.print("ethernet subnetMask: "); 200 | Serial.println(eth.subnetMask()); 201 | Serial.print("ethernet gateway: "); 202 | Serial.println(eth.gatewayIP()); 203 | } 204 | 205 | void loop() { 206 | } 207 | ``` 208 | 209 | # WiFi examples 210 | https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi/examples 211 | -------------------------------------------------------------------------------- /TCP_Client/README.md: -------------------------------------------------------------------------------- 1 | # TCP Client 2 | TCP Server using Python & zeroconf. 3 | You need to install zeroconf mDNS library. 4 | ``` 5 | $ python3 -m pip install zeroconf 6 | ``` 7 | 8 | 9 | 10 | ``` 11 | #!/usr/bin/python 12 | #-*- encoding: utf-8 -*- 13 | import argparse 14 | import sys 15 | import socket 16 | import netifaces 17 | import signal 18 | import subprocess 19 | 20 | from zeroconf import IPVersion, ServiceInfo, Zeroconf 21 | 22 | def handler(signal, frame): 23 | global running 24 | print('handler') 25 | running = False 26 | 27 | if __name__ == "__main__": 28 | signal.signal(signal.SIGINT, handler) 29 | running = True 30 | 31 | parser = argparse.ArgumentParser() 32 | parser.add_argument('--interface', help="Ethernet interface", default="eth0") 33 | parser.add_argument('--port', type=int, help="TCP Port", default=9876) 34 | parser.add_argument("--debug", action="store_true") 35 | args = parser.parse_args() 36 | print("args.interface={}".format(args.interface)) 37 | print("args.port={}".format(args.port)) 38 | 39 | if args.debug: 40 | logging.getLogger("zeroconf").setLevel(logging.DEBUG) 41 | 42 | try: 43 | ip_addr = netifaces.ifaddresses(args.interface)[netifaces.AF_INET][0]['addr'] 44 | except: 45 | print("{} not found.". format(args.interface)) 46 | print("You must specify a valid interface name.") 47 | command = "ifconfig" 48 | p = subprocess.run(command, 49 | stdout=subprocess.PIPE, 50 | stderr=subprocess.PIPE, 51 | shell=True 52 | ) 53 | print(p.stdout.decode("utf-8", "ignore")) 54 | sys.exit() 55 | print("ip_addr={}".format(ip_addr)) 56 | 57 | info = ServiceInfo( 58 | "_http._tcp.local.", 59 | "TCP Server._http._tcp.local.", 60 | addresses=[socket.inet_aton(ip_addr)], 61 | port=args.port, 62 | server="esp8266-server.local.", 63 | ) 64 | 65 | ip_version = IPVersion.V4Only 66 | zeroconf = Zeroconf(ip_version=ip_version) 67 | print("Registration of a service, press Ctrl-C to exit...") 68 | zeroconf.register_service(info) 69 | 70 | tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 71 | tcp_server.bind(("", args.port)) 72 | tcp_server.listen(5) 73 | 74 | while running: 75 | client,address = tcp_server.accept() 76 | print("Connected!! [ Source : {}]".format(address)) 77 | 78 | rmsg = client.recv(1024) 79 | if (type(rmsg) is bytes): 80 | rmsg=rmsg.decode('utf-8') 81 | #print("Received Data : {}".format(rmsg)) 82 | smsg = rmsg.swapcase() 83 | client.send(smsg.encode('utf-8')) 84 | print("{} --> {}".format(rmsg, smsg)) 85 | 86 | client.close() 87 | 88 | print("Unregistering...") 89 | zeroconf.unregister_service(info) 90 | zeroconf.close() 91 | ``` 92 | 93 | 94 | # Screen Shot 95 | ``` 96 | SDK:2.2.2-dev(38a443e)/Core:3.1.2=30102000/lwIP:STABLE-2_1_3_RELEASE/glue:1.2-65-g06164fb/BearSSL:b024386 97 | connecting ethernet... 98 | ethernet ip address: 192.168.10.114 99 | ethernet subnetMask: 255.255.255.0 100 | ethernet gateway: 192.168.10.1 101 | Sending mDNS query 102 | mDNS query done 103 | 1 service(s) found 104 | 1: esp8266-server.local (192.168.10.46:9876) 105 | remoteIp=192.168.10.46 106 | Client connect to 192.168.10.46 .....ok 107 | Server response....ok 108 | TCP Client Receive Size=23 109 | data from ESP8266 00000-->DATA FROM esp8266 00000 110 | Client disconnect 111 | Client connect to 192.168.10.46 .....ok 112 | Server response....ok 113 | TCP Client Receive Size=23 114 | data from ESP8266 00001-->DATA FROM esp8266 00001 115 | Client disconnect 116 | Client connect to 192.168.10.46 .....ok 117 | Server response....ok 118 | TCP Client Receive Size=23 119 | data from ESP8266 00002-->DATA FROM esp8266 00002 120 | Client disconnect 121 | Client connect to 192.168.10.46 .....ok 122 | Server response....ok 123 | TCP Client Receive Size=23 124 | data from ESP8266 00003-->DATA FROM esp8266 00003 125 | Client disconnect 126 | Client connect to 192.168.10.46 .....ok 127 | Server response....ok 128 | TCP Client Receive Size=23 129 | data from ESP8266 00004-->DATA FROM esp8266 00004 130 | Client disconnect 131 | ``` 132 | 133 | -------------------------------------------------------------------------------- /TCP_Client/TCP_Client.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * Simple TCP Client 3 | * Connect to esp8266-server._esp._tcp.local 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | //#include 11 | //#include 12 | 13 | #define CSPIN 16 14 | 15 | Wiznet5500lwIP eth(CSPIN); 16 | //Wiznet5100lwIP eth(CSPIN); 17 | //ENC28J60lwIP eth(CSPIN); 18 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x04}; 19 | 20 | #define INTERVAL 1000 21 | #define TCP_PORT 9876 22 | #define TIME_OUT 10000 23 | 24 | IPAddress remoteIp; 25 | 26 | WiFiClient client; 27 | 28 | unsigned long nextMillis; 29 | 30 | void setup() { 31 | delay(1000); 32 | Serial.begin(115200); 33 | Serial.println(); 34 | Serial.print(ESP.getFullVersion()); 35 | Serial.println(); 36 | 37 | SPI.begin(); 38 | SPI.setBitOrder(MSBFIRST); 39 | SPI.setDataMode(SPI_MODE0); 40 | SPI.setFrequency(4000000); 41 | 42 | eth.setDefault(); // use ethernet for default route 43 | int present = eth.begin(mac); 44 | if (!present) { 45 | Serial.println("no ethernet hardware present"); 46 | while(1); 47 | } 48 | 49 | Serial.print("connecting ethernet"); 50 | while (!eth.connected()) { 51 | Serial.print("."); 52 | delay(1000); 53 | } 54 | Serial.println(); 55 | Serial.print("ethernet ip address: "); 56 | Serial.println(eth.localIP()); 57 | Serial.print("ethernet subnetMask: "); 58 | Serial.println(eth.subnetMask()); 59 | Serial.print("ethernet gateway: "); 60 | Serial.println(eth.gatewayIP()); 61 | 62 | // Start the mDNS responder for esp8266-client.local 63 | if (!MDNS.begin("esp8266-client")) { 64 | Serial.println("Error setting up MDNS responder!"); 65 | } 66 | 67 | // Find endpoint for esp tcp 68 | int validRemoteIp = 0; 69 | while(1) { 70 | // Send out query for esp tcp services 71 | Serial.println("Sending mDNS query"); 72 | int n = MDNS.queryService("http", "tcp"); 73 | Serial.println("mDNS query done"); 74 | if (n == 0) { 75 | Serial.println("no services found"); 76 | delay(1000); 77 | } else { 78 | Serial.print(n); 79 | Serial.println(" service(s) found"); 80 | for (int i = 0; i < n; ++i) { 81 | // Print details for each service found 82 | Serial.print(i + 1); 83 | Serial.print(": "); 84 | Serial.print(MDNS.hostname(i)); 85 | Serial.print(" ("); 86 | Serial.print(MDNS.IP(i)); 87 | Serial.print(":"); 88 | Serial.print(MDNS.port(i)); 89 | Serial.println(")"); 90 | 91 | if (MDNS.port(i) == TCP_PORT) { 92 | remoteIp = MDNS.IP(i); 93 | validRemoteIp++; 94 | } // end if 95 | } // end for 96 | } // end if 97 | if (validRemoteIp != 0) break; 98 | } // endo for 99 | 100 | Serial.print("remoteIp="); 101 | Serial.println(remoteIp); 102 | 103 | nextMillis = millis(); 104 | } 105 | 106 | 107 | void loop() { 108 | static int num = 0; 109 | char smsg[30]; 110 | char rmsg[30]; 111 | int rflag; 112 | unsigned long timeout; 113 | unsigned long now; 114 | 115 | now = millis(); 116 | if ( long(now - nextMillis) > 0) { 117 | Serial.print("Client connect to "); 118 | Serial.print(remoteIp); 119 | Serial.print(" ....."); 120 | if (!client.connect(remoteIp, TCP_PORT)) { 121 | Serial.println("failed"); 122 | while(1) { 123 | delay(1); 124 | } 125 | } else { 126 | Serial.println("ok"); 127 | sprintf(smsg,"data from ESP8266 %05d",num); 128 | num++; 129 | client.write(smsg, strlen(smsg)); 130 | 131 | // wait for responce 132 | rflag = 1; 133 | timeout = millis(); 134 | while(client.available() == 0) { 135 | now = millis(); 136 | if (long(now - timeout) > TIME_OUT) { 137 | rflag = 0; 138 | } 139 | } // end while 140 | 141 | Serial.print("Server response...."); 142 | if (rflag == 0) { 143 | Serial.println("failed"); 144 | } else { 145 | Serial.println("ok"); 146 | int size; 147 | while((size = client.available()) > 0) { 148 | Serial.print("TCP Client Receive Size="); 149 | Serial.println(size); 150 | size = client.read((uint8_t *)rmsg,size); 151 | Serial.write((uint8_t *)smsg,size); 152 | Serial.write("-->"); 153 | Serial.write((uint8_t *)rmsg,size); 154 | Serial.println(""); 155 | } // end while 156 | } 157 | 158 | //disconnect client 159 | Serial.println("Client disconnect"); 160 | client.stop(); 161 | nextMillis = millis() + INTERVAL; 162 | 163 | } // end if 164 | } // end if 165 | } 166 | -------------------------------------------------------------------------------- /TCP_Server/README.md: -------------------------------------------------------------------------------- 1 | # TCP Server 2 | TCP Client using Python. 3 | ``` 4 | #!/usr/bin/python 5 | #-*- encoding: utf-8 -*- 6 | import argparse 7 | import sys 8 | import socket 9 | import time 10 | import signal 11 | 12 | def handler(signal, frame): 13 | global running 14 | print('handler') 15 | running = False 16 | 17 | if __name__ == "__main__": 18 | signal.signal(signal.SIGINT, handler) 19 | running = True 20 | 21 | parser = argparse.ArgumentParser() 22 | parser.add_argument('--host', help="TCP Host", default="esp8266-server.local") 23 | parser.add_argument('--port', type=int, help="TCP Port", default=9876) 24 | args = parser.parse_args() 25 | print("args.host={}".format(args.host)) 26 | print("args.port={}".format(args.port)) 27 | 28 | while running: 29 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 30 | try: 31 | client.connect((args.host, args.port)) 32 | except: 33 | print("{} not found".format(args.host)) 34 | sys.exit() 35 | request = "Hello World" 36 | #client.send(b'Hello World') 37 | client.send(request.encode('utf-8')) 38 | response = client.recv(1024) 39 | if (type(response) is bytes): 40 | response=response.decode('utf-8') 41 | print("{} --> {}".format(request, response)) 42 | client.close() 43 | time.sleep(5) 44 | ``` 45 | 46 | 47 | # Screen Shot 48 | ``` 49 | SDK:2.2.2-dev(38a443e)/Core:3.1.2=30102000/lwIP:STABLE-2_1_3_RELEASE/glue:1.2-65-g06164fb/BearSSL:b024386 50 | connecting ethernet... 51 | ethernet ip address: 192.168.10.113 52 | ethernet subnetMask: 255.255.255.0 53 | ethernet gateway: 192.168.10.1 54 | Server Start. Wait for Client 55 | mDNS responder started 56 | new client 57 | TCP Server Receive Size=11 58 | Hello World->hELLO wORLD 59 | Client disonnected 60 | new client 61 | TCP Server Receive Size=11 62 | Hello World->hELLO wORLD 63 | Client disonnected 64 | new client 65 | TCP Server Receive Size=11 66 | Hello World->hELLO wORLD 67 | Client disonnected 68 | ``` 69 | 70 | -------------------------------------------------------------------------------- /TCP_Server/TCP_Server.ino: -------------------------------------------------------------------------------- 1 | /* 2 | TCP Server 3 | mDNS host name is "esp8266-server.local" 4 | Service name is "esp8266-server._esp._tcp.local" 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | //#include 12 | //#include 13 | 14 | #define CSPIN 16 15 | 16 | Wiznet5500lwIP eth(CSPIN); 17 | //Wiznet5100lwIP eth(CSPIN); 18 | //ENC28J60lwIP eth(CSPIN); 19 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 20 | 21 | #define SERVER_PORT 9876 22 | 23 | // Create an instance of the server 24 | // specify the port to listen on as an argument 25 | WiFiServer server(SERVER_PORT); 26 | 27 | //#define STATIC 28 | 29 | void setup() { 30 | delay(1000); 31 | Serial.begin(115200); 32 | Serial.println(); 33 | Serial.print(ESP.getFullVersion()); 34 | Serial.println(); 35 | 36 | SPI.begin(); 37 | SPI.setBitOrder(MSBFIRST); 38 | SPI.setDataMode(SPI_MODE0); 39 | SPI.setFrequency(4000000); 40 | 41 | eth.setDefault(); // use ethernet for default route 42 | 43 | #ifdef STATIC 44 | IPAddress ip(192,168,10,130); 45 | IPAddress gateway(192,168,10,1); 46 | IPAddress subnet(255,255,255,0); 47 | eth.config(ip, gateway, subnet); 48 | #endif 49 | 50 | int present = eth.begin(mac); 51 | if (!present) { 52 | Serial.println("no ethernet hardware present"); 53 | while (1); 54 | } 55 | 56 | Serial.print("connecting ethernet"); 57 | while (!eth.connected()) { 58 | Serial.print("."); 59 | delay(1000); 60 | } 61 | Serial.println(); 62 | Serial.print("ethernet ip address: "); 63 | Serial.println(eth.localIP()); 64 | Serial.print("ethernet subnetMask: "); 65 | Serial.println(eth.subnetMask()); 66 | Serial.print("ethernet gateway: "); 67 | Serial.println(eth.gatewayIP()); 68 | 69 | server.begin(); 70 | Serial.println("Server Start. Wait for Client"); 71 | 72 | // Start the mDNS responder for esp8266-server.local 73 | if (!MDNS.begin("esp8266-server")) { 74 | Serial.println("Error setting up MDNS responder!"); 75 | while (1) { 76 | delay(1000); 77 | } 78 | } 79 | // Add service to MDNS-SD 80 | Serial.println("mDNS responder started"); 81 | MDNS.addService("esp", "tcp", SERVER_PORT); 82 | 83 | } 84 | 85 | void loop() { 86 | char smsg[30]; 87 | char rmsg[30]; 88 | 89 | MDNS.update(); 90 | 91 | // Check if a client has connected 92 | WiFiClient client = server.available(); 93 | if (!client) { 94 | return; 95 | } 96 | 97 | // Wait until the client sends some data 98 | Serial.println("new client"); 99 | while (!client.available()) { 100 | delay(1); 101 | } 102 | 103 | int size; 104 | while ((size = client.available()) > 0) { 105 | Serial.print("TCP Server Receive Size="); 106 | Serial.println(size); 107 | size = client.read((uint8_t *)rmsg, size); 108 | for (uint32_t i = 0; i < size; i++) { 109 | if (islower(rmsg[i])) { 110 | smsg[i] = toupper(rmsg[i]); 111 | } else { 112 | smsg[i] = tolower(rmsg[i]); 113 | } // end if 114 | } // end for 115 | 116 | client.write(smsg, size); 117 | 118 | Serial.write((uint8_t *)rmsg, size); 119 | Serial.write("->"); 120 | Serial.write((uint8_t *)smsg, size); 121 | Serial.println(""); 122 | } // end while 123 | 124 | Serial.println("Client disonnected"); 125 | // The client will actually be disconnected 126 | // when the function returns and 'client' object is detroyed 127 | } 128 | -------------------------------------------------------------------------------- /UdpBroadcastReceive/README.md: -------------------------------------------------------------------------------- 1 | # UDP Broadcast Receive 2 | Transmitter using Python. 3 | Using Limited broadcast address. 4 | Limited broadcast addresses can't cross routers. 5 | __Note:__ 6 | If you use a direct broadcast address, you will need to change . 7 | Direct broadcast addresses can cross routers. 8 | ``` 9 | #!/usr/bin/python 10 | #-*- encoding: utf-8 -*- 11 | import argparse 12 | import sys 13 | import socket 14 | import time 15 | import signal 16 | import datetime 17 | 18 | def handler(signal, frame): 19 | global running 20 | print('handler') 21 | running = False 22 | 23 | if __name__ == "__main__": 24 | signal.signal(signal.SIGINT, handler) 25 | running = True 26 | 27 | parser = argparse.ArgumentParser() 28 | parser.add_argument('--port', type=int, help="UDP Port", default=9876) 29 | args = parser.parse_args() 30 | print("args.port={}".format(args.port)) 31 | 32 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 33 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 34 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) 35 | 36 | while running: 37 | #print("sys.version_info={}".format(sys.version_info)) 38 | dt_now = datetime.datetime.now() 39 | #print("type(dt_now)={}".format(type(dt_now))) 40 | message = dt_now.strftime('%Y/%m/%d/ %H:%M:%S') 41 | #print("type(message)={}".format(type(message))) 42 | print("message={}".format(message)) 43 | if (sys.version_info >= (3, 0)): 44 | sock.sendto(message.encode('utf-8'), ('', args.port)) 45 | else: 46 | sock.sendto(message, ('', args.port)) 47 | time.sleep(5) 48 | 49 | #print("socket close") 50 | sock.close() 51 | ``` 52 | 53 | 54 | # Screen Shot 55 | ``` 56 | SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635 57 | connecting ethernet.. 58 | ethernet ip address: 192.168.10.127 59 | ethernet subnetMask: 255.255.255.0 60 | ethernet gateway: 192.168.10.1 61 | Now listening at IP:192.168.10.127 UDP port:9876 62 | 63 | Received 20 bytes from 192.168.10.45, port 57811 64 | UDP packet from 192.168.10.45 57811 65 | UDP packet contents: 2022/03/30/ 08:14:08 66 | 67 | Received 20 bytes from 192.168.10.45, port 57811 68 | UDP packet from 192.168.10.45 57811 69 | UDP packet contents: 2022/03/30/ 08:14:13 70 | 71 | Received 20 bytes from 192.168.10.45, port 57811 72 | UDP packet from 192.168.10.45 57811 73 | UDP packet contents: 2022/03/30/ 08:14:18 74 | ``` 75 | 76 | -------------------------------------------------------------------------------- /UdpBroadcastReceive/UdpBroadcastReceive.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | //#include 6 | //#include 7 | 8 | #define CSPIN 16 9 | 10 | Wiznet5500lwIP eth(CSPIN); 11 | //Wiznet5100lwIP eth(CSPIN); 12 | //ENC28J60lwIP eth(CSPIN); 13 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x04}; 14 | 15 | #define LOCAL_PORT 9876 16 | 17 | WiFiUDP Udp; 18 | 19 | void setup() { 20 | delay(1000); 21 | Serial.begin(115200); 22 | Serial.println(); 23 | Serial.print(ESP.getFullVersion()); 24 | Serial.println(); 25 | 26 | SPI.begin(); 27 | SPI.setBitOrder(MSBFIRST); 28 | SPI.setDataMode(SPI_MODE0); 29 | SPI.setFrequency(4000000); 30 | 31 | eth.setDefault(); // use ethernet for default route 32 | int present = eth.begin(mac); 33 | if (!present) { 34 | Serial.println("no ethernet hardware present"); 35 | while(1); 36 | } 37 | 38 | Serial.print("connecting ethernet"); 39 | while (!eth.connected()) { 40 | Serial.print("."); 41 | delay(1000); 42 | } 43 | Serial.println(); 44 | Serial.print("ethernet ip address: "); 45 | Serial.println(eth.localIP()); 46 | Serial.print("ethernet subnetMask: "); 47 | Serial.println(eth.subnetMask()); 48 | Serial.print("ethernet gateway: "); 49 | Serial.println(eth.gatewayIP()); 50 | 51 | Udp.begin(LOCAL_PORT); 52 | Serial.print("Now listening at IP:"); 53 | Serial.print(eth.localIP()); 54 | Serial.print(" UDP port:"); 55 | Serial.print(LOCAL_PORT); 56 | Serial.println(); 57 | } 58 | 59 | void loop() { 60 | char payload[128]; 61 | IPAddress remoteIp; 62 | int remotePort; 63 | 64 | int packetSize = Udp.parsePacket(); 65 | if (packetSize) { 66 | // receive incoming UDP packets 67 | Serial.println(); 68 | Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort()); 69 | remoteIp = Udp.remoteIP(); 70 | remotePort = Udp.remotePort(); 71 | int payloadSize = Udp.read(payload, sizeof(payload)); 72 | if (payloadSize > 0) { 73 | payload[payloadSize] = 0; 74 | Serial.print("UDP packet from "); 75 | Serial.print(remoteIp); 76 | Serial.print(" "); 77 | Serial.println(remotePort); 78 | Serial.print("UDP packet contents: "); 79 | Serial.println(payload); 80 | } // end payloadSize 81 | } // end packetSize 82 | 83 | } 84 | -------------------------------------------------------------------------------- /UdpBroadcastSend/README.md: -------------------------------------------------------------------------------- 1 | # UDP Broadcast Send 2 | This project uses Limited broadcast address by default. 3 | Limited broadcast addresses can't cross routers. 4 | If you use a directed broadcast address, Uncomment this line. 5 | Directed broadcast addresses can cross routers. 6 | ``` 7 | //#define DIRECT 8 | ``` 9 | 10 | Limited broadcast address is 255.255.255.255. 11 | Directed broadcast address is like 192.168.10.255, only the last octet is 255. 12 | 13 | __Notes on using staic ip__ 14 | If you use a static IP, use Directed broadcast address instead of Limited broadcast address. 15 | 16 | # UDP Broadcast Receiver using python 17 | ``` 18 | #-*- encoding: utf-8 -*- 19 | import argparse 20 | import select 21 | import socket 22 | import signal 23 | 24 | def handler(signal, frame): 25 | global running 26 | print('handler') 27 | running = False 28 | 29 | if __name__ == "__main__": 30 | signal.signal(signal.SIGINT, handler) 31 | running = True 32 | 33 | parser = argparse.ArgumentParser() 34 | parser.add_argument('--port', type=int, help="UDP Port", default=9876) 35 | parser.add_argument('--directed', action='store_true', help="Use Directed broadcast address") 36 | args = parser.parse_args() 37 | print("args.port={}".format(args.port)) 38 | print("args.directed={}".format(args.directed)) 39 | 40 | if (args.directed): 41 | connect_interface = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 42 | connect_interface.connect(("8.8.8.8", 80)) 43 | local_address = connect_interface.getsockname()[0] 44 | #print(connect_interface.getsockname()[0]) 45 | print("local_address={} {}".format(local_address, type(local_address))) 46 | connect_interface.close() 47 | local_address = local_address.split(".") 48 | print("local_address={} {}".format(local_address, type(local_address))) 49 | remote_address = "{}.{}.{}.255".format(local_address[0],local_address[1],local_address[2]) 50 | else: 51 | remote_address = '' 52 | 53 | print("remote_address={}".format(remote_address)) 54 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 55 | #sock.bind(('', args.port)) 56 | sock.bind((remote_address, args.port)) 57 | sock.setblocking(False) 58 | 59 | while running: 60 | # With 1 second timeout 61 | result = select.select([sock],[],[], 1) 62 | #print("result[0]={}".format(result[0])) 63 | # Not timeout 64 | if (len(result[0]) != 0): 65 | msg = result[0][0].recv(1024) 66 | if (type(msg) is bytes): 67 | msg=msg.decode('utf-8') 68 | print(msg) 69 | 70 | sock.close() 71 | ``` 72 | -------------------------------------------------------------------------------- /UdpBroadcastSend/UdpBroadcastSend.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | //#include 6 | //#include 7 | 8 | #define CSPIN 16 9 | 10 | //Uncomment if using Directed broadcast address 11 | //#define DIRECT 12 | 13 | Wiznet5500lwIP eth(CSPIN); 14 | //Wiznet5100lwIP eth(CSPIN); 15 | //ENC28J60lwIP eth(CSPIN); 16 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 17 | 18 | // local port to listen for UDP packets 19 | #define LOCAL_PORT 8888 20 | 21 | // remote port to send for UDP packets 22 | #define REMOTE_PORT 9876 23 | 24 | // remote address to send for UDP packets 25 | char remote_address[64]; 26 | 27 | // Time of last packet transmission(ms) 28 | unsigned long lastSendPacketTime = 0; 29 | 30 | // A UDP instance to let us send and receive packets over UDP 31 | WiFiUDP udp; 32 | 33 | void setup() { 34 | delay(1000); 35 | Serial.begin(115200); 36 | Serial.println(); 37 | Serial.print(ESP.getFullVersion()); 38 | Serial.println(); 39 | 40 | SPI.begin(); 41 | SPI.setBitOrder(MSBFIRST); 42 | SPI.setDataMode(SPI_MODE0); 43 | SPI.setFrequency(4000000); 44 | 45 | eth.setDefault(); // use ethernet for default route 46 | int present = eth.begin(mac); 47 | if (!present) { 48 | Serial.println("no ethernet hardware present"); 49 | while(1); 50 | } 51 | 52 | Serial.print("connecting ethernet"); 53 | while (!eth.connected()) { 54 | Serial.print("."); 55 | delay(1000); 56 | } 57 | Serial.println(); 58 | Serial.print("ethernet ip address: "); 59 | Serial.println(eth.localIP()); 60 | Serial.print("ethernet subnetMask: "); 61 | Serial.println(eth.subnetMask()); 62 | Serial.print("ethernet gateway: "); 63 | Serial.println(eth.gatewayIP()); 64 | 65 | strcpy(remote_address, "255.255.255.255"); 66 | #ifdef DIRECT 67 | //Serial.println(eth.localIP()[0]); 68 | //Serial.println(eth.localIP()[1]); 69 | //Serial.println(eth.localIP()[2]); 70 | sprintf(remote_address, "%d.%d.%d.255", eth.localIP()[0], eth.localIP()[1], eth.localIP()[2]); 71 | #endif 72 | 73 | Serial.print("remote_address=["); 74 | Serial.print(remote_address); 75 | Serial.println("]"); 76 | Serial.println("Starting UDP Send"); 77 | udp.begin(LOCAL_PORT); 78 | } 79 | 80 | void loop() { 81 | long now = millis(); 82 | if (now - lastSendPacketTime > 1000) { // 5 seconds passed 83 | lastSendPacketTime = now; 84 | udp.beginPacket(remote_address, REMOTE_PORT); 85 | byte packetBuffer[64]; 86 | sprintf((char *)packetBuffer, "Hello World! %ld", millis()); 87 | size_t packetSize = strlen((char*)packetBuffer); 88 | udp.write(packetBuffer, packetSize); 89 | udp.endPacket(); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /configTzTime/README.md: -------------------------------------------------------------------------------- 1 | # configTzTime 2 | Set the NTP server time to RTC. 3 | 4 | You need to adapt this to your environment. 5 | ``` 6 | #define TIME_ZONE "JST-9" 7 | ``` 8 | 9 | # Screen Shot 10 | ``` 11 | SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635 12 | connecting ethernet.. 13 | ethernet ip address: 192.168.10.127 14 | ethernet subnetMask: 255.255.255.0 15 | ethernet gateway: 192.168.10.1 16 | ESP8266/Arduino ver3.0.2 : 1970/01/01(Thr) 09:00:03 17 | ESP8266/Arduino ver3.0.2 : 1970/01/01(Thr) 09:00:04 18 | ESP8266/Arduino ver3.0.2 : 1970/01/01(Thr) 09:00:05 19 | ESP8266/Arduino ver3.0.2 : 2022/03/30(Wed) 09:11:49 20 | ESP8266/Arduino ver3.0.2 : 2022/03/30(Wed) 09:11:50 21 | ESP8266/Arduino ver3.0.2 : 2022/03/30(Wed) 09:11:51 22 | ``` 23 | 24 | -------------------------------------------------------------------------------- /configTzTime/configTzTime.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //#include 5 | //#include 6 | 7 | #include 8 | 9 | #define CSPIN 16 10 | 11 | Wiznet5500lwIP eth(CSPIN); 12 | //Wiznet5100lwIP eth(CSPIN); 13 | //ENC28J60lwIP eth(CSPIN); 14 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x04}; 15 | 16 | #define TIME_ZONE "JST-9" 17 | 18 | void setup() { 19 | delay(1000); 20 | Serial.begin(115200); 21 | Serial.println(); 22 | Serial.print(ESP.getFullVersion()); 23 | Serial.println(); 24 | 25 | SPI.begin(); 26 | SPI.setBitOrder(MSBFIRST); 27 | SPI.setDataMode(SPI_MODE0); 28 | SPI.setFrequency(4000000); 29 | 30 | eth.setDefault(); // use ethernet for default route 31 | int present = eth.begin(mac); 32 | if (!present) { 33 | Serial.println("no ethernet hardware present"); 34 | while(1); 35 | } 36 | 37 | Serial.print("connecting ethernet"); 38 | while (!eth.connected()) { 39 | Serial.print("."); 40 | delay(1000); 41 | } 42 | Serial.println(); 43 | Serial.print("ethernet ip address: "); 44 | Serial.println(eth.localIP()); 45 | Serial.print("ethernet subnetMask: "); 46 | Serial.println(eth.subnetMask()); 47 | Serial.print("ethernet gateway: "); 48 | Serial.println(eth.gatewayIP()); 49 | 50 | configTzTime(TIME_ZONE, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); // 2.7.0 or later 51 | } 52 | 53 | void loop() { 54 | time_t t; 55 | struct tm *tm; 56 | static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"}; 57 | 58 | t = time(NULL); 59 | tm = localtime(&t); 60 | Serial.printf("ESP8266/Arduino ver%s : %04d/%02d/%02d(%s) %02d:%02d:%02d\n", 61 | __STR(ARDUINO_ESP8266_GIT_DESC), 62 | tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, 63 | wd[tm->tm_wday], 64 | tm->tm_hour, tm->tm_min, tm->tm_sec); 65 | delay(1000); 66 | } 67 | -------------------------------------------------------------------------------- /connectEthernet/README.md: -------------------------------------------------------------------------------- 1 | # connectEthernet 2 | Connect Ethernet and Get current Ethernet status. 3 | 4 | # Using static ip 5 | Enable here. 6 | ``` 7 | //#define STATIC 8 | ``` 9 | 10 | 11 | # Screen Shot 12 | Detects that the Ethernet cable has been unplugged. 13 | ``` 14 | SDK:2.2.2-dev(38a443e)/Core:3.1.2=30102000/lwIP:STABLE-2_1_3_RELEASE/glue:1.2-65-g06164fb/BearSSL:b024386 15 | connecting ethernet... 16 | ethernet ip address: 192.168.10.113 17 | ethernet subnetMask: 255.255.255.0 18 | ethernet gateway: 192.168.10.1 19 | Ethernet Connected! 20 | Ethernet Connected! 21 | Ethernet Connected! 22 | Ethernet Connected! 23 | Ethernet Connected! 24 | Ethernet Not Connected! 25 | Ethernet Not Connected! 26 | Ethernet Not Connected! 27 | Ethernet Connected! 28 | Ethernet Connected! 29 | Ethernet Connected! 30 | ``` 31 | -------------------------------------------------------------------------------- /connectEthernet/connectEthernet.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //#include 5 | //#include 6 | 7 | #define CSPIN 16 8 | 9 | //#define STATIC 10 | 11 | Wiznet5500lwIP eth(CSPIN); 12 | //Wiznet5100lwIP eth(CSPIN); 13 | //ENC28J60lwIP eth(CSPIN); 14 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 15 | 16 | void setup() { 17 | delay(1000); 18 | Serial.begin(115200); 19 | Serial.println(); 20 | Serial.print(ESP.getFullVersion()); 21 | Serial.println(); 22 | 23 | SPI.begin(); 24 | SPI.setBitOrder(MSBFIRST); 25 | SPI.setDataMode(SPI_MODE0); 26 | SPI.setFrequency(4000000); 27 | 28 | eth.setDefault(); // use ethernet for default route 29 | 30 | #ifdef STATIC 31 | IPAddress ip(192,168,10,130); 32 | IPAddress gateway(192,168,10,1); 33 | IPAddress subnet(255,255,255,0); 34 | IPAddress dns1(8,8,8,8); 35 | IPAddress dns2(8,8,4,4); 36 | eth.config(ip, gateway, subnet, dns1, dns2); 37 | #endif 38 | 39 | int present = eth.begin(mac); 40 | if (!present) { 41 | Serial.println("no ethernet hardware present"); 42 | while(1); 43 | } 44 | 45 | Serial.print("connecting ethernet"); 46 | while (!eth.connected()) { 47 | Serial.print("."); 48 | delay(1000); 49 | } 50 | Serial.println(); 51 | Serial.print("ethernet ip address: "); 52 | Serial.println(eth.localIP()); 53 | Serial.print("ethernet subnetMask: "); 54 | Serial.println(eth.subnetMask()); 55 | Serial.print("ethernet gateway: "); 56 | Serial.println(eth.gatewayIP()); 57 | } 58 | 59 | void loop() { 60 | //Serial.print("eth.status="); 61 | //Serial.println(eth.status()); 62 | if (eth.status() == WL_CONNECTED) { 63 | Serial.println("Ethernet Connected!"); 64 | } 65 | else { 66 | Serial.println("Ethernet Not Connected!"); 67 | } 68 | delay(1000); 69 | } 70 | -------------------------------------------------------------------------------- /connectEthernetAndWiFi/README.md: -------------------------------------------------------------------------------- 1 | # connectEthernetAndWiFi 2 | Connect Ethernet and WiFi. 3 | You can get two IP addresses: Ethernet and WiFi. 4 | 5 | You need to adapt this to your environment. 6 | ``` 7 | const char* SSID = "ssid_of_your_router"; 8 | const char* PASSWORD = "password_of_your_router"; 9 | ``` 10 | 11 | 12 | # Screen Shot 13 | ``` 14 | SDK:2.2.2-dev(38a443e)/Core:3.1.2=30102000/lwIP:STABLE-2_1_3_RELEASE/glue:1.2-65-g06164fb/BearSSL:b024386 15 | connecting ethernet... 16 | connecting wifi.... 17 | Ethernet.localIP: 192.168.10.113 18 | WiFi.localIP: 192.168.10.115 19 | Ethernet.localIP: 192.168.10.113 20 | WiFi.localIP: 192.168.10.115 21 | Ethernet.localIP: 192.168.10.113 22 | WiFi.localIP: 192.168.10.115 23 | Ethernet.localIP: 192.168.10.113 24 | WiFi.localIP: 192.168.10.115 25 | ``` 26 | -------------------------------------------------------------------------------- /connectEthernetAndWiFi/connectEthernetAndWiFi.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | //#include 5 | //#include 6 | 7 | const char* SSID = "ssid_of_your_router"; 8 | const char* PASSWORD = "password_of_your_router"; 9 | 10 | #define CSPIN 16 11 | 12 | Wiznet5500lwIP eth(CSPIN); 13 | //Wiznet5100lwIP eth(CSPIN); 14 | //ENC28J60lwIP eth(CSPIN); 15 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 16 | 17 | void setup() { 18 | delay(1000); 19 | Serial.begin(115200); 20 | Serial.println(); 21 | Serial.print(ESP.getFullVersion()); 22 | Serial.println(); 23 | 24 | SPI.begin(); 25 | SPI.setBitOrder(MSBFIRST); 26 | SPI.setDataMode(SPI_MODE0); 27 | SPI.setFrequency(4000000); 28 | 29 | eth.setDefault(); // use ethernet for default route 30 | int present = eth.begin(mac); 31 | if (!present) { 32 | Serial.println("no ethernet hardware present"); 33 | while(1); 34 | } 35 | 36 | Serial.print("connecting ethernet"); 37 | while (!eth.connected()) { 38 | Serial.print("."); 39 | delay(1000); 40 | } 41 | Serial.println(); 42 | #if 0 43 | Serial.print("ethernet ip address: "); 44 | Serial.println(eth.localIP()); 45 | Serial.print("ethernet subnetMask: "); 46 | Serial.println(eth.subnetMask()); 47 | Serial.print("ethernet gateway: "); 48 | Serial.println(eth.gatewayIP()); 49 | Serial.print("ethernet dns: "); 50 | Serial.println(eth.dnsIP()); 51 | #endif 52 | 53 | Serial.print("connecting wifi"); 54 | WiFi.mode(WIFI_STA); 55 | WiFi.begin(SSID, PASSWORD); 56 | while (WiFi.status() != WL_CONNECTED) { 57 | Serial.print("."); 58 | delay(1000); 59 | } 60 | Serial.println(); 61 | #if 0 62 | Serial.print("WiFi ip address: "); 63 | Serial.println(WiFi.localIP()); 64 | Serial.print("Wifi subnetMask: "); 65 | Serial.println(WiFi.subnetMask()); 66 | Serial.print("WiFi.gateway: "); 67 | Serial.println(WiFi.gatewayIP()); 68 | Serial.print("WiFi.dns: "); 69 | Serial.println(WiFi.dnsIP()); 70 | #endif 71 | } 72 | 73 | void loop() { 74 | if (eth.status() != WL_CONNECTED) { 75 | Serial.println("Ethernet Disconnected"); 76 | } else { 77 | Serial.print("Ethernet.localIP: "); 78 | Serial.println(eth.localIP()); 79 | } 80 | 81 | if (WiFi.status() != WL_CONNECTED) { 82 | Serial.println("Wifi Disconnected"); 83 | } else { 84 | Serial.print("WiFi.localIP: "); 85 | Serial.println(WiFi.localIP()); 86 | } 87 | delay(1000); 88 | } 89 | -------------------------------------------------------------------------------- /mqtt_pub/README.md: -------------------------------------------------------------------------------- 1 | # mqtt publish 2 | mqtt subscribe using mosquitto_sub. 3 | ``` 4 | $ sudo apt install mosquitto-clients 5 | 6 | $ sudo apt install moreutils 7 | 8 | $ mosquitto_sub -v -h broker.emqx.io -p 1883 -t "/topic/test" | ts "%y/%m/%d %H:%M:%S" 9 | 24/07/11 19:35:12 /topic/test hello world #1 10 | 24/07/11 19:35:13 /topic/test hello world #2 11 | 24/07/11 19:35:14 /topic/test hello world #3 12 | 24/07/11 19:35:15 /topic/test hello world #4 13 | 24/07/11 19:35:16 /topic/test hello world #5 14 | 24/07/11 19:35:17 /topic/test hello world #6 15 | 24/07/11 19:35:18 /topic/test hello world #7 16 | 24/07/11 19:35:19 /topic/test hello world #8 17 | 24/07/11 19:35:20 /topic/test hello world #9 18 | ``` 19 | -------------------------------------------------------------------------------- /mqtt_pub/mqtt_pub.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // https://github.com/knolleary/pubsubclient 4 | #include 5 | //#include 6 | //#include 7 | 8 | #define INTERVAL 1000 9 | #define MQTT_PORT 1883 10 | #define MQTT_TOPIC "/topic/test" // You can change 11 | #define MQTT_WILL_TOPIC "/topic/test" // You can change 12 | #define MQTT_WILL_MSG "I am leaving..." // You can change 13 | #define MQTT_SERVER "broker.emqx.io" 14 | #define PAYLOAD_SIZE 50 15 | 16 | #define CSPIN 16 17 | 18 | Wiznet5500lwIP eth(CSPIN); 19 | //Wiznet5100lwIP eth(CSPIN); 20 | //ENC28J60lwIP eth(CSPIN); 21 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 22 | 23 | WiFiClient espClient; 24 | PubSubClient client(espClient); 25 | 26 | unsigned long lastMillis; 27 | 28 | IPAddress localIP; 29 | 30 | void errorDisplay(char* buff) { 31 | Serial.print("Error:"); 32 | Serial.println(buff); 33 | while(1) { 34 | delay(200); 35 | } 36 | } 37 | 38 | void setup() { 39 | delay(1000); 40 | Serial.begin(115200); 41 | Serial.println(); 42 | Serial.print(ESP.getFullVersion()); 43 | Serial.println(); 44 | 45 | SPI.begin(); 46 | SPI.setBitOrder(MSBFIRST); 47 | SPI.setDataMode(SPI_MODE0); 48 | SPI.setFrequency(4000000); 49 | 50 | eth.setDefault(); // use ethernet for default route 51 | int present = eth.begin(mac); 52 | if (!present) { 53 | Serial.println("no ethernet hardware present"); 54 | while(1); 55 | } 56 | 57 | Serial.print("connecting ethernet"); 58 | while (!eth.connected()) { 59 | Serial.print("."); 60 | delay(1000); 61 | } 62 | Serial.println(); 63 | Serial.print("ethernet ip address: "); 64 | Serial.println(eth.localIP()); 65 | Serial.print("ethernet subnetMask: "); 66 | Serial.println(eth.subnetMask()); 67 | Serial.print("ethernet gateway: "); 68 | Serial.println(eth.gatewayIP()); 69 | localIP = eth.localIP(); 70 | 71 | client.setServer(MQTT_SERVER, MQTT_PORT); 72 | char clientid[20]; 73 | sprintf(clientid,"ESP8266-%06x",ESP.getChipId()); 74 | Serial.print("clientid="); 75 | Serial.println(clientid); 76 | Serial.print("Attempting MQTT connection..."); 77 | // Attempt to connect 78 | if (strlen(MQTT_WILL_MSG)) { 79 | // Serial.println("WILL"); 80 | if (!client.connect(clientid,MQTT_WILL_TOPIC,0,0,MQTT_WILL_MSG)) { 81 | errorDisplay("connect Fail"); 82 | } 83 | } else { 84 | // Serial.println("NOT WILL"); 85 | if (!client.connect(clientid)) { 86 | errorDisplay("connect Fail"); 87 | } 88 | } 89 | Serial.println("connected"); 90 | lastMillis = millis(); 91 | } 92 | 93 | void loop() { 94 | static int value = 0; 95 | char payload[PAYLOAD_SIZE]; 96 | 97 | client.loop(); 98 | 99 | if (Serial.available() > 0) { 100 | char inChar = Serial.read(); 101 | Serial.println("KeyIn"); 102 | client.disconnect(); 103 | Serial.println("Publish end"); 104 | while(1) { } 105 | } 106 | 107 | if (!client.connected()) { 108 | errorDisplay("not connect"); 109 | } 110 | 111 | long now = millis(); 112 | if (now - lastMillis > INTERVAL) { 113 | lastMillis = now; 114 | ++value; 115 | snprintf (payload, PAYLOAD_SIZE, "hello world #%ld", value); 116 | Serial.print("loalIP:"); 117 | Serial.print(localIP); 118 | Serial.print(" Publish message: "); 119 | Serial.println(payload); 120 | if (!client.publish(MQTT_TOPIC, payload)) { 121 | errorDisplay("publish fail"); 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /mqtt_sub/README.md: -------------------------------------------------------------------------------- 1 | # mqtt subscribe 2 | mqtt publish using mosquitto_sub. 3 | ``` 4 | $ sudo apt install mosquitto-clients 5 | 6 | $ cat mqtt_pub.sh 7 | #!/bin/bash 8 | #set -x 9 | count=1 10 | while : 11 | do 12 | payload=`date "+%Y/%m/%d %H:%M:%S"` 13 | echo ${payload} 14 | mosquitto_pub -h broker.emqx.io -p 1883 -t "/topic/test" -m "$payload" 15 | count=$((++count)) 16 | sleep 1 17 | done 18 | 19 | $ ./mqtt_pub.sh 20 | ``` 21 | -------------------------------------------------------------------------------- /mqtt_sub/mqtt_sub.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // https://github.com/knolleary/pubsubclient 4 | #include 5 | //#include 6 | //#include 7 | 8 | #define MQTT_PORT 1883 9 | #define MQTT_TOPIC "/topic/test/#" // You can change 10 | #define MQTT_WILL_TOPIC "/topic/test" // You can change 11 | #define MQTT_WILL_MSG "I am leaving..." // You can change 12 | #define MQTT_SERVER "broker.emqx.io" 13 | 14 | #define CSPIN 16 15 | 16 | Wiznet5500lwIP eth(CSPIN); 17 | //Wiznet5100lwIP eth(CSPIN); 18 | //ENC28J60lwIP eth(CSPIN); 19 | byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; 20 | 21 | WiFiClient espClient; 22 | PubSubClient client(espClient); 23 | 24 | void errorDisplay(char* buff) { 25 | Serial.print("Error:"); 26 | Serial.println(buff); 27 | while(1) { 28 | delay(200); 29 | } 30 | } 31 | 32 | void callback(char* topic, byte* payload, unsigned int length) { 33 | Serial.print("Message arrived ["); 34 | Serial.print(topic); 35 | Serial.print("] "); 36 | for (int i = 0; i < length; i++) { 37 | Serial.print((char)payload[i]); 38 | } 39 | Serial.println(); 40 | } 41 | 42 | 43 | void setup() { 44 | delay(1000); 45 | Serial.begin(115200); 46 | Serial.println(); 47 | Serial.print(ESP.getFullVersion()); 48 | Serial.println(); 49 | 50 | SPI.begin(); 51 | SPI.setBitOrder(MSBFIRST); 52 | SPI.setDataMode(SPI_MODE0); 53 | SPI.setFrequency(4000000); 54 | 55 | eth.setDefault(); // use ethernet for default route 56 | int present = eth.begin(mac); 57 | if (!present) { 58 | Serial.println("no ethernet hardware present"); 59 | while(1); 60 | } 61 | 62 | Serial.print("connecting ethernet"); 63 | while (!eth.connected()) { 64 | Serial.print("."); 65 | delay(1000); 66 | } 67 | Serial.println(); 68 | Serial.print("ethernet ip address: "); 69 | Serial.println(eth.localIP()); 70 | Serial.print("ethernet subnetMask: "); 71 | Serial.println(eth.subnetMask()); 72 | Serial.print("ethernet gateway: "); 73 | Serial.println(eth.gatewayIP()); 74 | 75 | client.setServer(MQTT_SERVER, MQTT_PORT); 76 | //client.setServer(mqtt_server, 1883); 77 | client.setCallback(callback); 78 | //server_connect("#"); 79 | 80 | char clientid[20]; 81 | sprintf(clientid,"ESP8266-%06x",ESP.getChipId()); 82 | Serial.print("clientid="); 83 | Serial.println(clientid); 84 | Serial.print("Attempting MQTT connection..."); 85 | // Attempt to connect 86 | if (strlen(MQTT_WILL_MSG)) { 87 | // Serial.println("WILL"); 88 | if (!client.connect(clientid,MQTT_WILL_TOPIC,0,0,MQTT_WILL_MSG)) { 89 | errorDisplay("connect Fail"); 90 | } 91 | } else { 92 | // Serial.println("NOT WILL"); 93 | if (!client.connect(clientid)) { 94 | errorDisplay("connect Fail"); 95 | } 96 | } 97 | Serial.println("connected"); 98 | client.subscribe(MQTT_TOPIC); 99 | } 100 | 101 | void loop() { 102 | client.loop(); // Receive MQTT Event 103 | } 104 | --------------------------------------------------------------------------------