簡體   English   中英

從處理向Arduino發送JSON數據(Twitter(Temboo))

[英]Sending JSON data (Twitter (Temboo)) from Processing to Arduino

我正在研究一個學生項目,並嘗試從Processing向Arduino發送JSON數據(基於twitter標簽“ #tune”),但方法為“ 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
  }
}

Arduino的:

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點亮。 任何幫助將不勝感激!

干杯

亞瑟

write()方法不能將JSONObject作為輸入,只能將byte []String用作輸入。 但是您已經擁有了所需的一切:只需在上面兩行中使用getString()方法(使用適當的密鑰字符串,您應該從API知道您正在JSON之上使用它):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM