본문 바로가기
ESP8266

esp8266 - multicast with python pc

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

https://python-dds.tistory.com/entry/python-multicast-receive-with-esp8266

https://python-dds.tistory.com/entry/python-multicast-send-with-esp8266

 

python - multicast send with esp8266

Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import socket MCAST_GRP = '224.1.1.1' MCAST_PORT = 5123 MULTICAST_TTL = 2 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTI

python-dds.tistory.com

 

python - multicast receive with esp8266

Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import socket import struct MCAST_GRP = '224.1.1.1' MCAST_PORT = 5123 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock

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
//==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_ss;
void loop_time()
  gettimeofday(&tv, nullptr);
  static time_t lastv = 0;
  if (lastv != tv.tv_sec) {    
    lastv = tv.tv_sec;   
    now_ss = (lastv % 60);
    
    sendUDP_multicast((String)multicast_print_info());
    
  }
}
//==TIME_LOOP==END
 
 
//==MULTICAST==START
#include <WiFiUdp.h>
WiFiUDP Udp_multi;
IPAddress ipMulti(224111);
unsigned int portMulti = 5123;
#define BUF_SIZE 200
 
 
void multicast_recv_loop(){
  int noBytes = 0,msg_len =0;
 
  noBytes= Udp_multi.parsePacket();
  
  if(noBytes>0)
  {  
    if((strcmp(WiFi.localIP().toString().c_str(),Udp_multi.remoteIP().toString().c_str())))
    {
      Serial.print(Udp_multi.remoteIP());
      Serial.print(":");
      Serial.print(Udp_multi.remotePort());
      Serial.print("|");
 
      char packetBuffer[BUF_SIZE];
      msg_len=Udp_multi.read(packetBuffer,BUF_SIZE);
      
      if (msg_len>0)
      {
        packetBuffer[msg_len]=0;
        Serial.print(packetBuffer);
      }
    }
  }
}
 
 
void sendUDP_multicast(String string) {
  Serial.print("sendUDP_multicast : ");
  Serial.println(string);
  
  char msg[BUF_SIZE];
  string.toCharArray(msg,BUF_SIZE);
 
  Udp_multi.beginPacketMulticast(ipMulti, portMulti, WiFi.localIP());
  Udp_multi.write(msg);
  Udp_multi.endPacket();
}
 
 
char mcast_buf[BUF_SIZE];
char* multicast_print_info(void)
{  
  memset(mcast_buf,0,BUF_SIZE);
  sprintf(mcast_buf,"esp8266_(%d): %d",portMulti, now_ss);
 
  return mcast_buf;
}
 
//==MULTICAST==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
 
//==MULTICAST==START
  Udp_multi.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
  Serial.println("Udp_multi.beginMulticast..");
//==MULTICAST==END
 
//setup()
 
 
void loop(void) {
  
//==MULTICAST==TART
  multicast_recv_loop();
//==MULTICAST==END
 
//==TIME_LOOP==START
  loop_time();  
//==TIME_LOOP==END
 
//loop()
cs

 

Result


Connecting to xxxx-xxx

Connected: xxxx-xxx
Udp_multi.beginMulticast..
sendUDP_multicast : esp8266_(5123): 7
sendUDP_multicast : esp8266_(5123): 8
sendUDP_multicast : esp8266_(5123): 9
sendUDP_multicast : esp8266_(5123): 10
sendUDP_multicast : esp8266_(5123): 11
sendUDP_multicast : esp8266_(5123): 12
192.168.10.61:51838|python_(5123)
192.168.10.61:51838|python_(5123)
sendUDP_multicast : esp8266_(5123): 13
sendUDP_multicast : esp8266_(5123): 14
192.168.10.61:45196|python_(5123)
192.168.10.61:45196|python_(5123)
sendUDP_multicast : esp8266_(5123): 15
sendUDP_multicast : esp8266_(5123): 16
192.168.10.61:35352|python_(5123)
192.168.10.61:35352|python_(5123)
sendUDP_multicast : esp8266_(5123): 17
sendUDP_multicast : esp8266_(5123): 18
sendUDP_multicast : esp8266_(5123): 19
sendUDP_multicast : esp8266_(5123): 20

'ESP8266' 카테고리의 다른 글

esp8266 - eeprom (read, write)  (0) 2023.01.07
esp8266 - udp (send, receive)  (0) 2023.01.05
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