本帖最后由 i7456 于 2014-6-17 14:34 编辑
硬件:Microduino-GPRS/GSM、Microduino-Core+、Microduino-FT232R
实物照片:
程序如下:
- long previousMillis = 0; // will store last time LED was updated
- long interval = 10000; // interval at which to get (milliseconds)
- void setup()
- {
- Serial.begin(9600);
- Serial1.begin(9600);
- while(!gprsINI());
-
- }
- void loop()
- {
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > interval)
- {
- previousMillis = currentMillis;
-
- sim800MSG("AT+HTTPINIT");
- sim800MSG("AT+HTTPPARA="URL","http://api.yeelink.net/v1.0/device/1642/sensor/6130/datapoints"");
- sim800MSG("AT+HTTPPARA="CID",1");
- sim800MSG("AT+HTTPACTION=0");
- Serial.println(sim800MSG("AT+HTTPREAD"));
- sim800MSG("AT+HTTPTERM");
- }
- }
- boolean gprsINI()
- {
- String msg;
- msg = sim800MSG("AT+SAPBR=2,1");
- Serial.println(msg);
- sim800MSG("ATE0");
- if(msg.indexOf("OK") > 0)
- if(msg.indexOf("0.0.0.0") > 0)
- {
- sim800MSG("AT+SAPBR=3,1,"Contype","GPRS"");
- sim800MSG("AT+SAPBR=3,1,"APN","CMNET"");
- sim800MSG("AT+SAPBR=1,1");
- }
- else return 1;
- delay(2000);
- return 0;
- }
- String sim800MSG(String msg)
- {
- String str;
- boolean ActionFlag = false;
-
- if(msg.indexOf("HTTPACTION")>0)
- ActionFlag = true;
-
- Serial1.println(msg);
- while(!Serial1.available())
- delay(10);
- while(Serial1.available())
- {
- char c = Serial1.read();
- str += c;
- delay(2);
- }
-
- if(ActionFlag)
- {
- while(!Serial1.available())
- delay(10);
-
- while(Serial1.available())
- {
- char c = Serial1.read();
- str += c;
- delay(2);
- }
- }
- return str;
- }
复制代码
串口监视器输出获取的数据如下:
+SAPBR: 1,1,"10.81.114.10"
OK
+HTTPREAD: 48
{"timestamp":"2014-06-17T14:15:09","value":28.8}
OK
+HTTPREAD: 48
{"timestamp":"2014-06-17T14:15:09","value":28.8}
OK
+HTTPREAD: 48
{"timestamp":"2014-06-17T14:15:40","value":28.8}
OK
|