Ciao a tutti , finalmente sono riuscito a fare funzionare i miei due xbee pro serie 1!
Partiamo innanzitutto dai collegamenti fatti:
Lato Fisso: PC fisso con attaccato l'usb shield Xbee con relativo modulo xbee pro (Tutto collegato ad esempio alla COM5)
Lato Remoto: Arduino alimentato a 9V (con una pila) (per provare più velocemente senza dovere trovare una pila e connettere il tutto, si può naturalmente provare collegando l'arduino in questione con cavo USB a un altro pc, fisso o portatile DIVERSO da quello del lato fisso) e relativa Arduino Xbee shield + modulo xbee
Come operare:
-Aprire Arduino IDE dalla postazione fissa
-Selezionare la giusta porta COM (nel mio caso COM5)
-Aprire il serial monitor da uno sketch qualsiasi
-Alimentare l'arduino remoto (come si vuole)
-Digitare dal Serial monitor precedentemente aperto, una lettera maiuscola tra le seguenti (e premere invio):
H
L
T
Con la H si dovrebbe potere accendere il LED 13
Con la L si dovrebbe potere spegnere il LED 13
Con la T si dovrebbe visualizzare le info relative al termistore!!!
Spero che questa cosa sia utile a qualcuno.
Ecco il codice (ringrazio tra le tante persone: ferex2 (per il codice sul termistore), GianfrancoPa (per i consigli sulla comunicazione Xbee) Federico Vanzati e Brainbooster, colgo l'occasione per ringraziare anche gbm, sempre disponibile):
#include <math.h>
//Schematic: modified 4.K by forex2
// [Ground] ---- [10k-Resister] -------|------- [Thermistor] ---- [+5v]
// |
// Analog Pin 0
double Thermistor(int RawADC) {
// Inputs ADC Value from Thermistor and outputs Temperature in Celsius
// requires: include <math.h>
// Utilizes the Steinhart-Hart Thermistor Equation:
// Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
long Resistance;
double Temp; // Dual-Purpose variable to save space.
Resistance=((10240000/RawADC) - 10000); // Calculation is actually: Resistance = (1024 * BalanceResistor/ADC) - BalanceResistor
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later. // "Temp" means "Temporary" on this line.
Temp = 1 / (0.0012954816 + (0.00023539242 * Temp) + (0.00000011285038 * Temp * Temp * Temp)); // Now it means both "Temporary" and "Temperature"
Temp = Temp - 273.15; // Convert Kelvin to Celsius // Now it only means "Temperature"
// BEGIN- Remove these lines for the function not to display anything
Serial.print("ADC: "); Serial.print(RawADC); Serial.print("/1024"); // Print out RAW ADC Number
Serial.print(", Volts: "); printDouble(((RawADC*5)/1024.0),3); // 5 volts is what my USB Port outputs.
Serial.print(", Resistance: "); Serial.print(Resistance); Serial.print("ohms");
// END- Remove these lines for the function not to display anything
// Uncomment this line for the function to return Fahrenheit instead.
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
return Temp; // Return the Temperature
}
void printDouble(double val, byte precision) {
// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimal places
// example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
Serial.print (int(val)); //prints the int part
if( precision > 0) {
Serial.print("."); // print the decimal point
unsigned long frac, mult = 1;
byte padding = precision -1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) Serial.print("0");
Serial.print(frac,DEC) ;
}
}
#define ThermistorPIN 0 // Analog Pin 0
#define LED 13
double temp;
char myData;
void setup() {
Serial.begin(9600);
//ATTENTION, to have results in serial monitor, you have to put this BAUD
//rate in the serial monitor or you will see some strange characters
pinMode(LED,OUTPUT);
}
void loop() {
myData = Serial.read();
if(myData == 'H'){
Serial.print(myData,BYTE); //Stampa il carattere H sul serial monitor
digitalWrite(LED,HIGH);
}
else if (myData == 'L'){
Serial.print(myData, BYTE); //Stampa il carattere L sul serial monitor
digitalWrite(LED,LOW);
}
else if (myData == 'T'){
//Non commentare la linea seguente se si vuole provare l'entrata nell'else if
//Serial.print(myData,BYTE);
// read ADC and convert it to Celsius
temp=Thermistor(analogRead(ThermistorPIN));
// display Celsius
Serial.print(", Celsius: "); printDouble(temp,3);
// End of Line
Serial.println("");
// Delay a bit...for fun, and to not Serial.print faster than the serial connection can output
delay(100);
}
}