我试图通过XBee S2获取传感器值到另一个连接到Arduino Mega 2560的XBee S2并将数据发送到Web服务。在这段代码中我发送一个常量数据(10,0,0),但我想发送传感器数据,直到现在我都无法获得。这是代码:
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7, 8);
SoftwareSerial xbee(0, 1);
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;
int buf[10], temp;
void setup() {
gprsSerial.begin(9600);
Serial.begin(9600);
xbee.begin(9600);
Serial.println("Config SIM900...");
delay(2000);
Serial.println("Done!...");
gprsSerial.flush(); //Waits for the transmission of outgoing serial data to complete.
// Attach or detach from GPRS service
gprsSerial.println("AT+CGATT=1");
delay(100);
toSerial();
// Bearer settings
gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
toSerial();
// Bearer settings
gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"zain\"");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=1,1");
delay(2000);
toSerial();
};
void loop() {
if (xbee.available() >= 0) {
for (int i = 1; i < 19; i++) {
byte discardByte = xbee.read();
}
int analogMSB = xbee.read();
int analogLSB = xbee.read();
int analogReading = analogLSB + (analogMSB * 256);
float phValue=(float)analogReading * 5.0 / 1024 / 6; //Convert the analog into millivolt
phValue=-35.3*phValue; //Convert the millivolt into pH value
Serial.print(" pH:");
Serial.print(phValue,2);
Serial.println(" ");
// Initialize HTTP service
gprsSerial.println("AT+HTTPINIT");
delay(2000);
toSerial();
// Set HTTP parameter value
gprsSerial.println("AT+HTTPPARA= \"URL\" ,\"http://ar.ahu.edu.jo/sensor.aspx?Sens1=10&Sens2=0&Sens3=0\"");
delay(2000);
toSerial();
// Set HTTP action type 0 = GET, 1 = POST, 2 = HEAD
gprsSerial.println("AT+HTTPACTION=0");
delay(6000);
toSerial();
// Read server response
gprsSerial.println("AT+HTTPREAD");
delay(1000);
toSerial();
gprsSerial.println("");
gprsSerial.println("AT+HTTPTERM");
toSerial();
delay(300);
gprsSerial.println("");
delay(10000);
}
}
void toSerial()
{
while(gprsSerial.available()!=0)
{
Serial.println(gprsSerial.read());
}
}