본문 바로가기
ESP8266

esp8266 - udp (send, receive)

by space father 2023. 1. 5.
반응형

https://python-dds.tistory.com/entry/Python-UDP-send-receive

 

Python - UDP (send, receive)

https://arduino-dds.tistory.com/entry/esp8266-udp-send-receive esp8266 - udp (send, receive) Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

python-dds.tistory.com

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//==NodeMCU==START
#define D0 (16)
#define D1 (5//SCL
#define D2 (4//SDA
#define D3 (0)
#define D4 (2)
#define D5 (14)
#define D6 (12)
#define D7 (13)
#define D8 (15)
 
#define SD0 (7)
#define SD1 (8)
#define SD2 (9)
#define SD3 (10)
//==NodeMCU==END
 
//==WIFI==START
#include <ESP8266WiFi.h>
const char* ssid = "xxxx-xxx";
const char* password = "xxxxxxxxxx";
//==WIFI==END
 
 
//==LED BLINK==START
#define BUILTIN_LED     D4
volatile bool statusLed = false;
//==LED BLINK==END
 
 
//==TIME_LOOP==START
#include <time.h>
#include <sys/time.h>
 
timeval tv;
 
int now_second;
void loop_time()
  gettimeofday(&tv, nullptr);
  static time_t lastv = 0;
  if (lastv != tv.tv_sec) {    
    lastv = tv.tv_sec;   
    now_second = (lastv % 60);
//==UDP==START        
    udp_send((String)udp_print_info());
//==UDP==END    
  }
}
//==TIME_LOOP==END
 
//==UDP==START
#include <WiFiUdp.h>
#define UDP_SEND_PORT 54321
#define UDP_RECEIVE_PORT 51234
const char* ip_udp = "192.168.10.61";
 
#define BUF_SIZE 200
char receiveBuffer[BUF_SIZE];
char sendBuffer[BUF_SIZE];
 
WiFiUDP Udp;
 
 
 
void udp_recv_loop(void)
{
  int packetSize = Udp.parsePacket();
  if (packetSize) 
  {
    IPAddress remoteIp = Udp.remoteIP();
    int len = Udp.read(receiveBuffer, BUF_SIZE);
    if (len > 0) {
      receiveBuffer[len] = 0;
    }
 
    char buf[BUF_SIZE];
    memset(buf,0,BUF_SIZE);
    sprintf(buf,"recv: %d|%s|%d| %s\n",packetSize,Udp.remoteIP().toString().c_str(),Udp.remotePort(),receiveBuffer);
    Serial.println(buf);
  }
}
 
 
 
void udp_send(String send_message)
{
  Udp.beginPacket(ip_udp, UDP_SEND_PORT);
  
  send_message.toCharArray(sendBuffer, send_message.length()+1);
  Serial.print("send_message:");
  Serial.println(send_message);
 
  Udp.write(sendBuffer);
  Udp.endPacket();  
}
 
 
char udp_buf[BUF_SIZE];
char* udp_print_info(void)
{  
  memset(udp_buf,0,BUF_SIZE);
  sprintf(udp_buf,"esp8266_udp(%d): %d",UDP_SEND_PORT, now_second);
 
  return udp_buf;
}
 
 
//==UDP==END
 
 
 
 
 
void setup(void) {
  
  Serial.begin(115200);  
  delay(3000);
  
//==WIFI==START
  Serial.println("WIFI !");
  WiFi.mode(WIFI_STA);
  
  WiFi.begin(ssid, password);
  Serial.print("\nConnecting to "); Serial.println(ssid);
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED && i++ < 600) delay(500);
  if(i == 601){
    Serial.print("Could not connect to"); Serial.println(ssid);
    while(1) delay(500);
  }
  else
  {
    Serial.print("\nConnected: "); Serial.println(ssid);
  }  
//==WIFI==END
 
//==UDP==START  
  Udp.begin(UDP_RECEIVE_PORT);
 
  Serial.print("Udp.begin..");  
//==UDP==END
 
//setup()
 
 
void loop(void) {
 
//==UDP==START
  udp_recv_loop();
//==UDP==END  
 
 
//==TIME_LOOP==START
  loop_time();  
//==TIME_LOOP==END
 
//loop()
cs
 

Result

Connecting to xxxx-xxx

Connected: xxxx-xxx
Udp.begin..send_message:esp8266_udp(54321): 7
recv: 16|192.168.10.61|54321| Python send_data

send_message:esp8266_udp(54321): 8
recv: 16|192.168.10.61|54321| Python send_data

send_message:esp8266_udp(54321): 9
recv: 16|192.168.10.61|54321| Python send_data

send_message:esp8266_udp(54321): 10
recv: 16|192.168.10.61|54321| Python send_data

send_message:esp8266_udp(54321): 11
recv: 16|192.168.10.61|54321| Python send_data

send_message:esp8266_udp(54321): 12
recv: 16|192.168.10.61|54321| Python send_data

send_message:esp8266_udp(54321): 13
recv: 16|192.168.10.61|54321| Python send_data

send_message:esp8266_udp(54321): 14
recv: 16|192.168.10.61|54321| Python send_data

'ESP8266' 카테고리의 다른 글

esp8266 - eeprom (read, write)  (0) 2023.01.07
esp8266 - multicast with python pc  (0) 2023.01.02
ESP8266 - lcd 20x4 with ntp  (0) 2023.01.02
esp8266 - lcd 16x2 and ntp  (0) 2023.01.02
esp8266 - i2c 주소 찾기 ( find i2c address)  (0) 2023.01.02