mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-27 21:16:50 +03:00
* WIP compile examples on host with 'make examples' * WIP bufferize tcp input * WIP Makefile * WIP network to rework, tcp/udp to factorize, udp addresses broken * minor changes to the core * WIP basic udp working * WIP mdns * WIP mcast receiving, not sending * WIP mdns OK * beta version * SSL + doc * update travis host test command * licenses * typo * doc: arduino builder is not around: declare functions before calling them * fix with latest SSL PR, compile in 32 bits mode * fix make clean * make -m32 optional * 32bits compiler ability tester * WIP * WIP (fix 1 vtable error, still another one to hunt with using spiffs) * example astyle * fix os_printf_plus * load / save mock spiffs * fix style * fix using spiffs/mock * don't mess ram * update doc * remove leftover * optimization -Os except for CI, rename ARCH32 to FORCE32 * revert useless cast (not even compiled) * remove unused function * use proper type for pointer arithmetics * makefile: sketch object and cpp file moved to bin/ directories easier to clean, and IDE don't like them * changes for review * make use of %zd * less verbose makefile by default (option) * update readme
81 lines
2.0 KiB
C++
81 lines
2.0 KiB
C++
/*
|
|
UDPSendReceive.pde:
|
|
This sketch receives UDP message strings, prints them to the serial port
|
|
and sends an "acknowledge" string back to the sender
|
|
|
|
A Processing sketch is included at the end of file that can be used to send
|
|
and received messages for testing with a computer.
|
|
|
|
created 21 Aug 2010
|
|
by Michael Margolis
|
|
|
|
This code is in the public domain.
|
|
|
|
adapted from Ethernet library examples
|
|
*/
|
|
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <WiFiUdp.h>
|
|
|
|
#define SSID "ssid"
|
|
#define PSK "psk"
|
|
|
|
unsigned int localPort = 8888; // local port to listen on
|
|
|
|
// buffers for receiving and sending data
|
|
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
|
|
char ReplyBuffer[] = "acknowledged\r\n"; // a string to send back
|
|
|
|
WiFiUDP Udp;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(SSID, PSK);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
Serial.print('.');
|
|
delay(500);
|
|
}
|
|
Serial.print("Connected! IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
Serial.printf("UDP server on port %d\n", localPort);
|
|
Udp.begin(localPort);
|
|
}
|
|
|
|
void loop() {
|
|
// if there's data available, read a packet
|
|
int packetSize = Udp.parsePacket();
|
|
if (packetSize) {
|
|
Serial.print("Received packet of size ");
|
|
Serial.println(packetSize);
|
|
Serial.print("From ");
|
|
IPAddress remote = Udp.remoteIP();
|
|
for (int i = 0; i < 4; i++) {
|
|
Serial.print(remote[i], DEC);
|
|
if (i < 3) {
|
|
Serial.print(".");
|
|
}
|
|
}
|
|
Serial.print(", port ");
|
|
Serial.println(Udp.remotePort());
|
|
|
|
// read the packet into packetBufffer
|
|
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
|
Serial.println("Contents:");
|
|
Serial.println(packetBuffer);
|
|
|
|
// send a reply, to the IP address and port that sent us the packet we received
|
|
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
|
|
Udp.write(ReplyBuffer);
|
|
Udp.endPacket();
|
|
}
|
|
delay(10);
|
|
}
|
|
|
|
/*
|
|
test (shell/netcat):
|
|
---------------
|
|
nc -u 192.168.esp.address 8888
|
|
*/
|