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

+- OpenMarine (http://forum.openmarine.net)
+-- Forum: OpenPlotter (http://forum.openmarine.net/forumdisplay.php?fid=1)
+--- Forum: How do I...? (http://forum.openmarine.net/forumdisplay.php?fid=3)
+--- 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

Hi,
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) 
/Lars

Code:
/********************************************************************************
*
* 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.
* https://sites.google.com/site/olewsaa/yacht-server-with-raspberry
* https://github.com/olewsaa/Yacht-computer
*
* 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 = "192.168.1.89"; // RPI 3 with OpenPlotter

WiFiUDP Udp;

void setup() {
 Serial.begin(38400);
 delay(10);
 pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output

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

 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);

 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
 delay(500);
 Udp.begin(SK_port);
} /* 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...");
 delay(2000);
}  /* 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 192.168.1.89 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....
/Lars