将JSON从Arduino发送到Python插座



我尝试将一些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)

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

最新更新