使用Arduino GSM / GPRS Shield将数据发送到我的Web服务

时间:2021-12-09 17:15:20

I know how to make calls and send SMSes using a GSM/GPRS shield for Arduino Uno. But I have a web service at the location http://mydomain.com/rest/receiveSensorData, and I want to send sensor data to this URL using a GSM/GPRS shield because I can not count on Wi-Fi being present at the operating locations. This is the shield I have, and I also have a 3G SIM card and am running on an Arduino Uno.

我知道如何使用GSM / GPRS屏蔽为Arduino Uno拨打电话和发送短信。但我在位置http://mydomain.com/rest/receiveSensorData上有一个Web服务,我想使用GSM / GPRS屏蔽将传感器数据发送到此URL,因为我不能指望Wi-Fi存在于经营地点。这是我的盾牌,我也有一张3G SIM卡,我正在使用Arduino Uno。

How do I do this?

我该怎么做呢?

2 个解决方案

#1


9  

You probably already got this working but just in case:

你可能已经有了这个工作,但以防万一:

In my setup I'm using the Seeed Quad Band GPRS Shield that uses the Sim900 module. I believe this code will work with yours as well after looking at the specs for yours.

在我的设置中,我使用的是使用Sim900模块的Seeed Quad Band GPRS Shield。我相信在您查看规格后,此代码也适用于您的代码。

    #include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7, 8);

void setup()
{
  gprsSerial.begin(19200);
  Serial.begin(19200);

  Serial.println("Config SIM900...");
  delay(2000);
  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\",\"epc.tmobile.com\"");
  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://YOUR.DOMAIN.COM/rest/receiveSensorData?sensorval1=blah&sensorval2=blah\"");
   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());
  }
}

Just change "epc.tmobile.com" to the apn for your carrier and "YOUR.DOMAIN.COM" to your server info and change the "sensorval1=blah" to your variable and sensor data.

只需将“epc.tmobile.com”更改为您的运营商的apn,将“YOUR.DOMAIN.COM”更改为您的服务器信息,并将“sensorval1 = blah”更改为您的变量和传感器数据。

Let me know if you get this working or not. I can help you figure it out it's not too hard.

如果你有这个工作,请告诉我。我可以帮你解决它并不太难。

Good Luck.

祝你好运。

#2


0  

It might actually be easier to keep sending SMS if the data is small and you add the ability to receive SMS info to your web application.

如果数据很小并且您添加了向Web应用程序接收SMS信息的功能,那么实际上可能更容易继续发送SMS。

See the answers to the following question in particular the first one which suggests: http://www.twilio.com/sms/:

请参阅以下问题的答案,特别是第一个建议的答案:http://www.twilio.com/sms/:

Receive SMS messages in a web application in the US on a hosted server

在托管服务器上的美国Web应用程序中接收SMS消息

One advantage of using SMS is that it will save the shield having to set up a GPRS connection which will generally take longer and may use more power. One disadvantage, to be aware of is that SMS is not a guaranteed messaging system, although you could build some sort of acknowledgment on top of SMS if you wanted.

使用SMS的一个优点是它将节省必须建立GPRS连接的屏蔽,这通常需要更长时间并且可能使用更多功率。需要注意的一个缺点是SMS不是一个有保证的消息系统,尽管如果你愿意,你可以在SMS之上建立某种确认。

As an aside, having a 3G SIM will not enable you to use 3G on a GPRS modem unless the modem also supports 3G.

另外,拥有3G SIM卡不能让你在GPRS调制解调器上使用3G,除非调制解调器也支持3G。

#1


9  

You probably already got this working but just in case:

你可能已经有了这个工作,但以防万一:

In my setup I'm using the Seeed Quad Band GPRS Shield that uses the Sim900 module. I believe this code will work with yours as well after looking at the specs for yours.

在我的设置中,我使用的是使用Sim900模块的Seeed Quad Band GPRS Shield。我相信在您查看规格后,此代码也适用于您的代码。

    #include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7, 8);

void setup()
{
  gprsSerial.begin(19200);
  Serial.begin(19200);

  Serial.println("Config SIM900...");
  delay(2000);
  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\",\"epc.tmobile.com\"");
  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://YOUR.DOMAIN.COM/rest/receiveSensorData?sensorval1=blah&sensorval2=blah\"");
   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());
  }
}

Just change "epc.tmobile.com" to the apn for your carrier and "YOUR.DOMAIN.COM" to your server info and change the "sensorval1=blah" to your variable and sensor data.

只需将“epc.tmobile.com”更改为您的运营商的apn,将“YOUR.DOMAIN.COM”更改为您的服务器信息,并将“sensorval1 = blah”更改为您的变量和传感器数据。

Let me know if you get this working or not. I can help you figure it out it's not too hard.

如果你有这个工作,请告诉我。我可以帮你解决它并不太难。

Good Luck.

祝你好运。

#2


0  

It might actually be easier to keep sending SMS if the data is small and you add the ability to receive SMS info to your web application.

如果数据很小并且您添加了向Web应用程序接收SMS信息的功能,那么实际上可能更容易继续发送SMS。

See the answers to the following question in particular the first one which suggests: http://www.twilio.com/sms/:

请参阅以下问题的答案,特别是第一个建议的答案:http://www.twilio.com/sms/:

Receive SMS messages in a web application in the US on a hosted server

在托管服务器上的美国Web应用程序中接收SMS消息

One advantage of using SMS is that it will save the shield having to set up a GPRS connection which will generally take longer and may use more power. One disadvantage, to be aware of is that SMS is not a guaranteed messaging system, although you could build some sort of acknowledgment on top of SMS if you wanted.

使用SMS的一个优点是它将节省必须建立GPRS连接的屏蔽,这通常需要更长时间并且可能使用更多功率。需要注意的一个缺点是SMS不是一个有保证的消息系统,尽管如果你愿意,你可以在SMS之上建立某种确认。

As an aside, having a 3G SIM will not enable you to use 3G on a GPRS modem unless the modem also supports 3G.

另外,拥有3G SIM卡不能让你在GPRS调制解调器上使用3G,除非调制解调器也支持3G。