我正在尝试将传感器数据发送到我的 MySQL 服务器,但无法实现。我在其他问题中遵循了几个类似的示例,但它不起作用。Arduino代码如下。这个想法是测量负载上的电压和电流并将其发送到 MySQL 服务器。TIA。
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7, 8);
//variables that imitates actual voltage and current data
float a=random(300.0);
float b=random(2.00);
char c[10]="B110";
void setup() {
gprsSerial.begin(19200);
Serial.begin(19200);
Serial.println("Config SIM900A...");
delay(2000);`enter code here`
Serial.println("Done!...");
gprsSerial.flush();
Serial.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\",\"my carrier apn here\"");
delay(2000);
toSerial();
// bearer settings
gprsSerial.println("AT+SAPBR=1,1");
delay(2000);
toSerial();
}
void loop() {
// initialize http service
gprsSerial.println("AT+HTTPINIT");
delay(2000);
toSerial();
// set http param value
gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://mocdl.net/api/data/create?voltage="" + a + ""¤t="" + b + "" &load_id="" + c + ""\"");
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.write(gprsSerial.read());
}
}