├── MqttPublish ├── mqtt_sub.sh ├── README.md ├── platformio.ini └── src │ └── main.cpp ├── MqttSubscribe ├── mqtt_pub.sh ├── README.md ├── platformio.ini └── src │ └── main.cpp ├── UdpBroadcastServer ├── udp-transfer.py ├── README.md ├── platformio.ini └── src │ └── main.cpp ├── UdpBroadcastClient ├── udp-receive.py ├── README.md ├── platformio.ini └── src │ └── main.cpp ├── DhcpAddressPrinter ├── README.md ├── platformio.ini └── src │ └── main.cpp ├── UdpNtpClient ├── README.md ├── platformio.ini └── src │ └── main.cpp └── README.md /MqttPublish/mqtt_sub.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # sudo apt install mosquitto-clients 3 | # sudo apt install moreutils 4 | mosquitto_sub -d -h broker.hivemq.com -p 1883 -t "/arduino/#" | ts "%y/%m/%d %H:%M:%S" 5 | #mosquitto_sub -d -h iot.eclipse.org -p 1883 -t "/arduino/#" | ts "%y/%m/%d %H:%M:%S" 6 | -------------------------------------------------------------------------------- /MqttSubscribe/mqtt_pub.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #set -x 3 | count=1 4 | while : 5 | do 6 | #payload=$(date +"%T") 7 | payload="text${count}" 8 | echo ${payload} 9 | mosquitto_pub -h broker.hivemq.com -p 1883 -t "/arduino/STM32" -m ${payload} 10 | count=$((++count)) 11 | sleep 10 12 | done 13 | -------------------------------------------------------------------------------- /UdpBroadcastServer/udp-transfer.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import time 3 | import datetime 4 | 5 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 6 | s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) 7 | 8 | while True: 9 | now = datetime.datetime.now() 10 | rmsg = "Hello UDP {}".format(now) 11 | #s.sendto(b'Hello UDP', ('255.255.255.255', 9876)) 12 | s.sendto(rmsg.encode(), ('255.255.255.255', 9876)) 13 | time.sleep(10) 14 | -------------------------------------------------------------------------------- /UdpBroadcastClient/udp-receive.py: -------------------------------------------------------------------------------- 1 | import select, socket 2 | 3 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 4 | s.bind(('', 9876)) 5 | s.setblocking(0) 6 | 7 | while True: 8 | result = select.select([s],[],[]) 9 | rbuf = result[0][0].recv(1024) 10 | #print("recv type={}".format(type(rbuf))) 11 | if (type(rbuf) is not str): 12 | msg=rbuf.decode('utf-8') 13 | #print("type(msg)={}".format(type(msg))) 14 | print(msg) 15 | 16 | -------------------------------------------------------------------------------- /UdpBroadcastClient/README.md: -------------------------------------------------------------------------------- 1 | # Using USB-TTL converter 2 | Serial.print goto PA9. 3 | ``` 4 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 5 | cd Arduino-STM32-Ethernet/UdpBroadcastClient 6 | pio run -e bluepill_f103c8 -t upload 7 | ``` 8 | 9 | # Using STM Virtual COM port 10 | Serial.print goto USB Virtual COM port. 11 | ``` 12 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 13 | cd Arduino-STM32-Ethernet/UdpBroadcastClient 14 | pio run -e bluepill_f103c8_usbcon -t upload && pio device monitor -b 115200 -p /dev/ttyACM0 15 | ``` 16 | 17 | 18 | # UDP Broadcast Receiver 19 | ``` 20 | python3 udp-receive.py 21 | ``` 22 | 23 | 24 | -------------------------------------------------------------------------------- /DhcpAddressPrinter/README.md: -------------------------------------------------------------------------------- 1 | # Using USB-TTL converter 2 | Serial.print goto PA9. 3 | ``` 4 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 5 | cd Arduino-STM32-Ethernet/DhcpAddressPrinter 6 | pio run -e bluepill_f103c8 -t upload 7 | ``` 8 | 9 | # Using STM Virtual COM port 10 | Serial.print goto USB Virtual COM port. 11 | ``` 12 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 13 | cd Arduino-STM32-Ethernet/DhcpAddressPrinter 14 | pio run -e bluepill_f103c8_usbcon -t upload && pio device monitor -b 115200 -p /dev/ttyACM0 15 | ``` 16 | 17 | # Screen Shot 18 | ![Screen_DhcpAddressPrinter](https://user-images.githubusercontent.com/6020549/169679491-367d3085-e57c-4595-af20-3ade5524b261.jpg) 19 | -------------------------------------------------------------------------------- /UdpBroadcastServer/README.md: -------------------------------------------------------------------------------- 1 | # Using USB-TTL converter 2 | Serial.print goto PA9. 3 | ``` 4 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 5 | cd Arduino-STM32-Ethernet/UdpBroadcastServer 6 | pio run -e bluepill_f103c8 -t upload 7 | ``` 8 | 9 | # Using STM Virtual COM port 10 | Serial.print goto USB Virtual COM port. 11 | ``` 12 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 13 | cd Arduino-STM32-Ethernet/UdpBroadcastServer 14 | pio run -e bluepill_f103c8_usbcon -t upload && pio device monitor -b 115200 -p /dev/ttyACM0 15 | ``` 16 | 17 | # Screen Shot 18 | ![Screen_UdpBroadcastServer](https://user-images.githubusercontent.com/6020549/169679090-74b7a624-37f4-4620-b3cd-cb2da6db2b39.jpg) 19 | 20 | # UDP Broadcast Client 21 | ``` 22 | python3 udp-transfer.py 23 | ``` 24 | -------------------------------------------------------------------------------- /UdpNtpClient/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | ``` 3 | '-D NTP_SERVER="pool.ntp.org"' 4 | -D TIME_ZONE=9 5 | ``` 6 | 7 | # Using USB-TTL converter 8 | Serial.print goto PA9. 9 | ``` 10 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 11 | cd Arduino-STM32-Ethernet 12 | cd UdpNtpClient 13 | pio run -e bluepill_f103c8 -t upload 14 | ``` 15 | 16 | # Using STM Virtual COM port 17 | Serial.print goto USB Virtual COM port. 18 | ``` 19 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 20 | cd Arduino-STM32-Ethernet 21 | cd UdpNtpClient 22 | pio run -e bluepill_f103c8_usbcon -t upload && pio device monitor -b 115200 -p /dev/ttyACM0 23 | ``` 24 | 25 | # Screen Shot 26 | ![Screen_UdpNtpClient](https://user-images.githubusercontent.com/6020549/169679093-d0ebab57-90e5-4dbb-8bd0-1006d04a4fee.jpg) 27 | 28 | -------------------------------------------------------------------------------- /MqttSubscribe/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | ``` 3 | '-D MQTT_SERVER="broker.hivemq.com"' 4 | ``` 5 | 6 | # Using USB-TTL converter 7 | Serial.print goto PA9. 8 | ``` 9 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 10 | cd Arduino-STM32-Ethernet/MqttSubscribe 11 | pio run -e bluepill_f103c8 -t upload 12 | ``` 13 | 14 | # Using STM Virtual COM port 15 | Serial.print goto USB Virtual COM port. 16 | ``` 17 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 18 | cd Arduino-STM32-Ethernet/MqttSubscribe 19 | pio run -e bluepill_f103c8_usbcon -t upload && pio device monitor -b 115200 -p /dev/ttyACM0 20 | ``` 21 | 22 | # MQTT Publish using python 23 | ``` 24 | sudo apt install mosquitto-clients moreutils 25 | chmod 777 mqtt_pub.sh 26 | ./mqtt_pub.sh 27 | ``` 28 | 29 | # Screen Shot 30 | ![Screen_MqttSubscribe](https://user-images.githubusercontent.com/6020549/169679099-aa6ff0dc-bfe4-4079-88c4-c5d03df507d3.jpg) 31 | 32 | -------------------------------------------------------------------------------- /MqttPublish/README.md: -------------------------------------------------------------------------------- 1 | # Environment 2 | ``` 3 | '-D MQTT_SERVER="broker.hivemq.com"' 4 | '-D MQTT_PAYLOAD="mqtt from stm32"' 5 | ``` 6 | 7 | # Using USB-TTL converter 8 | Serial.print goto PA9. 9 | ``` 10 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 11 | cd Arduino-STM32-Ethernet/MqttPublish 12 | pio run -e bluepill_f103c8 -t upload 13 | ``` 14 | 15 | # Using STM Virtual COM port 16 | Serial.print goto USB Virtual COM port. 17 | ``` 18 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 19 | cd Arduino-STM32-Ethernet/MqttPublish 20 | pio run -e bluepill_f103c8_usbcon -t upload && pio device monitor -b 115200 -p /dev/ttyACM0 21 | ``` 22 | 23 | # MQTT Subscribe using python 24 | ``` 25 | sudo apt install mosquitto-clients moreutils 26 | chmod 777 mqtt_sub.sh 27 | ./mqtt_sub.sh 28 | ``` 29 | 30 | # Screen Shot 31 | ![Screen_MqttPublish](https://user-images.githubusercontent.com/6020549/169679097-bfc5aed9-12e7-4507-a2fa-7a0577efec99.jpg) 32 | 33 | -------------------------------------------------------------------------------- /UdpBroadcastServer/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:bluepill_f103c8] 12 | platform = ststm32 13 | board = bluepill_f103c8 14 | framework = arduino 15 | build_flags = 16 | -D GPIO_CS=PA4 17 | upload_protocol = stlink 18 | lib_deps = 19 | https://github.com/arduino-libraries/Ethernet 20 | 21 | [env:blackpill_f103c8] 22 | platform = ststm32 23 | board = blackpill_f103c8 24 | framework = arduino 25 | build_flags = 26 | -D GPIO_CS=PA4 27 | upload_protocol = stlink 28 | lib_deps = 29 | https://github.com/arduino-libraries/Ethernet 30 | 31 | [env:bluepill_f103c8_usbcon] 32 | platform = ststm32 33 | board = bluepill_f103c8 34 | framework = arduino 35 | build_flags = 36 | -D GPIO_CS=PA4 37 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 38 | -D USBCO1 39 | upload_protocol = stlink 40 | lib_deps = 41 | https://github.com/arduino-libraries/Ethernet 42 | 43 | [env:blackpill_f103c8_usbcon] 44 | platform = ststm32 45 | board = blackpill_f103c8 46 | framework = arduino 47 | build_flags = 48 | -D GPIO_CS=PA4 49 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 50 | -D USBCO1 51 | upload_protocol = stlink 52 | lib_deps = 53 | https://github.com/arduino-libraries/Ethernet 54 | -------------------------------------------------------------------------------- /DhcpAddressPrinter/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:bluepill_f103c8] 12 | platform = ststm32 13 | board = bluepill_f103c8 14 | framework = arduino 15 | build_flags = 16 | -D GPIO_CS=PA4 17 | upload_protocol = stlink 18 | lib_deps = 19 | https://github.com/arduino-libraries/Ethernet 20 | 21 | [env:blackpill_f103c8] 22 | platform = ststm32 23 | board = blackpill_f103c8 24 | framework = arduino 25 | build_flags = 26 | -D GPIO_CS=PA4 27 | upload_protocol = stlink 28 | lib_deps = 29 | https://github.com/arduino-libraries/Ethernet 30 | 31 | [env:bluepill_f103c8_usbcon] 32 | platform = ststm32 33 | board = bluepill_f103c8 34 | framework = arduino 35 | build_flags = 36 | -D GPIO_CS=PA4 37 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 38 | -D USBCO1 39 | upload_protocol = stlink 40 | lib_deps = 41 | https://github.com/arduino-libraries/Ethernet 42 | 43 | [env:blackpill_f103c8_usbcon] 44 | platform = ststm32 45 | board = blackpill_f103c8 46 | framework = arduino 47 | build_flags = 48 | -D GPIO_CS=PA4 49 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 50 | -D USBCO1 51 | upload_protocol = stlink 52 | lib_deps = 53 | https://github.com/arduino-libraries/Ethernet 54 | 55 | -------------------------------------------------------------------------------- /UdpBroadcastClient/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | 12 | [env:bluepill_f103c8] 13 | platform = ststm32 14 | board = bluepill_f103c8 15 | framework = arduino 16 | build_flags = 17 | -D GPIO_CS=PA4 18 | upload_protocol = stlink 19 | lib_deps = 20 | https://github.com/arduino-libraries/Ethernet 21 | 22 | [env:blackpill_f103c8] 23 | platform = ststm32 24 | board = blackpill_f103c8 25 | framework = arduino 26 | build_flags = 27 | -D GPIO_CS=PA4 28 | upload_protocol = stlink 29 | lib_deps = 30 | https://github.com/arduino-libraries/Ethernet 31 | 32 | 33 | [env:bluepill_f103c8_usbcon] 34 | platform = ststm32 35 | board = bluepill_f103c8 36 | framework = arduino 37 | build_flags = 38 | -D GPIO_CS=PA4 39 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 40 | -D USBCO1 41 | upload_protocol = stlink 42 | lib_deps = 43 | https://github.com/arduino-libraries/Ethernet 44 | 45 | [env:blackpill_f103c8_usbcon] 46 | platform = ststm32 47 | board = blackpill_f103c8 48 | framework = arduino 49 | build_flags = 50 | -D GPIO_CS=PA4 51 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 52 | -D USBCO1 53 | upload_protocol = stlink 54 | lib_deps = 55 | https://github.com/arduino-libraries/Ethernet 56 | -------------------------------------------------------------------------------- /MqttSubscribe/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:bluepill_f103c8] 12 | platform = ststm32 13 | board = bluepill_f103c8 14 | framework = arduino 15 | build_flags = 16 | -D GPIO_CS=PA4 17 | '-D MQTT_SERVER="broker.hivemq.com"' 18 | upload_protocol = stlink 19 | lib_deps = 20 | https://github.com/arduino-libraries/Ethernet 21 | https://github.com/knolleary/pubsubclient 22 | 23 | [env:blackpill_f103c8] 24 | platform = ststm32 25 | board = blackpill_f103c8 26 | framework = arduino 27 | build_flags = 28 | -D GPIO_CS=PA4 29 | '-D MQTT_SERVER="broker.hivemq.com"' 30 | upload_protocol = stlink 31 | lib_deps = 32 | https://github.com/arduino-libraries/Ethernet 33 | https://github.com/knolleary/pubsubclient 34 | 35 | [env:bluepill_f103c8_usbcon] 36 | platform = ststm32 37 | board = bluepill_f103c8 38 | framework = arduino 39 | build_flags = 40 | -D GPIO_CS=PA4 41 | '-D MQTT_SERVER="broker.hivemq.com"' 42 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 43 | -D USBCO1 44 | upload_protocol = stlink 45 | lib_deps = 46 | https://github.com/arduino-libraries/Ethernet 47 | https://github.com/knolleary/pubsubclient 48 | 49 | [env:blackpill_f103c8_usbcon] 50 | platform = ststm32 51 | board = blackpill_f103c8 52 | framework = arduino 53 | build_flags = 54 | -D GPIO_CS=PA4 55 | '-D MQTT_SERVER="broker.hivemq.com"' 56 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 57 | -D USBCO1 58 | upload_protocol = stlink 59 | lib_deps = 60 | https://github.com/arduino-libraries/Ethernet 61 | https://github.com/knolleary/pubsubclient 62 | -------------------------------------------------------------------------------- /UdpNtpClient/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:bluepill_f103c8] 12 | platform = ststm32 13 | board = bluepill_f103c8 14 | framework = arduino 15 | build_flags = 16 | -D GPIO_CS=PA4 17 | '-D NTP_SERVER="pool.ntp.org"' 18 | -D TIME_ZONE=9 19 | upload_protocol = stlink 20 | lib_deps = 21 | https://github.com/arduino-libraries/Ethernet 22 | https://github.com/PaulStoffregen/Time 23 | 24 | [env:blackpill_f103c8] 25 | platform = ststm32 26 | board = blackpill_f103c8 27 | framework = arduino 28 | build_flags = 29 | -D GPIO_CS=PA4 30 | '-D NTP_SERVER="pool.ntp.org"' 31 | -D TIME_ZONE=9 32 | upload_protocol = stlink 33 | lib_deps = 34 | https://github.com/arduino-libraries/Ethernet 35 | https://github.com/PaulStoffregen/Time 36 | 37 | [env:bluepill_f103c8_usbcon] 38 | platform = ststm32 39 | board = bluepill_f103c8 40 | framework = arduino 41 | build_flags = 42 | -D GPIO_CS=PA4 43 | '-D NTP_SERVER="pool.ntp.org"' 44 | -D TIME_ZONE=9 45 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 46 | -D USBCO1 47 | upload_protocol = stlink 48 | lib_deps = 49 | https://github.com/arduino-libraries/Ethernet 50 | https://github.com/PaulStoffregen/Time 51 | 52 | [env:blackpill_f103c8_usbcon] 53 | platform = ststm32 54 | board = blackpill_f103c8 55 | framework = arduino 56 | build_flags = 57 | -D GPIO_CS=PA4 58 | '-D NTP_SERVER="pool.ntp.org"' 59 | -D TIME_ZONE=9 60 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 61 | -D USBCO1 62 | upload_protocol = stlink 63 | lib_deps = 64 | https://github.com/arduino-libraries/Ethernet 65 | https://github.com/PaulStoffregen/Time 66 | 67 | -------------------------------------------------------------------------------- /MqttPublish/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:bluepill_f103c8] 12 | platform = ststm32 13 | board = bluepill_f103c8 14 | framework = arduino 15 | build_flags = 16 | -D GPIO_CS=PA4 17 | '-D MQTT_SERVER="broker.hivemq.com"' 18 | '-D MQTT_PAYLOAD="mqtt from stm32"' 19 | upload_protocol = stlink 20 | lib_deps = 21 | https://github.com/arduino-libraries/Ethernet 22 | https://github.com/knolleary/pubsubclient 23 | 24 | [env:blackpill_f103c8] 25 | platform = ststm32 26 | board = blackpill_f103c8 27 | framework = arduino 28 | build_flags = 29 | -D GPIO_CS=PA4 30 | '-D MQTT_SERVER="broker.hivemq.com"' 31 | '-D MQTT_PAYLOAD="mqtt from stm32"' 32 | upload_protocol = stlink 33 | lib_deps = 34 | https://github.com/arduino-libraries/Ethernet 35 | https://github.com/knolleary/pubsubclient 36 | 37 | [env:bluepill_f103c8_usbcon] 38 | platform = ststm32 39 | board = bluepill_f103c8 40 | framework = arduino 41 | build_flags = 42 | -D GPIO_CS=PA4 43 | '-D MQTT_SERVER="broker.hivemq.com"' 44 | '-D MQTT_PAYLOAD="mqtt from stm32"' 45 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 46 | -D USBCO1 47 | upload_protocol = stlink 48 | lib_deps = 49 | https://github.com/arduino-libraries/Ethernet 50 | https://github.com/knolleary/pubsubclient 51 | 52 | [env:blackpill_f103c8_usbcon] 53 | platform = ststm32 54 | board = blackpill_f103c8 55 | framework = arduino 56 | build_flags = 57 | -D GPIO_CS=PA4 58 | '-D MQTT_SERVER="broker.hivemq.com"' 59 | '-D MQTT_PAYLOAD="mqtt from stm32"' 60 | -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC 61 | -D USBCO1 62 | upload_protocol = stlink 63 | lib_deps = 64 | https://github.com/arduino-libraries/Ethernet 65 | https://github.com/knolleary/pubsubclient 66 | 67 | -------------------------------------------------------------------------------- /DhcpAddressPrinter/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * W5500 Ethernet Module DHCP-based IP printer. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | // Enter a MAC address for your controller below. 9 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 10 | byte mac[] = { 11 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 12 | }; 13 | 14 | void setup() { 15 | // Open serial communications and wait for port to open: 16 | delay(1000); 17 | Serial.begin(115200); 18 | while (!Serial) { 19 | ; // wait for serial port to connect. Needed for native USB port only 20 | } 21 | 22 | // You can use Ethernet.init(pin) to configure the CS pin 23 | #if defined GPIO_CS 24 | Serial.print("GPIO_CS="); 25 | Serial.println(GPIO_CS); 26 | Ethernet.init(GPIO_CS); 27 | #else 28 | Ethernet.init(PA4); 29 | #endif 30 | 31 | 32 | // start the Ethernet connection: 33 | Serial.println("Initialize Ethernet with DHCP:"); 34 | if (Ethernet.begin(mac) == 0) { 35 | Serial.println("Failed to configure Ethernet using DHCP"); 36 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 37 | Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); 38 | } else if (Ethernet.linkStatus() == LinkOFF) { 39 | Serial.println("Ethernet cable is not connected."); 40 | } 41 | // no point in carrying on, so do nothing forevermore: 42 | while (true) { 43 | delay(1); 44 | } 45 | } 46 | 47 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 48 | Serial.println("Ethernet shield was not found."); 49 | } 50 | else if (Ethernet.hardwareStatus() == EthernetW5100) { 51 | Serial.println("W5100 Ethernet controller detected."); 52 | } 53 | else if (Ethernet.hardwareStatus() == EthernetW5200) { 54 | Serial.println("W5200 Ethernet controller detected."); 55 | } 56 | else if (Ethernet.hardwareStatus() == EthernetW5500) { 57 | Serial.println("W5500 Ethernet controller detected."); 58 | } 59 | 60 | // print your local IP address: 61 | Serial.print("Ethernet.localIP: "); 62 | Serial.println(Ethernet.localIP()); 63 | Serial.print("Ethernet.subnetMask: "); 64 | Serial.println(Ethernet.subnetMask()); 65 | Serial.print("Ethernet.gatewayIP: "); 66 | Serial.println(Ethernet.gatewayIP()); 67 | Serial.print("Ethernet.dnsServerIP: "); 68 | Serial.println(Ethernet.dnsServerIP()); 69 | } 70 | 71 | void loop() { 72 | switch (Ethernet.maintain()) { 73 | case 1: 74 | //renewed fail 75 | Serial.println("Error: renewed fail"); 76 | break; 77 | 78 | case 2: 79 | //renewed success 80 | Serial.println("Renewed success"); 81 | //print your local IP address: 82 | Serial.print("My IP address: "); 83 | Serial.println(Ethernet.localIP()); 84 | break; 85 | 86 | case 3: 87 | //rebind fail 88 | Serial.println("Error: rebind fail"); 89 | break; 90 | 91 | case 4: 92 | //rebind success 93 | Serial.println("Rebind success"); 94 | //print your local IP address: 95 | Serial.print("My IP address: "); 96 | Serial.println(Ethernet.localIP()); 97 | break; 98 | 99 | default: 100 | //nothing happened 101 | break; 102 | } 103 | } 104 | 105 | -------------------------------------------------------------------------------- /UdpBroadcastClient/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * W5500 Ethernet Module UDP Broadcast Client example. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | // Enter a MAC address for your controller below. 10 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 11 | byte mac[] = { 12 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 13 | }; 14 | 15 | // local port to listen for UDP packets 16 | #define LOCAL_PORT 8888 17 | 18 | // remote port to send for UDP packets 19 | #define REMOTE_PORT 9876 20 | 21 | // A UDP instance to let us send and receive packets over UDP 22 | EthernetUDP Udp; 23 | 24 | // Time of last packet transmission(ms) 25 | unsigned long lastSendPacketTime = 0; 26 | 27 | void setup() { 28 | // Open serial communications and wait for port to open: 29 | delay(1000); 30 | Serial.begin(115200); 31 | while (!Serial) { 32 | ; // wait for serial port to connect. Needed for native USB port only 33 | } 34 | 35 | // You can use Ethernet.init(pin) to configure the CS pin 36 | #if defined GPIO_CS 37 | Serial.print("GPIO_CS="); 38 | Serial.println(GPIO_CS); 39 | Ethernet.init(GPIO_CS); 40 | #else 41 | Ethernet.init(PA4); 42 | #endif 43 | 44 | 45 | // start the Ethernet connection: 46 | Serial.println("Initialize Ethernet with DHCP:"); 47 | if (Ethernet.begin(mac) == 0) { 48 | Serial.println("Failed to configure Ethernet using DHCP"); 49 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 50 | Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); 51 | } else if (Ethernet.linkStatus() == LinkOFF) { 52 | Serial.println("Ethernet cable is not connected."); 53 | } 54 | // no point in carrying on, so do nothing forevermore: 55 | while (true) { 56 | delay(1); 57 | } 58 | } 59 | 60 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 61 | Serial.println("Ethernet shield was not found."); 62 | } 63 | else if (Ethernet.hardwareStatus() == EthernetW5100) { 64 | Serial.println("W5100 Ethernet controller detected."); 65 | } 66 | else if (Ethernet.hardwareStatus() == EthernetW5200) { 67 | Serial.println("W5200 Ethernet controller detected."); 68 | } 69 | else if (Ethernet.hardwareStatus() == EthernetW5500) { 70 | Serial.println("W5500 Ethernet controller detected."); 71 | } 72 | 73 | // print your local IP address: 74 | Serial.print("Ethernet.localIP: "); 75 | Serial.println(Ethernet.localIP()); 76 | Serial.print("Ethernet.subnetMask: "); 77 | Serial.println(Ethernet.subnetMask()); 78 | Serial.print("Ethernet.gatewayIP: "); 79 | Serial.println(Ethernet.gatewayIP()); 80 | Serial.print("Ethernet.dnsServerIP: "); 81 | Serial.println(Ethernet.dnsServerIP()); 82 | 83 | Udp.begin(LOCAL_PORT); 84 | } 85 | 86 | void loop() { 87 | static unsigned long lastSendPacketTime = 0; 88 | unsigned long now = millis(); 89 | if (now < lastSendPacketTime) lastSendPacketTime = 0; 90 | if (now - lastSendPacketTime > 1000) { // 5 seconds passed 91 | lastSendPacketTime = now; 92 | Udp.beginPacket("255.255.255.255", REMOTE_PORT); 93 | byte packetBuffer[64]; 94 | sprintf((char *)packetBuffer, "Hello World %ld", millis()); 95 | size_t packetSize = strlen((char*)packetBuffer); 96 | Udp.write(packetBuffer, packetSize); 97 | Udp.endPacket(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /UdpBroadcastServer/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * W5500 Ethernet Module UDP Broadcast Server example. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | // Enter a MAC address for your controller below. 10 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 11 | byte mac[] = { 12 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 13 | }; 14 | 15 | // local port to listen for UDP packets 16 | #define LOCAL_PORT 9876 17 | 18 | // A UDP instance to let us send and receive packets over UDP 19 | EthernetUDP Udp; 20 | 21 | // Time of last packet transmission(ms) 22 | unsigned long lastSendPacketTime = 0; 23 | 24 | void setup() { 25 | // Open serial communications and wait for port to open: 26 | delay(1000); 27 | Serial.begin(115200); 28 | while (!Serial) { 29 | ; // wait for serial port to connect. Needed for native USB port only 30 | } 31 | 32 | // You can use Ethernet.init(pin) to configure the CS pin 33 | #if defined GPIO_CS 34 | Serial.print("GPIO_CS="); 35 | Serial.println(GPIO_CS); 36 | Ethernet.init(GPIO_CS); 37 | #else 38 | Ethernet.init(PA4); 39 | #endif 40 | 41 | 42 | // start the Ethernet connection: 43 | Serial.println("Initialize Ethernet with DHCP:"); 44 | if (Ethernet.begin(mac) == 0) { 45 | Serial.println("Failed to configure Ethernet using DHCP"); 46 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 47 | Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); 48 | } else if (Ethernet.linkStatus() == LinkOFF) { 49 | Serial.println("Ethernet cable is not connected."); 50 | } 51 | // no point in carrying on, so do nothing forevermore: 52 | while (true) { 53 | delay(1); 54 | } 55 | } 56 | 57 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 58 | Serial.println("Ethernet shield was not found."); 59 | } 60 | else if (Ethernet.hardwareStatus() == EthernetW5100) { 61 | Serial.println("W5100 Ethernet controller detected."); 62 | } 63 | else if (Ethernet.hardwareStatus() == EthernetW5200) { 64 | Serial.println("W5200 Ethernet controller detected."); 65 | } 66 | else if (Ethernet.hardwareStatus() == EthernetW5500) { 67 | Serial.println("W5500 Ethernet controller detected."); 68 | } 69 | 70 | // print your local IP address: 71 | Serial.print("Ethernet.localIP: "); 72 | Serial.println(Ethernet.localIP()); 73 | Serial.print("Ethernet.subnetMask: "); 74 | Serial.println(Ethernet.subnetMask()); 75 | Serial.print("Ethernet.gatewayIP: "); 76 | Serial.println(Ethernet.gatewayIP()); 77 | Serial.print("Ethernet.dnsServerIP: "); 78 | Serial.println(Ethernet.dnsServerIP()); 79 | 80 | Udp.begin(LOCAL_PORT); 81 | } 82 | 83 | String ipToString(IPAddress ipadr){ 84 | String result = ""; 85 | 86 | result += String(ipadr[0], 10); 87 | result += "."; 88 | result += String(ipadr[1], 10); 89 | result += "."; 90 | result += String(ipadr[2], 10); 91 | result += "."; 92 | result += String(ipadr[3], 10); 93 | 94 | return result; 95 | } 96 | 97 | void loop() { 98 | int packetSize = Udp.parsePacket(); 99 | if (packetSize) 100 | { 101 | // receive incoming UDP packets 102 | String ip = ipToString(Udp.remoteIP()); 103 | char buf[20]; 104 | ip.toCharArray(buf, sizeof(buf)); 105 | 106 | //Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP(), Udp.remotePort()); 107 | //Serial.printf("Received %d bytes from %s, port %d\n", packetSize, ipToString(Udp.remoteIP()), Udp.remotePort()); 108 | Serial.printf("Received %d bytes from %s, port %d\n", packetSize, buf, Udp.remotePort()); 109 | char rmsg[128]; 110 | int len = Udp.read(rmsg, sizeof(rmsg)); 111 | if (len > 0) { 112 | rmsg[len] = 0; 113 | Serial.printf("UDP packet contents: %s\n", rmsg); 114 | } 115 | } 116 | } 117 | 118 | -------------------------------------------------------------------------------- /MqttSubscribe/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * W5500 Ethernet Module MQTT Subscribe example. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | //#define MQTT_SERVER "broker.hivemq.com" 10 | #define MQTT_PORT 1883 11 | #define MQTT_TOPIC "/arduino/STM32" 12 | #define MQTT_WILL_TOPIC "/arduino/STM32" 13 | #define MQTT_WILL_MSG "I am leaving..." // You can change 14 | 15 | // Enter a MAC address for your controller below. 16 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 17 | byte mac[] = { 18 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 19 | }; 20 | 21 | EthernetClient ethClient; 22 | PubSubClient pubsubClient(ethClient); 23 | 24 | void callback(char* topic, byte* payload, unsigned int length) { 25 | Serial.print("Message arrived ["); 26 | Serial.print(topic); 27 | Serial.print("] ["); 28 | for (int i = 0; i < length; i++) { 29 | Serial.print((char)payload[i]); 30 | } 31 | Serial.println("]"); 32 | } 33 | 34 | void errorDisplay(char* buff) { 35 | Serial.print("Error:"); 36 | Serial.println(buff); 37 | while(1) { 38 | delay(100); 39 | } 40 | } 41 | 42 | void setup() { 43 | // Open serial communications and wait for port to open: 44 | delay(1000); 45 | Serial.begin(115200); 46 | while (!Serial) { 47 | ; // wait for serial port to connect. Needed for native USB port only 48 | } 49 | 50 | // You can use Ethernet.init(pin) to configure the CS pin 51 | #if defined GPIO_CS 52 | Serial.print("GPIO_CS="); 53 | Serial.println(GPIO_CS); 54 | Ethernet.init(GPIO_CS); 55 | #else 56 | Ethernet.init(PA4); 57 | #endif 58 | 59 | 60 | // start the Ethernet connection: 61 | Serial.println("Initialize Ethernet with DHCP:"); 62 | if (Ethernet.begin(mac) == 0) { 63 | Serial.println("Failed to configure Ethernet using DHCP"); 64 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 65 | Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); 66 | } else if (Ethernet.linkStatus() == LinkOFF) { 67 | Serial.println("Ethernet cable is not connected."); 68 | } 69 | // no point in carrying on, so do nothing forevermore: 70 | while (true) { 71 | delay(1); 72 | } 73 | } 74 | 75 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 76 | Serial.println("Ethernet shield was not found."); 77 | } 78 | else if (Ethernet.hardwareStatus() == EthernetW5100) { 79 | Serial.println("W5100 Ethernet controller detected."); 80 | } 81 | else if (Ethernet.hardwareStatus() == EthernetW5200) { 82 | Serial.println("W5200 Ethernet controller detected."); 83 | } 84 | else if (Ethernet.hardwareStatus() == EthernetW5500) { 85 | Serial.println("W5500 Ethernet controller detected."); 86 | } 87 | 88 | // print your local IP address: 89 | Serial.print("Ethernet.localIP: "); 90 | Serial.println(Ethernet.localIP()); 91 | Serial.print("Ethernet.subnetMask: "); 92 | Serial.println(Ethernet.subnetMask()); 93 | Serial.print("Ethernet.gatewayIP: "); 94 | Serial.println(Ethernet.gatewayIP()); 95 | Serial.print("Ethernet.dnsServerIP: "); 96 | Serial.println(Ethernet.dnsServerIP()); 97 | 98 | char clientid[30]; 99 | IPAddress ip = Ethernet.localIP(); 100 | sprintf(clientid,"%03d-%03d-%03d-%03d",ip[0], ip[1], ip[2], ip[3]); 101 | //strcpy(clientid, "CLIENT001"); 102 | Serial.print("clientid="); 103 | Serial.println(clientid); 104 | pubsubClient.setServer(MQTT_SERVER, MQTT_PORT); 105 | pubsubClient.setCallback(callback); 106 | 107 | // Loop until we're reconnected 108 | while (!pubsubClient.connected()) { 109 | Serial.print("Attempting MQTT connection..."); 110 | // Attempt to connect 111 | // connect (String id, String willTopic, uint8_t willQos, bool willRetain, String willMessage) 112 | if (pubsubClient.connect(clientid,MQTT_WILL_TOPIC,0,0,MQTT_WILL_MSG)) { 113 | Serial.println("connected"); 114 | } else { 115 | Serial.print("failed, rc="); 116 | Serial.print(pubsubClient.state()); 117 | Serial.println(" try again in 5 seconds"); 118 | // Wait 5 seconds before retrying 119 | delay(5000); 120 | } 121 | } 122 | 123 | if (!pubsubClient.subscribe(MQTT_TOPIC)) { 124 | errorDisplay("subscribe Fail"); 125 | } 126 | } 127 | 128 | void loop() { 129 | pubsubClient.loop(); 130 | } 131 | 132 | -------------------------------------------------------------------------------- /MqttPublish/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * W5500 Ethernet Module MQTT Publish example. 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #define INTERVAL 10 // Publish Interval [Seconds] 10 | //#define MQTT_SERVER "broker.hivemq.com" 11 | #define MQTT_PORT 1883 12 | #define MQTT_TOPIC "/arduino/STM32" 13 | #define MQTT_WILL_TOPIC "/arduino/STM32" 14 | #define MQTT_WILL_MSG "I am leaving..." // You can change 15 | 16 | // Enter a MAC address for your controller below. 17 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 18 | byte mac[] = { 19 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 20 | }; 21 | 22 | EthernetClient ethClient; 23 | PubSubClient pubsubClient(ethClient); 24 | 25 | unsigned long lastMillis; 26 | 27 | void callback(char* topic, byte* payload, unsigned int length) { 28 | Serial.print("Message arrived ["); 29 | Serial.print(topic); 30 | Serial.print("] "); 31 | for (int i = 0; i < length; i++) { 32 | Serial.print((char)payload[i]); 33 | } 34 | Serial.println(); 35 | } 36 | 37 | void errorDisplay(char* buff) { 38 | Serial.print("Error:"); 39 | Serial.println(buff); 40 | while(1) { 41 | delay(100); 42 | } 43 | } 44 | 45 | void setup() { 46 | // Open serial communications and wait for port to open: 47 | delay(1000); 48 | Serial.begin(115200); 49 | while (!Serial) { 50 | ; // wait for serial port to connect. Needed for native USB port only 51 | } 52 | 53 | // You can use Ethernet.init(pin) to configure the CS pin 54 | #if defined GPIO_CS 55 | Serial.print("GPIO_CS="); 56 | Serial.println(GPIO_CS); 57 | Ethernet.init(GPIO_CS); 58 | #else 59 | Ethernet.init(PA4); 60 | #endif 61 | 62 | 63 | // start the Ethernet connection: 64 | Serial.println("Initialize Ethernet with DHCP:"); 65 | if (Ethernet.begin(mac) == 0) { 66 | Serial.println("Failed to configure Ethernet using DHCP"); 67 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 68 | Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); 69 | } else if (Ethernet.linkStatus() == LinkOFF) { 70 | Serial.println("Ethernet cable is not connected."); 71 | } 72 | // no point in carrying on, so do nothing forevermore: 73 | while (true) { 74 | delay(1); 75 | } 76 | } 77 | 78 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 79 | Serial.println("Ethernet shield was not found."); 80 | } 81 | else if (Ethernet.hardwareStatus() == EthernetW5100) { 82 | Serial.println("W5100 Ethernet controller detected."); 83 | } 84 | else if (Ethernet.hardwareStatus() == EthernetW5200) { 85 | Serial.println("W5200 Ethernet controller detected."); 86 | } 87 | else if (Ethernet.hardwareStatus() == EthernetW5500) { 88 | Serial.println("W5500 Ethernet controller detected."); 89 | } 90 | 91 | // print your local IP address: 92 | Serial.print("Ethernet.localIP: "); 93 | Serial.println(Ethernet.localIP()); 94 | Serial.print("Ethernet.subnetMask: "); 95 | Serial.println(Ethernet.subnetMask()); 96 | Serial.print("Ethernet.gatewayIP: "); 97 | Serial.println(Ethernet.gatewayIP()); 98 | Serial.print("Ethernet.dnsServerIP: "); 99 | Serial.println(Ethernet.dnsServerIP()); 100 | 101 | Serial.print("broker is "); 102 | Serial.println(MQTT_SERVER); 103 | 104 | char clientid[30]; 105 | IPAddress ip = Ethernet.localIP(); 106 | sprintf(clientid,"%03d-%03d-%03d-%03d",ip[0], ip[1], ip[2], ip[3]); 107 | //strcpy(clientid, "CLIENT001"); 108 | Serial.print("clientid="); 109 | Serial.println(clientid); 110 | pubsubClient.setServer(MQTT_SERVER, MQTT_PORT); 111 | pubsubClient.setCallback(callback); 112 | 113 | // Loop until we're reconnected 114 | while (!pubsubClient.connected()) { 115 | Serial.print("Attempting MQTT connection..."); 116 | // Attempt to connect 117 | // connect (String id, String willTopic, uint8_t willQos, bool willRetain, String willMessage) 118 | if (pubsubClient.connect(clientid,MQTT_WILL_TOPIC,0,0,MQTT_WILL_MSG)) { 119 | Serial.println("connected"); 120 | } else { 121 | Serial.print("failed, rc="); 122 | Serial.print(pubsubClient.state()); 123 | Serial.println(" try again in 5 seconds"); 124 | // Wait 5 seconds before retrying 125 | delay(5000); 126 | } 127 | } 128 | lastMillis = 0; 129 | } 130 | 131 | void loop() { 132 | static int counter = INTERVAL; 133 | char payload[50]; 134 | 135 | pubsubClient.loop(); 136 | unsigned long now = millis(); 137 | if (now - lastMillis > 1000) { // 1 Second 138 | lastMillis = now; 139 | counter++; 140 | if (counter > INTERVAL) { 141 | Serial.print("Publish message: "); 142 | Serial.println(MQTT_PAYLOAD); 143 | if (!pubsubClient.publish(MQTT_TOPIC, MQTT_PAYLOAD) ) { 144 | errorDisplay("Failed to Publsh"); 145 | } 146 | counter=0; 147 | } 148 | } 149 | } 150 | 151 | -------------------------------------------------------------------------------- /UdpNtpClient/src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * W5500 Ethernet Module NTP Client example. 3 | * 4 | * Get the time from a Network Time Protocol (NTP) time server 5 | * Demonstrates use of UDP sendPacket and ReceivePacket 6 | * For more on NTP time servers and the messages needed to communicate with them, 7 | * see http://en.wikipedia.org/wiki/Network_Time_Protocol 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // Enter a MAC address for your controller below. 17 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 18 | byte mac[] = { 19 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 20 | }; 21 | 22 | // Your NTP Server 23 | //#define NTP_SERVER "pool.ntp.org" 24 | 25 | // Your local time zone 26 | //#define TIME_ZONE 9 27 | 28 | // local port to listen for UDP packets 29 | #define LOCAL_PORT 8888 30 | 31 | // NTP time stamp is in the first 48 bytes of the message 32 | #define NTP_PACKET_SIZE 48 33 | 34 | //buffer to hold incoming and outgoing packets 35 | byte packetBuffer[ NTP_PACKET_SIZE ]; 36 | 37 | // A UDP instance to let us send and receive packets over UDP 38 | EthernetUDP Udp; 39 | 40 | // Time of last packet transmission(ms) 41 | unsigned long lastSendPacketTime = 0; 42 | 43 | // Get Day of the week string [Sun,Mon....] 44 | char * dow_char_EN(byte days) { 45 | char *dayOfWeek[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 46 | return dayOfWeek[days]; 47 | } 48 | 49 | #if 0 50 | // Get Day of the week string (Japanese) [日曜,火曜....] 51 | char * dow_char_JP(byte days) { 52 | char *dayOfWeek[] = {"日曜","月曜","火曜","水曜","木曜","金曜","土曜"}; 53 | return dayOfWeek[days]; 54 | } 55 | #endif 56 | 57 | // Get Day of the week [0-Sunday, 1-Monday etc.] 58 | uint8_t dow(unsigned long t) { 59 | return ((t / 86400) + 4) % 7; 60 | } 61 | 62 | void showTime(char * title, time_t timet, char * dow) { 63 | Serial.print(title); 64 | Serial.print(year(timet)); 65 | Serial.print("/"); 66 | Serial.print(month(timet)); 67 | Serial.print("/"); 68 | Serial.print(day(timet)); 69 | Serial.print(" "); 70 | Serial.print(hour(timet)); 71 | Serial.print(":"); 72 | Serial.print(minute(timet)); 73 | Serial.print(":"); 74 | Serial.print(second(timet)); 75 | Serial.print(" ["); 76 | Serial.print(dow); 77 | Serial.println("]"); 78 | } 79 | 80 | // send an NTP request to the time server at the given address 81 | void sendNTPpacket() 82 | { 83 | // set all bytes in the buffer to 0 84 | memset(packetBuffer, 0, NTP_PACKET_SIZE); 85 | 86 | // Initialize values needed to form NTP request 87 | // (see URL above for details on the packets) 88 | packetBuffer[0] = 0b11100011; // LI, Version, Mode 89 | packetBuffer[1] = 0; // Stratum, or type of clock 90 | packetBuffer[2] = 6; // Polling Interval 91 | packetBuffer[3] = 0xEC; // Peer Clock Precision 92 | // 8 bytes of zero for Root Delay & Root Dispersion 93 | packetBuffer[12] = 49; 94 | packetBuffer[13] = 0x4E; 95 | packetBuffer[14] = 49; 96 | packetBuffer[15] = 52; 97 | 98 | // all NTP fields have been given values, now 99 | // you can send a packet requesting a timestamp: 100 | Udp.beginPacket(NTP_SERVER, 123); //NTP requests are to port 123 101 | Udp.write(packetBuffer,NTP_PACKET_SIZE); 102 | Udp.endPacket(); 103 | 104 | return; 105 | } 106 | 107 | void setup() { 108 | // Open serial communications and wait for port to open: 109 | delay(1000); 110 | Serial.begin(115200); 111 | while (!Serial) { 112 | ; // wait for serial port to connect. Needed for native USB port only 113 | } 114 | 115 | // You can use Ethernet.init(pin) to configure the CS pin 116 | #if defined GPIO_CS 117 | Serial.print("GPIO_CS="); 118 | Serial.println(GPIO_CS); 119 | Ethernet.init(GPIO_CS); 120 | #else 121 | Ethernet.init(PA4); 122 | #endif 123 | 124 | 125 | // start the Ethernet connection: 126 | Serial.println("Initialize Ethernet with DHCP:"); 127 | if (Ethernet.begin(mac) == 0) { 128 | Serial.println("Failed to configure Ethernet using DHCP"); 129 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 130 | Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); 131 | } else if (Ethernet.linkStatus() == LinkOFF) { 132 | Serial.println("Ethernet cable is not connected."); 133 | } 134 | // no point in carrying on, so do nothing forevermore: 135 | while (true) { 136 | delay(1); 137 | } 138 | } 139 | 140 | if (Ethernet.hardwareStatus() == EthernetNoHardware) { 141 | Serial.println("Ethernet shield was not found."); 142 | } 143 | else if (Ethernet.hardwareStatus() == EthernetW5100) { 144 | Serial.println("W5100 Ethernet controller detected."); 145 | } 146 | else if (Ethernet.hardwareStatus() == EthernetW5200) { 147 | Serial.println("W5200 Ethernet controller detected."); 148 | } 149 | else if (Ethernet.hardwareStatus() == EthernetW5500) { 150 | Serial.println("W5500 Ethernet controller detected."); 151 | } 152 | 153 | // print your local IP address: 154 | Serial.print("Ethernet.localIP: "); 155 | Serial.println(Ethernet.localIP()); 156 | Serial.print("Ethernet.subnetMask: "); 157 | Serial.println(Ethernet.subnetMask()); 158 | Serial.print("Ethernet.gatewayIP: "); 159 | Serial.println(Ethernet.gatewayIP()); 160 | Serial.print("Ethernet.dnsServerIP: "); 161 | Serial.println(Ethernet.dnsServerIP()); 162 | 163 | // Resolving host names 164 | DNSClient dns; 165 | IPAddress ServerIP; 166 | dns.begin(Ethernet.dnsServerIP()); 167 | if(dns.getHostByName(NTP_SERVER,ServerIP) == 1) { 168 | Serial.println(F("dns lookup success")); 169 | } else { 170 | Serial.println(F("dns lookup failed")); 171 | while(1) { } 172 | } 173 | 174 | Udp.begin(LOCAL_PORT); 175 | Serial.println("Started listening for response."); 176 | } 177 | 178 | void loop() { 179 | long now = millis(); 180 | if (now - lastSendPacketTime > 5000) { // 5 seconds passed 181 | lastSendPacketTime = now; 182 | sendNTPpacket(); 183 | } 184 | 185 | // wait to see if a reply is available 186 | if ( Udp.parsePacket() ) { 187 | Serial.println("\nNTP Client"); 188 | 189 | // We've received a packet, read the data from it 190 | Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer 191 | 192 | //the timestamp starts at byte 40 of the received packet and is four bytes, 193 | // or two words, long. First, esxtract the two words: 194 | 195 | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); 196 | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); 197 | // combine the four bytes (two words) into a long integer 198 | // this is NTP time (seconds since Jan 1 1900): 199 | unsigned long secsSince1900 = highWord << 16 | lowWord; 200 | Serial.print("Seconds since Jan 1 1900 = " ); 201 | Serial.println(secsSince1900); 202 | 203 | // now convert NTP time into everyday time: 204 | // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: 205 | const unsigned long seventyYears = 2208988800UL; 206 | // subtract seventy years: 207 | unsigned long epoch = secsSince1900 - seventyYears; 208 | // print Unix time: 209 | Serial.print("Unix time = "); 210 | Serial.println(epoch); 211 | 212 | // Greenwich Mean Time 213 | uint8_t DayOfWeek = dow(epoch); 214 | showTime("The UTC time is ", epoch, dow_char_EN(DayOfWeek)); 215 | 216 | if (TIME_ZONE != 0) { 217 | // Local Time 218 | DayOfWeek = dow(epoch + (TIME_ZONE * 60 * 60)); 219 | showTime("The LOCAL time is ", epoch + (TIME_ZONE * 60 * 60), dow_char_EN(DayOfWeek)); 220 | } 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino-STM32-Ethernet 2 | Arduino stm32 example for W5x00 ethernet modules. 3 | This project uses [this](https://github.com/arduino-libraries/Ethernet) library. 4 | 5 | 6 | # Software requirements 7 | PlatformIO will automatically install the required libraries according to the ini file. 8 | Great! 9 | 10 | # Platform IO installation 11 | [PlatformIO](https://github.com/platformio/platformio-core) 12 | ``` 13 | $ python3 --version 14 | Python 3.7.3 15 | 16 | $ sudo apt install python3-pip python3-setuptools 17 | 18 | $ python3 -m pip -V 19 | pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7) 20 | 21 | # pip installed by apt is old, so update to the latest. 22 | $ python3 -m pip install -U pip 23 | 24 | $ python3 -m pip -V 25 | pip 20.3.1 from /home/orangepi/.local/lib/python3.7/site-packages/pip (python 3.7) 26 | 27 | # Update wheels. 28 | $ python3 -m pip install -U wheel 29 | 30 | $ pip3 install -U platformio 31 | ``` 32 | 33 | # Hardware requirements 34 | - STM32F103 Development Board 35 | - W5100/W5200/W5500 Ethernet Module 36 | About $5 for aliexpress/eBay. 37 | W5100 is for 5V only. 38 | W5500 can be used at both 3.3V / 5V. 39 | - ST-LINK/V2 or ST-LINK/V2.1 Module 40 | Arduino-IDE only supports ST-LINK V2.1 adapters. 41 | But OpenOCD used by PlatformIO supports both V2.0 and V2.1. 42 | PlatformIO allows you to use cheap Chinese ST-LINK adapters. 43 | You can get it at a low price (about $2). 44 | ![ST-LINK-1](https://user-images.githubusercontent.com/6020549/221345715-b86e0a93-bdf4-46dd-a9b9-c05a27b042a4.JPG) 45 | ![ST-LINK-2](https://user-images.githubusercontent.com/6020549/221345711-7749b557-d55f-442f-8390-3632c63d5239.JPG) 46 | You can use NECLEO board as ST-LINK/V2.1 47 | - USB-TTL Converter 48 | For Serial monitoring. 49 | - External Voltage Regurator 50 | W5x00 ethernet modules require a lot of current. 51 | BluePill or BlackPill board cannot supply too much current. 52 | It is more stable when supplied from an external power source. 53 | AMS1117 is often used. 54 | 55 | # Selecting the Ethernet type controller (W5100, W5200 or W5500) 56 | It is automatically selected inside the library. 57 | 58 | # Wiring 59 | 60 | |PHY|STM32|| 61 | |:-:|:-:|:-:| 62 | |MOSI|PA7|| 63 | |MISO|PA6|| 64 | |SCLK|PA5|| 65 | |SS|PA4|*1| 66 | |RST|RESET|*2| 67 | |3.3V|3.3V|*3| 68 | |GND|GND|*3| 69 | 70 | (*1) 71 | You can specify any pin in platformio.ini. 72 | 73 | (*2) 74 | Pull up when there is no RESET pin. 75 | 76 | (*3) 77 | BluePill or BlackPill board cannot supply too much current. 78 | It is more stable when supplied from an external power source. 79 | 80 | - Supplied from ST-LINK 81 | ``` 82 | +----------+ +----------+ +----------+ 83 | |BluePill | |ST-LINK | |HOST | 84 | |BlackPill | | [------------] | 85 | | |------------|SWD-IO [ USB ] | 86 | | |------------|SWD-CLK [------------] | 87 | | |------------|GND | | | 88 | | |------------|3V3 | | | 89 | | | +----------+ | | 90 | | | | | 91 | | | +----------+ | | 92 | +----------+ | PA9|------------|RX | | | 93 | | |---(MOSI)---| GND|------------|GND | | | 94 | | |---(MISO)---| | | | | | 95 | | |---(SCLK)---| | | | | | 96 | | |---(SS)-----| | | USB-TTL [------------] | 97 | | |---(RESET)--| | | [ USB ] | 98 | | | | | | [/dev/ttyUSB0] | 99 | | PHY | | | | [------------] | 100 | | | +----------+ | | | | 101 | | | | | | | 102 | | | +----------+ | | | | 103 | | |---(3V3)----|5v->3.3V |----(5V)----| | | | 104 | | |---(GND)----|Regulator |----(GND)---| | | | 105 | +----------+ +----------+ +----------+ +----------+ 106 | ``` 107 | 108 | - Supplied from USB 109 | ``` 110 | +----------+ +----------+ +----------+ 111 | |BluePill | |ST-LINK | |HOST | 112 | |BlackPill | | [------------] | 113 | | |------------|SWD-IO [ USB ] | 114 | | |------------|SWD-CLK [------------] | 115 | | |------------|GND | | | 116 | | | | | | | 117 | | | +----------+ | | 118 | | | | | 119 | | [------------------------------------] | 120 | +----------+ | [ USB ] | 121 | | |---(MOSI)---| [------------------------------------] | 122 | | |---(MISO)---| | | | 123 | | |---(SCLK)---| | | | 124 | | |---(SS)-----| |-------+ | | 125 | | |---(RESET)--| |---+ | | | 126 | | | | | | | | | 127 | | PHY | | | | (GND) | | 128 | | | +----------+ (5V) | | | 129 | | | | | | | 130 | | | +----------+ | | | | 131 | | |---(3V3)----|5v->3.3V |---+ | | | 132 | | |---(GND)----|Regulator |-------+ | | 133 | +----------+ +----------+ +----------+ 134 | ``` 135 | 136 | - Supplied from USB-TTL(Without firmware flash) 137 | ``` 138 | +----------+ +----------+ +----------+ 139 | |BluePill | |USB-TTL | |HOST | 140 | |BlackPill | | [------------] | 141 | | |----(5V)----| [ USB ] | 142 | | |----(GND)---| [------------] | 143 | | | | | | | 144 | | | | | | | 145 | | | | | | | 146 | | | | | | | 147 | | | | | | | 148 | +----------+ | PA9|------------|RX | | | 149 | | |---(MOSI)---| | | | | | 150 | | |---(MISO)---| | | | | | 151 | | |---(SCLK)---| | | | | | 152 | | |---(SS)-----| |-------+ | | | | 153 | | |---(RESET)--| |---+ | | | | | 154 | | | | | | | | | | | 155 | | PHY | | | | (GND) | | | | 156 | | | +----------+ (5V) | | | | | 157 | | | | | | | | | 158 | | | +----------+ | | | | | | 159 | | |---(3V3)----|5v->3.3V |---+ | | | | | 160 | | |---(GND)----|Regulator |-------+ | | | | 161 | +----------+ +----------+ +----------+ +----------+ 162 | ``` 163 | 164 | - Supplied from USB Port(Without firmware flash) 165 | ``` 166 | +----------+ +----------+ 167 | |BluePill | |HOST | 168 | |BlackPill [------------------------------------] | 169 | | [ USB ] | 170 | | [------------------------------------] | 171 | | | | | 172 | | | | | 173 | | | | | 174 | | | | | 175 | | | | | 176 | +----------+ | | | | 177 | | |---(MOSI)---| | | | 178 | | |---(MISO)---| | | | 179 | | |---(SCLK)---| | | | 180 | | |---(SS)-----| |-------+ | | 181 | | |---(RESET)--| |---+ | | | 182 | | | | | | | | | 183 | | PHY | | | | (GND) | | 184 | | | +----------+ (5V) | | | 185 | | | | | | | 186 | | | +----------+ | | | | 187 | | |---(3V3)----|5v->3.3V |---+ | | | 188 | | |---(GND)----|Regulator |-------+ | | 189 | +----------+ +----------+ +----------+ 190 | ``` 191 | 192 | 193 | # Using USB-TTL converter 194 | Serial.print goto PA9. 195 | ``` 196 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 197 | cd Arduino-STM32-Ethernet/DhcpAddressPrinter 198 | pio run -t upload -e bluepill_f103c8 199 | ``` 200 | 201 | # Using STM Virtual COM port 202 | Serial.print is output to the USB virtual COM port. 203 | /dev/ttyACM0 may change. 204 | ``` 205 | git clone https://github.com/nopnop2002/Arduino-STM32-Ethernet 206 | cd Arduino-STM32-Ethernet/DhcpAddressPrinter 207 | pio run -e bluepill_f103c8_usbcon -t upload && pio device monitor -b 115200 -p /dev/ttyACM0 208 | ``` 209 | 210 | # Note 211 | - I has not been tested on a real W5200 controller. 212 | - It cannot retrieve the Internal MAC address from WIZ550io. 213 | 214 | 215 | # More information 216 | See [here](https://www.arduino.cc/en/Reference/Ethernet). 217 | 218 | --------------------------------------------------------------------------------