ESP-8266 – Using UDP as a replacement for serial port debugging messages

The ESP-01 isn’t exactly bristling with I/O pins, and running out of pins is a given. Assigning an entire pin for serial debugging output seems silly, so here is a thought; just send the strings via UDP to your workstation instead.

Below are two ways to do this.

In the first instance I’m using a String class which can copy itself to a global buffer, which is fine for quick-n-dirty messages, but as you’ll see it is cumbersome for including variables.

It’s just as easy to skip the String parameter and use sprintf to pre-fill the global char buffer. You get variable formatting with sprintf for free, too. Either way works and has a place. Unless you *really* hate the String class (some folks do).

// UDP_Test - Noel Dodd 2017
// One day I'll put this on git but for now, enjoy.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define UDP_BUFFER_SIZE 256
#define DEBUG_PORT 10101
WiFiUDP UDP;
char udpBuffer[UDP_BUFFER_SIZE];
IPAddress logIP ( 172, 16, 25, 1 ); // Hardcoded destination IP address
uint8_t counter;

// ************ setup ****************
void setup() {
  // init
  counter = 0;

  // Do WiFi setup; hardcoded here for brevity
  IPAddress ip = IPAddress(172,16,25,100);
  IPAddress gw = IPAddress(172,16,25,1);
  IPAddress subnet = IPAddress(255,255,255,0);
  IPAddress dns = IPAddress(172,168,25,1);

  WiFi.persistent(false);
  WiFi.setAutoConnect(true);
  WiFi.mode(WIFI_STA);
  WiFi.config(ip,gw,subnet,dns); 
  
  int stat = WiFi.status();
  
  while (stat != WL_CONNECTED) {
    WiFi.begin("ssid", "pass");    // replace with real ssid, pass
    WiFi.waitForConnectResult();
    stat = WiFi.status();

    // optionally blink an LED here
    delay(50);
  }
  
  // Log that we are done, String method
  UDPSend( "Setup complete..." );  
}

// ************ Loop ****************
void loop() {
   // Log something (messy way, with Strings)
   String val = String(counter,DEC);
   String msg = String("String counter = " + val );
   UDPSend( msg );

   // Log something else (tidy, with sprintf)
   sprintf( udpBuffer, "Variable counter = %i", counter );
   UDPBufSend ( );

   // Do some work...
   delay(500);

   // Inc counter to see it work; let it wrap over 255
   counter++;
}

// ************ UDPSend ****************
void UDPSend ( String msg ) {

  // copy msg into buffer
  msg.toCharArray( udpBuffer, UDP_BUFFER_SIZE );
  
  UDP.beginPacket( logIP, DEBUG_PORT );
  UDP.write( udpBuffer, UDP_BUFFER_SIZE );
  UDP.endPacket();
  
}


// ************ UDPBufSend ****************
void UDPBufSend ( ) {

  UDP.beginPacket( logIP, DEBUG_PORT );
  UDP.write( udpBuffer, UDP_BUFFER_SIZE );
  UDP.endPacket();
  
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.