Created
July 13, 2023 05:54
-
-
Save maxistar/7d5362b230a745440a77bdb92a9d7f0f to your computer and use it in GitHub Desktop.
Sketch for getting location from GSM towers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************** | |
* | |
* This sketch connects to a website and downloads a page. | |
* It can be used to perform HTTP/RESTful API calls. | |
* | |
* TinyGSM Getting Started guide: | |
* https://tiny.cc/tinygsm-readme | |
* | |
**************************************************************/ | |
// Your GPRS credentials (leave empty, if missing) | |
const char apn[] = "chili"; // Your APN | |
const char gprsUser[] = ""; // User | |
const char gprsPass[] = ""; // Password | |
const char simPIN[] = ""; // SIM card PIN code, if any | |
// TTGO T-Call pin definitions | |
#define MODEM_RST 5 | |
#define MODEM_PWKEY 4 | |
#define MODEM_POWER_ON 23 | |
#define MODEM_TX 27 | |
#define MODEM_RX 26 | |
#define I2C_SDA 21 | |
#define I2C_SCL 22 | |
// Set serial for debug console (to the Serial Monitor, default speed 115200) | |
#define SerialMon Serial | |
// Set serial for AT commands (to the module) | |
#define SerialAT Serial1 | |
// Configure TinyGSM library | |
#define TINY_GSM_MODEM_SIM800 // Modem is SIM800 | |
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb | |
// Define the serial console for debug prints, if needed | |
//#define TINY_GSM_DEBUG SerialMon | |
//#define DUMP_AT_COMMANDS | |
#include <Wire.h> | |
#include <TinyGsmClient.h> | |
#include "utilities.h" | |
#ifdef DUMP_AT_COMMANDS | |
#include <StreamDebugger.h> | |
StreamDebugger debugger(SerialAT, SerialMon); | |
TinyGsm modem(debugger); | |
#else | |
TinyGsm modem(SerialAT); | |
#endif | |
// Server details | |
const char server[] = "enx5hjar0vnp.x.pipedream.net"; | |
const char resource[] = "/"; | |
TinyGsmClient client(modem); | |
const int port = 80; | |
String responce = ""; | |
String SIM800_send(String incoming) { | |
SerialAT.println(incoming); | |
delay(1000); //Print what is being sent to GSM module | |
String result = ""; | |
while (SerialAT.available()) { //Wait for result | |
char letter = SerialAT.read(); | |
result = result + String(letter); //combine char to string to get result | |
} | |
return result; //return the result | |
} | |
void setup() { | |
// Set console baud rate | |
SerialMon.begin(115200); | |
delay(10); | |
// Keep power when running from battery | |
Wire.begin(I2C_SDA, I2C_SCL); | |
bool isOk = setPowerBoostKeepOn(1); | |
SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL")); | |
// Set-up modem reset, enable, power pins | |
pinMode(MODEM_PWKEY, OUTPUT); | |
pinMode(MODEM_RST, OUTPUT); | |
pinMode(MODEM_POWER_ON, OUTPUT); | |
digitalWrite(MODEM_PWKEY, LOW); | |
digitalWrite(MODEM_RST, HIGH); | |
digitalWrite(MODEM_POWER_ON, HIGH); | |
// Set GSM module baud rate and UART pins | |
SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); | |
delay(5000); | |
// Restart takes quite some time | |
// To skip it, call init() instead of restart() | |
SerialMon.println("Initializing modem..."); | |
modem.restart(); | |
// Or, use modem.init() if you don't need the complete restart | |
String modemInfo = modem.getModemInfo(); | |
SerialMon.print("Modem: "); | |
SerialMon.println(modemInfo); | |
// Unlock your SIM card with a PIN if needed | |
if (strlen(simPIN) && modem.getSimStatus() != 3 ) { | |
modem.simUnlock(simPIN); | |
} | |
responce = SIM800_send("ATE1"); //Enable Echo if not enabled by default | |
SerialMon.print ("Responce:"); | |
SerialMon.println(responce); | |
delay(5000); | |
responce = SIM800_send("AT+CGATT=1"); //Set the SIM800 in GPRS mode | |
SerialMon.print ("Responce:"); | |
SerialMon.println(responce); | |
delay(5000); | |
responce = SIM800_send("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\" "); //Activate Bearer profile | |
SerialMon.print ("Responce:"); | |
SerialMon.println(responce); | |
delay(5000); | |
responce = SIM800_send("AT+SAPBR=3,1,\"APN\",\"chili\" "); //Set VPN options => 'RCMNET' 'www' | |
SerialMon.print ("Responce:"); | |
SerialMon.println(responce); | |
delay(5000); | |
responce = SIM800_send("AT+SAPBR=1,1"); //Open bearer Profile | |
SerialMon.print ("Responce:"); | |
SerialMon.println(responce); //Open bearer Profile | |
delay(5000); | |
responce = SIM800_send("AT+SAPBR=2,1"); //Get the IP address of the bearer profile | |
SerialMon.print ("Responce:"); | |
SerialMon.println(responce); | |
delay(5000); | |
} | |
void loop() { | |
responce = ""; | |
SerialMon.print("Waiting for network..."); | |
if (!modem.waitForNetwork(240000L)) { | |
SerialMon.println(" fail"); | |
delay(10000); | |
return; | |
} | |
SerialMon.println(" OK"); | |
if (modem.isNetworkConnected()) { | |
SerialMon.println("Network connected"); | |
} | |
SerialMon.print(F("Connecting to APN: ")); | |
SerialMon.print(apn); | |
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) { | |
SerialMon.println(" fail"); | |
delay(10000); | |
return; | |
} | |
SerialMon.println(" OK"); | |
SerialAT.println("AT+CIPGSMLOC=1,1"); | |
delay(5000); //Request for location data | |
while (SerialAT.available()) { | |
char letter = SerialAT.read(); | |
responce = responce + String(letter); //Store the location information in string responce | |
} | |
SerialMon.print("Result Obtained as:"); | |
SerialMon.print(responce); | |
SerialMon.println("*******"); | |
SerialMon.print("Connecting to "); | |
SerialMon.print(server); | |
if (!client.connect(server, port)) { | |
SerialMon.println(" fail"); | |
delay(10000); | |
return; | |
} | |
SerialMon.println(" OK"); | |
// Make a HTTP GET request: | |
SerialMon.println("Performing HTTP GET request..."); | |
client.print(String("GET ") + resource + " HTTP/1.1\r\n"); | |
client.print(String("Host: ") + server + "\r\n"); | |
client.print("Connection: close\r\n\r\n"); | |
client.println(); | |
unsigned long timeout = millis(); | |
while (client.connected() && millis() - timeout < 10000L) { | |
// Print available data | |
while (client.available()) { | |
char c = client.read(); | |
SerialMon.print(c); | |
timeout = millis(); | |
} | |
} | |
SerialMon.println(); | |
SerialAT.println("AT+CIPGSMLOC=1,1"); | |
delay(5000); //Request for location data | |
while (SerialAT.available()) { | |
char letter = SerialAT.read(); | |
responce = responce + String(letter); //Store the location information in string responce | |
} | |
SerialMon.print("Result Obtained as:"); | |
SerialMon.print(responce); | |
SerialMon.println("*******"); | |
// Shutdown | |
// client.stop(); | |
SerialMon.println(F("Server disconnected")); | |
// modem.gprsDisconnect(); | |
SerialMon.println(F("GPRS disconnected")); | |
// Do nothing forevermore | |
//while (true) { | |
delay(10000); | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment