Interface 33

PHOTO EMBED

Sun Jan 07 2024 18:40:22 GMT+0000 (Coordinated Universal Time)

Saved by @马丽

String ioUsername = "chathurazju";
String ioKey = "aio_fTGR25ihhqnx65rtc2aTOdaL8ODV";
String feed = "led"; // Replace with your Adafruit IO feed name
String adafruitIOUrl = "https://io.adafruit.com/api/v2/" + ioUsername + "/feeds/" + feed + "/data";
String[] response;

void setup() {
  size(400, 400);
  background(240);
  fill(50);
  textAlign(LEFT);

  response = loadStrings(adafruitIOUrl + "?x-aio-key=" + ioKey);
  if (response != null) {
    parseAndDisplayData(response);
  } else {
    println("Failed to fetch data from Adafruit IO.");
  }
}

void draw() {
  // Drawing content (if necessary)
}

void parseAndDisplayData(String[] data) {
  int yPos = 20;
  for (String line : data) {
    // Check if the line starts with '[' to handle array format
    if (line.trim().startsWith("[")) {
      JSONArray jsonArray = parseJSONArray(line);
      if (jsonArray != null) {
        for (int i = 0; i < jsonArray.size(); i++) {
          JSONObject entry = jsonArray.getJSONObject(i);
          if (entry != null) {
            String id = entry.getString("id");
            String value = entry.getString("value");
            String createdAt = entry.getString("created_at");

            String displayText = "ID: " + id + "\n";
            displayText += "Value: " + value + "\n";
            displayText += "Created At: " + createdAt + "\n\n";

            text(displayText, 20, yPos);
            yPos += 100; // Adjust vertical spacing as needed
          }
        }
      }
    } else {
      // Single JSON object in a single line
      JSONObject entry = parseJSONObject(line);
      if (entry != null) {
        String id = entry.getString("id");
        String value = entry.getString("value");
        String createdAt = entry.getString("created_at");

        String displayText = "ID: " + id + "\n";
        displayText += "Value: " + value + "\n";
        displayText += "Created At: " + createdAt + "\n\n";

        text(displayText, 20, yPos);
        yPos += 100; // Adjust vertical spacing as needed
      }
    }
  }
}
content_copyCOPY