【问题标题】:Sending JSON data (Twitter (Temboo)) from Processing to Arduino将 JSON 数据 (Twitter (Temboo)) 从 Processing 发送到 Arduino
【发布时间】:2015-01-11 12:29:34
【问题描述】:

我正在处理一个学生项目并尝试将 JSON 数据(基于 twitter 主题标签“#tune”)从 Processing 发送到 Arduino,但方法是“myPort.write(status);”不能与 JSON 一起使用,我在网上环顾四周,但不确定要使用什么命令 - 我在正确的轨道上吗?代码如下:

处理:

import processing.serial.*;  //Serial connection for Arduino
import java.lang.reflect.Method;
import com.temboo.core.*;  // Temboo library
import com.temboo.Library.Twitter.Search.*;  // Temboo Twitter search library

// Create a session using your Temboo account application details
TembooSession session = new TembooSession("MYUSERNAME", "MYTEMBOOAPPNAME", "MYTEMBOOCODE");

// Setup objects
Serial myPort;  // Create object from Serial class
int portNo = 7;  // Define portNo as int
int baudRate = 9600;  // Define baudRate as int

void setup() {
  // Run the Tweets Choreo function
  runTweetsChoreo();
  String portName = Serial.list()[portNo]; // Setup String for port ([7] is the port number for my machine)
  myPort = new Serial(this, portName, baudRate);  // Setting up serial port
}

void runTweetsChoreo() {
  // Create the Choreo object using your Temboo session
  Tweets tweetsChoreo = new Tweets(session);

  // Set credential
  tweetsChoreo.setCredential("ArduinoUkulele");

  // Set inputs

  // Run the Choreo and store the results
  TweetsResultSet tweetsResults = tweetsChoreo.run();

  // retrieve the results as JSON
  JSONObject results = parseJSONObject(tweetsResults.getResponse());

  // retrieve the statuses from the results
  JSONArray statuses = results.getJSONArray("statuses");

  // loop through the statuses
  for (int i = 0; i < statuses.size(); i++){
    JSONObject status = statuses.getJSONObject(i);
    println(status.getString("text"));
    println(" -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
    myPort.write(status);  // THIS IS THE CODE NOT WORKING WITH JSON
  }
}

阿杜诺:

char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13

void setup(){
  pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
  Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop(){
  if (Serial.available()) { // If data is available to read,
    val = Serial.read(); // read it and store it in val
    Serial.println(val);
    delay(10); // Wait 10 milliseconds for next reading
  }
}

我确定我只是在寻找某个命令 - 一旦我收到数据,我只是希望根据收到的新主题标签打开 LED。任何帮助将不胜感激!

干杯

亚瑟

【问题讨论】:

  • 我不知道处理,但在我看来,Serialwrite 方法似乎不采用 JSON 对象,而是采用字符串。你试过打电话给myPort.write(status.toString());吗?另外,请发布实际的错误消息。

标签: json arduino port processing


【解决方案1】:

write() 方法不能以 JSONObject 作为输入,只有 byte[]String。但是您已经拥有了所需的一切:只需使用 getString() 方法,就像上面两行一样(使用适当的密钥字符串,您应该从 API 中知道您在 JSON 之上使用的密钥字符串) :

 myPort.write(status.getString("text"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2020-04-16
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多