Hi everyone,
My arduino is server , ı'm sending json to my c++ client socket program. and ı got unknown chars as you see.
and the other problem is when the first command I sent is not working, but the second one I sent is working for example 1 is for getting info, I'm sending but not working , when ı send again ıt works, how ı am gonna fix those problems thx.
here is my code:
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, x, x,x);
IPAddress gateway( 10, 0, 0, 1 );
IPAddress subnet(255, 255, 0, 0);
EthernetServer server(5002);
boolean alreadyConnected = false; // whether or not the client was connected previously
StaticJsonDocument<300> doc;
String inData; //char inData;
void setup() {
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
String inData; //char inData;
char json[] = "{\"model\":\"Arduino UNO R3 - USB Chip CH340\",\"micro\":\"Microcontroller: ATmega328\",\"flash\":\"Flash Memory: 32 KB (ATmega328) 0.5 KB bootloader\",\"sram\":\"SRAM: 2 KB (ATmega328)\" ,\"eeprom\":\"EEPROM: 1 KB (ATmega328)\"}";
DeserializationError error = deserializeJson(doc, json);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
const char* model = doc["model"];
const char* micro = doc["micro"];
const char* flash= doc["flash"];
const char* sram = doc["sram"];
const char* eeprom = doc["eeprom"];
// Print values.
Serial.println(model);
Serial.println(micro);
Serial.println(flash);
Serial.println(sram);
Serial.println(eeprom);
Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
inData += thisChar;
// echo the bytes to the server as well:
Serial.println(thisChar);
}
if (inData == "1") {
server.write("Information is loading..");
serializeJsonPretty(doc, client);
}
inData="";
}
}
the c++ client socket side:
// Do-while loop to send and receive data
char buf[4096];
string userInput;
cout << "Getting information about device: 1" << endl;
do
{
// Prompt the user for some text
cout << "> ";
getline(cin, userInput);
if (userInput.size() > 0) // Make sure the user has typed in something
{
// Send the text
int sendResult = send(sock, userInput.c_str(), userInput.size() + 1, 0);
if (sendResult != SOCKET_ERROR)
{
// Wait for response
ZeroMemory(buf, 4096);
int bytesReceived = recv(sock, buf, 4096, 0);
if (bytesReceived > 0)
{
// Echo response to console
cout << "SERVER> " << string(buf, 0, bytesReceived) << endl;
}
}
}
} while (userInput.size() > 0);
