備忘錄_20160105(定位)
修改
回首頁
程式 2026-05-13 01:06:16 1778605576 100
lilygo t-sim7600g-h 送出網路指令,測試中
#define MODEM_RX 26
#define MODEM_TX 27
HardwareSerial SerialAT(1);
const int iBufferMax=1024;
char caBuffer[iBufferMax];
const int iEncodedDataMax=1024;
char caEncodedData[iEncodedDataMax];
char caData[iEncodedDataMax];
char caGps[iEncodedDataMax];
void setup()
{
Serial.begin(115200);
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(3000);
Serial.println("");
Serial.println("wait...");
while(getBooQA("AT", "OK")==false) { delay(1000); }
while(true)
{
if(getBooQA("AT", "OK")==false) { break; }
Serial.println("AT OK");
if(getBooQA("AT+CGDCONT=1,\"IP\",\"internet\"", "OK")==false) { break; }
Serial.println("CGDCONT OK");
break;
}
qa("AT+CGPS=0", caBuffer);
delay(3000);
qa("AT+CGPS=1", caBuffer);
Serial.println("setup finishd");
}
void sendData(const char *cData)
{
while(true)
{
if(getBooQA("AT+CPIN?", "+CPIN: READY")==false) { break; }
Serial.println("CPIN OK");
if(getBooQA("AT+CGATT=1", "OK")==false) { break; }
Serial.println("CGATT SET");
if(getBooQA("AT+CGATT?", "+CGATT: 1")==false) { break; }
Serial.println("CGATT REALLY OK");
if(getBooQA("AT+HTTPINIT", "OK")==false) { break; }
Serial.println("HTTPINIT OK");
urlEncode(cData, caEncodedData, iEncodedDataMax);
Serial.println(caEncodedData);
char caCmd[1024];
snprintf(
caCmd,
sizeof(caCmd),
"AT+HTTPPARA=\"URL\",\"http://liujiaje.com/resources/20260513/index.php"
"?data=%s\"",
caEncodedData);
if(getBooQA(caCmd, "OK")==false) { break; }
Serial.println("HTTPPARA OK");
if(getBooQA("AT+HTTPACTION=0", "OK")==false) { break; }
Serial.println("HTTPACTION OK");
getBooQA("AT+HTTPREAD=0,200", ".");
Serial.println("read data...");
delay(1000);
if(getBooQA("AT+HTTPTERM", "OK")==false) { break; }
Serial.println("HTTPTERM OK");
break;
}
}
bool getBooQA(const char *cQ, const char *cKW)
{
qa(cQ, caBuffer);
if(!strstr(caBuffer, cKW)) { return false; }
return true;
}
void qa(const char *cQ, char cA[])
{
while(SerialAT.available()) { SerialAT.read(); }
SerialAT.write(cQ);
SerialAT.write("\r\n");
delay(50);
int iLen=0;
unsigned long ulLast=millis();
while((millis()-ulLast)<3000)
{
while(SerialAT.available())
{
char c1=SerialAT.read();
ulLast=millis();
if(iLen<(iBufferMax-1))
{
cA[iLen]=c1;
iLen++;
cA[iLen]='\0';
}
}
if(strstr(cA, "OK") || strstr(cA, "ERROR")) { break; }
}
delay(50);
}
bool getBooSafeChar(char c)
{
if ((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))
{
return true;
}
if (c == '-' || c == '_' || c == '.' || c == '~')
{
return true;
}
return false;
}
void urlEncode(const char *cIn, char *cOut, int iOutMax)
{
int iIn=0;
int iOut=0;
while(cIn[iIn]!='\0' && iOut<(iOutMax-1))
{
char c = cIn[iIn];
if(c=='\r' || c=='\n')
{
}
else if(getBooSafeChar(c))
{
cOut[iOut++]=c;
}
else
{
if(iOut+3>=iOutMax) { break; }
sprintf(&cOut[iOut], "%%%02X", (unsigned char)c);
iOut+=3;
}
iIn++;
}
cOut[iOut]='\0';
}
bool extractGPS(char *in, char *out, int outMax)
{
const char *key = "+CGPSINFO:";
int i = 0;
int o = 0;
bool found = false;
while(in[i] != '\0')
{
// 1. 找到 +CGPSINFO:
if(!found)
{
if(strncmp(&in[i], key, strlen(key)) == 0)
{
found = true;
i += strlen(key);
// 可選:跳過空白
while(in[i] == ' ') i++;
continue;
}
}
else
{
// 2. 遇到 CR 或 LF 結束
if(in[i] == '\r' || in[i] == '\n')
{
break;
}
// 3. 存入 output
if(o < (outMax - 1))
{
out[o++] = in[i];
}
}
i++;
}
out[o] = '\0';
return (o > 0);
}
void loop()
{
while (Serial.available())
{
qa(Serial.readString().c_str(), caBuffer);
Serial.println(caBuffer);
}
if(getBooQA("AT+CGPS?", "+CGPS: 1,1")==true)
{
delay(10);
qa("AT+CGPSINFO", caBuffer);
if(extractGPS(caBuffer, caGps, sizeof(caGps)))
{
memcpy(caData, caGps, iEncodedDataMax);
if(strlen(caGps)>14)
{
Serial.println("sendData");
Serial.println(caData);
sendData(caData);
delay(1000);
}
}
}
delay(1000);
/*
while (Serial.available()) { SerialAT.write(Serial.read()); }
while (SerialAT.available()) { Serial.write(SerialAT.read()); }
*/
}