将Arduino中的JSON发送到python套接字

时间:2018-02-21 12:08:27

标签: python arduino

我尝试将一些json数据从arduino发送到一个简单的python服务器。我使用ArduinoJSON来创建json数据。之后我将其转换为字符串。 其余代码与TelnetClient示例大致相同。

我的问题是服务器只接收json数据的第一个字符:b'{'。 但是如果我使用client.print("TEST");而不是client.print(json);发送常规字符串,python服务器将接收并显示完整的b'TEST'

所以我想知道json数据的转换是否适用于想象或者这是否是一种工作方法。

Arduino客户端代码:

/*
  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服务器代码:

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)

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案:String json;就是问题所在。如果将client.print(json);定义为String,json将分别发送每个字符。解决方案:将json定义为char数组char json[100];。 char数组将由client.print()作为一行中的完整字符串发送。