我正在使用arduino mega 2650、sim 900 GSM/GPRS 模块和 2 个xbee(版本 2)模块。温度传感器在 2 个 xbees 无线之间发送数据,然后使用 sim900 将此数据上传到网页,但由于某种原因,我无法让代码正常工作。
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7, 8);
int temp;
void setup(){
gprsSerial.begin(19200);
Serial.begin(9600);
Serial1.begin(19200);
Serial1.println("Config SIM900...");
delay(2000);
Serial1.println("Done!...");
gprsSerial.flush();
Serial1.flush();
// attach or detach from GPRS service
gprsSerial.println("AT+CGATT?");
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\",\"Umniah Internet\"");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=1,1");
delay(2000);
toSerial();
};
void loop(){
if (Serial.available() >= 21) {
if (Serial.read() == 0x7E) {
for (int i = 1; i < 19; i++) {
byte discardByte = Serial.read();
}
int analogMSB = Serial.read();
int analogLSB = Serial.read();
int analogReading = analogLSB + (analogMSB * 256);
temp = analogReading / 1023.0 * 1.23;
temp = temp - 0.5;
temp = temp / 0.01;
Serial.print(temp);
Serial.println(" degrees c");
// initialize http service
gprsSerial.println("AT+HTTPINIT");
delay(2000);
toSerial();
// set http param 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)
{
Serial1.write(gprsSerial.read());
}
}