I bought the ELEGOO camera robot. This robot is compatible with ARDUINO UNO. For communication between him (web server) and my pc (client) we use WiFi. What I would like to do is set up a TCP/IP communication between my pc (client) and the robot (server - WiFi hotspot) by programming in C++. As library (pc side) for TCP/IP communication in C++, I use ASIO not Boost (standalone). To send command messages from pc to robot, JSON is used. Can you help me write a simple client program in C++ which sends a message/command to the robot using for example the following: {"H": "1", "N": 5, "D1": 1, "D2" : 90}
Write your PC client in Processing
Welcome to Processing! / Processing.org
Coding is "Arduino like" and libraries are available.
I did some UDP stuff years ago and it was straightforward.
Do take the time to review existing projects & examples.
Would you be kind enough to just show me your example, so I can start studying on it. If you want, I can send you the code I wrote, but it can't send the command to the robot, even if my client receives the connection to the server.
tcpip site:processing.org - Google Search
The forum is generally geared to "help forum" rather than do-it or code-writing. Essentially this condenses in your situation to Arduino-code on the uC and to client code on the PC. Thus, 2 parts.
MIT has a product for creating Android smartphone apps and lots of examples if you would rather not utilize Processing.
Beginner Tutorials (mit.edu)
A number of completed projects are searchable for Arduino + AppInventor and may completely eliminate most of the client effort.
But...
- Write your Arduino code
- Write your client code
- Come back with "specific" questions regarding your code
AISO not required for Processing:
Network / Libraries / Processing.org
Processing canned examples:
Examples / Processing.org
Network / Processing.org
String of the message that I have to send in Json format:
{"H": "6", "N" : 5, "D1" : 1, "D2" : 90} -> rotate 90 degree
My code for sending jason message string using ASIO:
asio::io_context context;
asio::error_code ec;
asio::ip::tcp::endpoint endpoint(asio::ip::make_address("192.168.4.1", ec), 100);
asio::ip::tcp::socket socket(context);
socket.connect(endpoint, ec);
if (socket.is_open())
{
std::string msg_j = "{"H": "1", "N": 5, "D1": 1, "D2": 90}";
socket.write_some(asio::buffer(msg_j, msg_j.size()), ec);
asio::streambuf receive_buffer;
asio::read(socket, receive_buffer, asio::transfer_all(), ec);
if (ec && ec != asio::error::eof)
{
std::cout << "receive failed: " << ec.message() << std::endl;
return 1;
}
else
{
const char* data = asio::buffer_cast<const char*>(receive_buffer.data());
std::cout << "Received: " << data << std::endl;
}
...
I'm not getting any response from the server-robot. The problem may be that the jason string is formatted as std::string. It is here that I would like some help on how to transform it into the correct jason and then into a string format to be able to give it to ASIO to send it.
Have you run your JSON through the online validator?
JSON Online Validator and Formatter - JSON Lint
"Have to" or "want to"? Such simple command structure could easily be handled in ASCII packets.
JSON is extreamly simple to fool:
Serial output directly into NodeRED - Arduino for STM32 (stm32duino.com)
I entered the string in the validator you indicated and this is the result
{
"H": "1",
"N": 5,
"D1": 1,
"D2": 90
}
So, not a JSON format issue, it would seem.
Have you added serial.Print() statements to assist in diags?
I tried running the Arduino IDE as well to see if it gets the message, but it doesn't display anything...
When I get into such a predicament, I usually stop, write very simple code with diag statements solely for testing. If the JSON is not being parsed, maybe try a different library or with something this simple, just send comma-delimited data and do your own parsing.
Hummm, on the Arduino server side, I don't want to touch the web server code that ELEGOO wrote, otherwise I risk having nothing working. At most I could try to write a client/server program on the pc using ASIO. But I've done this before and it works great.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.