Issue
I need help troubleshooting the following. I got a really simple program sending TCP command that succesfully sends json data to my Yeelight RGB light from my laptop, but not from the raspberry pi.
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String args[]) throws Exception
{
String modifiedSentence;
Socket clientSocket = new Socket(args[0], 55443);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
PrintWriter printWriterw = new PrintWriter(outToServer);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String command = "{\"id\":1,\"method\":\"set_rgb\",\"params\":[13631232, \"smooth\", 500]}";
printWriterw.println(command);
printWriterw.flush();
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
I also have a program that acts as a TCPServer on my laptop so I can see if I can send and receive the same message to my laptop ip.
Rpi, Laptop, Yeelight -> all in same network
- run above program on laptop to Yeelight IPadress -> success
- run above program on RPI to Yeelight IPadress -> keeps waiting for response, timeout.
- run above program on laptop to Yeelight IPadress -> success
- run above program on rpi to laptop IPadress -> success (just check if json arrives)
- run above program on laptop to laptop localhost -> success receiving message is exactly the same.
- using telnet on the rpi to send the json to the yeelight ip + port -> success...
Im really lost on why the program is not working when running from my RPI.
Hope someone can help.
Solution
I believe the problem lies in the println function. The open API for yeelight states that the end of commands must have \r\n
. This is default on a windows machine (which I suppose you are using on your laptop), yet the raspberry pi is probably linux, which does newlines with \n
only. Instead of println(), manually append \r\n
to the command. I believe this will fix your issue.
For reference:
Documentation for yeelight API is found here: http://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf
On page 8 it states " All messages must be delivered in defined
JSON format on a TCP connection and each individual message must be terminated by
"\r\n
"."
Answered By - tjacobhi