Thank you, kll
So, i have implemented at least some of your advice. Unfortunately, Im still very new with Processing, so im not sure i truly understand all of what youre trying to impart on me.
One thing that im confused by is moving the curly brackets…you mentioned moving them down…and i have…but when i do, the info that i should be reading in the Processing monitor from the arduino just results in “0”, instead of the numbers i would expect to get back.
That all said, I’ve adjusted both sets of code, and well…it works…at least i think it does. That is, the numbers im getting in the Processing monitor are correct, and they are stable and are doing exactly what i want them to do. Im not so sure its elegant, (in fact, my code seems rather “brutish” and clumsy, if you will), but it works. (Again, im a noob, so some of the stuff you mentioned are confusing to me)
import processing.serial.*;
Serial myPort; // Create object from Serial class
JSONObject json, main1, main2, main3, wind1, wind2, wind3 ;
String APIKEY = "";
int temp1, temp2, temp3, humidity1, humidity2, humidity3, speed1, speed2, speed3;
int increment1 = 0;
int time_to_fetch;
int time_between_fetches = 10000; // Ten seconds.
void setup() {
String portName = Serial.list()[2]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
delay(5000);
size(640, 500);
background(102);
noStroke();
fill(0, 102);
}
void draw() {
if ( millis() > time_to_fetch ) {
loadData();
}
myPort.write(temp1 + ";" + humidity1 + ";" + speed1 + ";" + temp2 + ";" + humidity2 + ";" + speed2 + ";" + ";" + increment1 + 'e' + ";");
//myPort.write(temp2 + ";" + humidity2 + ";" + speed2 + ";" + 'e' + ";");
//myPort.write(increment1 + ";");
//myPort.write(humidity1 + ":");
//myPort.write(speed1 + ":");
//myPort.write('\r'); //send an ASCII character 10 (value)
//myPort.write(Integer.toString(humidity1));
//myPort.write(Integer.toString(speed1));
//myPort.write('e');
myPort.clear();
}
void loadData() {
increment1++;
time_to_fetch = millis() + time_between_fetches;
json = loadJSONObject("http://api.openweathermap.org/data/2.5/group?id=5964347,1266510,2347078&units=imperial&APPID="+APIKEY);
JSONArray weatherArr = json.getJSONArray("list");
//println(weatherArr);
///////////////////////////////////////////////////////////////////////
JSONObject weatherObj1 = weatherArr.getJSONObject(0);
//println("Weather Object:\n" + weatherObj, ENTER);
main1 = weatherObj1.getJSONObject("main");
wind1 = weatherObj1.getJSONObject("wind");
speed1 = wind1.getInt("speed");
humidity1 = main1.getInt("humidity");
temp1 = main1.getInt("temp");
///////////////////////////////////////////////////////////////////////
JSONObject weatherObj2 = weatherArr.getJSONObject(1);
//println("Weather Object:\n" + weatherObj, ENTER);
main2 = weatherObj2.getJSONObject("main");
wind2 = weatherObj2.getJSONObject("wind");
speed2 = wind2.getInt("speed");
humidity2 = main2.getInt("humidity");
temp2 = main2.getInt("temp");
///////////////////////////////////////////////////////////////////////
JSONObject weatherObj3 = weatherArr.getJSONObject(2);
//println("Weather Object:\n" + weatherObj, ENTER);
main3 = weatherObj3.getJSONObject("main");
wind3 = weatherObj3.getJSONObject("wind");
speed3 = wind3.getInt("speed");
humidity3 = main3.getInt("humidity");
temp3 = main3.getInt("temp");
///////////////////////////////////////////////////////////////////////
//println(temp1);
//println(humidity1);
//println(speed1);
//println(temp2);
//println(humidity2);
//println(speed2);
//println(temp3);
//println(humidity3);
//println(speed3);
//println(increment1);
drawData();
//exit();
}
void drawData() {
background(102);
textSize(18);
text("Temp1: " + nfc(temp1) +
"\nHumidity1: " + nfc(humidity1) +
"\nSpeed1: " + speed1 +
"\n " +
"\nTemp2: " + temp2 +
"\nHumidity2: " + humidity2 +
"\nSpeed2: " + speed2 +
"\n " +
"\nTemp3: " + temp3 +
"\nHumidity3: " + humidity3 +
"\nSpeed3: " + speed3 +
"\n " +
"\nIncrement: " + increment1, 10, 50);
}
void serialEvent(Serial p) {
try {
// get message till line break (ASCII > 13)
String message = p.readStringUntil(13);
// just if there is data
if (message != null) {
println("message received: "+trim(message));
}
}
catch (Exception e) {
}
}
const long BAUD_RATE = 9600;
int x1, y1, z1, x2, y2, z2, x3, y3, z3;
int a;
char c;
long v = 0;
//buffer [];
void setup() {
Serial.begin(BAUD_RATE);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
x1 = 0;
y1 = 0;
z1 = 0;
x2 = 0;
y2 = 0;
z2 = 0;
x3 = 0;
y3 = 0;
z3 = 0;
a = 0;
}
void loop() {
receiveData();
if ( a <= 4 || a >= 7) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
Serial.println(x1);
// delay(5000);
Serial.println(y1);
// delay(5000);
Serial.println(z1);
// delay(5000);
Serial.println(x2);
Serial.println(y2);
Serial.println(z2);
Serial.println(a);
delay(500);
}
void receiveData() {
if (Serial.available() >= 4) {
for (int i = 0; i < 4; i++) {
Serial.readStringUntil('e');
x1 = Serial.parseInt();
y1 = Serial.parseInt();
z1 = Serial.parseInt();
x2 = Serial.parseInt();
y2 = Serial.parseInt();
z2 = Serial.parseInt();
a = Serial.parseInt();
}
}
}
// if (c == 'e') {
// Serial.println(v);
// v = 0;
// }
// }
//}