首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSON数据(Twitter (Temboo))从Processing发送到Arduino

将JSON数据(Twitter (Temboo))从Processing发送到Arduino
EN

Stack Overflow用户
提问于 2015-01-11 20:29:34
回答 1查看 445关注 0票数 1

我正在做一个学生项目,并试图将JSON数据(基于推特标签'#tune')从Processing发送到Arduino,但方法'myPort.write(status);‘不适用于JSON,我在网上四处寻找,但不确定要使用什么命令--我走对了路吗?代码如下:

处理中:

代码语言:javascript
复制
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
  }
}

Arduino:

代码语言:javascript
复制
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即可。任何帮助都将不胜感激!

干杯

亚瑟

EN

回答 1

Stack Overflow用户

发布于 2015-02-07 04:45:02

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

代码语言:javascript
复制
 myPort.write(status.getString("text"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27887053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档