UDP Issues - UDP message to SignalK from ESP8266 - Printable Version

+- OpenMarine (
+-- Forum: OpenPlotter (
+--- Forum: How do I...? (
+--- Thread: UDP Issues - UDP message to SignalK from ESP8266 (/showthread.php?tid=1013)

UDP Issues - UDP message to SignalK from ESP8266 - LarsD - 02-07-2018

struggling along with SignalK and sensors. I changed from Arduino to ESP8286 wanting to reduce cabling on our boat and go for WiFi instead.

I have an ESP8286 sketch that meassures a voltage and packs this into an SignalK message to send to the SignalK server on my OpenPlotter RPI.
In the SignalK Diagnostics it doesn't show up using UDP port 55557 so I started to debug.

- First I used netcat on my RPI (with another UDP port as 55557 is in use - I assume by the SignalK server. I used 9993....) and netcat then nicely displays the JSON data as I assume SignalK wants it.

- Then I used netcat (again on my RPI) and send the same JSON message (copied from the first try) with UDP to port 55557 and then it shows up nicely in SignalK diagnostics....

Is there some sort of SignalK definition I have to do to allow other IP addresses to send UDP messages  to the SignalK server as it works nicely from netcat (on localhost)???

Can't figure this one out - anyone having some insights??? Huh

The code I use (obvioulsy with my current SSID and pwd) 

* Sending UDP messages to the OpenPlotter SignalK server using the ESP8266
* and WiFi in an IoT approach
* Adopted from Ole W. Saastad, 15th Oct 2017.
* V 0.1 - 2018-02-07 - LD - Initial version, working with netcat but not to
*                           SignalK server directly
* *******************************************************************************/

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "MySSID";
const char* password = "MyPwd";
const uint16_t SK_port = 55557;
//const uint16_t SK_port = 9933; // Used when testing with netcat (nc -ul 9933) on RPI
const char * SK_host = ""; // RPI 3 with OpenPlotter

WiFiUDP Udp;

void setup() {
 pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output

 // Connect to WiFi network
 Serial.print("Connecting to ");

 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
} /* End setup */

void loop() {
 double U;
 int val;
 char Us[6];
 String cmd;

 val = analogRead(A0); // Read the analog input on the ESP2866.
 U = ((double)val / (double)1023.0) * 19.576; // Calibrated using a multimeter, check this value carefully.
 dtostrf(U, 3, 2, Us);
 cmd =  "{\"updates\": [{\"$source\": \"ESP8266\",\"values\":[ {\"path\":\"electrical.service.voltage\",\"value\":";
 cmd += Us; cmd += "}]}]}\0"; // Not sure if NULL is needed or not, but it's good custom to terminate with NULL:

 digitalWrite(LED_BUILTIN, LOW); // Turn the LED on while we transfer the data.
 // onvert from String to character array as Udp.write will not accept string type argument.
 char cmd2[cmd.length() + 1]; // Counting from zero, add one.
 strncpy(cmd2, cmd.c_str(), sizeof(cmd2)); // Convert to char array as Udp.write don't accept strings
 Udp.beginPacket(SK_host, SK_port); // Connect to to server and prepare for UDP transfer.
 Udp.write(cmd2); // Send the message to the SignalK server.
 Udp.endPacket(); // End the connection.
 Serial.println(cmd2); Serial.print(" Message length is: "); Serial.println(sizeof(cmd2));

 digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH.

 Serial.println("wait 2 sec...");
}  /* End infinite loop */

BTW, I'm using the lates (current) version of openplotter....

I tried by piping the input i'm reading from port 9933 to port 55557
nc -ul 9933 | nc-4u -w1 55557

but nothing shows up in SignalK diagnostics...

I'm sure there is something very basic I'm doing wrong but I fail to figure out what....

RE: UDP Issues - UDP message to SignalK from ESP8266 - bhuvaneshvirani - 03-16-2018

Hi, I think ESP8266 is an excellent piece of hardware for designing simple projects. Firstly, you have to set up at the PC terminal by using network debugging tools, then ESP8266 UDP test will be sent to ESP8266 UDP port. When the UDP data are received by ESP8266, the same message will be sent to the PC terminal, too. It helps me very much to solve my doubt on working of UDP and its issues.

RE: UDP Issues - UDP message to SignalK from ESP8266 - PaddyB - 03-16-2018

I've got it working here just with  dummy message, actually 2 of esp8266 sending SOG & COG messages. 

Code is just a messy try out but works this end. The code can be uploaded over the air as well, no need to plug into the 8266 - upload over wifi, really handy.

Another way to go is to use easyesp and send the data as MQTT which openplotter can then map to a signalk update. 

#include <ESP8266WiFi.h>

#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

const char* ssid = "openplotter";
const char* password = "12345678";

unsigned int localPort = 55557;      //  port to  send  UDP packets

// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;

void setup() {
 WiFi.begin(ssid, password);
 while (WiFi.waitForConnectResult() != WL_CONNECTED) {
   Serial.println("Connection Failed! Rebooting...");

 // Port defaults to 8266
 // ArduinoOTA.setPort(8266);

 // Hostname defaults to esp8266-[ChipID]
 // ArduinoOTA.setHostname("myesp8266");

 // No authentication by default
 // ArduinoOTA.setPassword("admin");

 // Password can be set with it's md5 value as well
 // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
 // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

 ArduinoOTA.onStart([]() {
   String type;
   if (ArduinoOTA.getCommand() == U_FLASH)
     type = "sketch";
   else // U_SPIFFS
     type = "filesystem";

   // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
   Serial.println("Start updating " + type);
 ArduinoOTA.onEnd([]() {
 ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
   Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
 ArduinoOTA.onError([](ota_error_t error) {
   Serial.printf("Error[%u]: ", error);
   if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
   else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
   else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
   else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
   else if (error == OTA_END_ERROR) Serial.println("End Failed");
 Serial.print("IP address: ");
 pinMode(2, OUTPUT);

void loop() {
 digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(500); // wait for 1/2 second
 digitalWrite(2, LOW); // turn the LED off by making the voltage LOW

 int value = random(20);  // create a random vaue between 0 and 20 to simulate SOG
 String tempString = "{\"updates\": [{\"$source\": \"OPsimulation\",\"values\":[ {\"path\":\"navigation.courseOverGroundTrue\",\"value\":\"";   //  create 1st part of signalk string
 tempString += String(value);  //add simulated SOG to string
 tempString += "\"}]}]}\n" ;   // finish the signelK string

 char  replyPacekt[tempString.length()+1] ;  //initialise char array for UDP

 tempString.toCharArray(replyPacekt, tempString.length()+1);   // add the signalk string to char array

// send the char array as UDP
 IPAddress  ip = IPAddress(10,10,10,1);
 Udp.beginPacket(ip, localPort);    //signalk server on Pi listens on port 55557