0

I try to send some json data from an arduino to a simple python server. I use ArduinoJSON to create the json data. Afterwards I convert it to a string. The remaining code is mostly identical to the TelnetClient example.

My problem is that the server receives only the first char of the json data: b'{' . But if I send a regular string with client.print("TEST"); instead of client.print(json); ,the python server receives and displays the full b'TEST'.

So i wonder if the conversion of the json data works how imagine or if this is a working approach at all.

Arduino client code:

/*
  Telnet client

 This sketch connects to a a telnet server (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.  You'll need a telnet server 
 to test this with.
 Processing's ChatServer example (part of the network library) works well, 
 running on port 10002. It can be found as part of the examples
 in the Processing application, available at 
 http://processing.org/

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 14 Sep 2010
 modified 9 Apr 2012
 by Tom Igoe

 */

#include <SPI.h>
#include <Ethernet.h>

#include <ArduinoJson.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,2);

// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,0,4); 

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use  port 10002):
EthernetClient client;

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 13381)) {
    Serial.println("connected");
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  //JSON stuff
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["sensor"] = "gps";
  root["time"] = 42;

  JsonArray& data = root.createNestedArray("data");
  data.add(48.756080);
  data.add(2.302038);  

  String json;
  root.printTo(json);
  Serial.print(json);

  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  if (client.connected()) {
      client.print(json);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

Python server code:

import socket
import sys
import json

host = '192.168.0.4'
port = 13380
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)

print ("waiting for a connection . . .")
conn, address = server_socket.accept()
print ("Connection established: "), address

while True:
        output = conn.recv(84048);
        if output.strip() == "disconnect":
                conn.close()
                sys.exit("Disconnect message received - terminate the connection")
                conn.send("dack")
        elif output:
                print(output)
                data = json.load(output)
                print(data)
chrisg
  • 85
  • 8

1 Answers1

1

I found the solution by myself: String json; is the problem. client.print(json); will send every character separate if json is defined as String. Solution: define json as a char array char json[100];. The char array will be send by client.print() as a complete string in one line.

chrisg
  • 85
  • 8