forked from JayaSurya-27/GSM_Module
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendingDataToThingSpeak2.ino
More file actions
119 lines (99 loc) · 3.47 KB
/
SendingDataToThingSpeak2.ino
File metadata and controls
119 lines (99 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <TinyGPS++.h>
//vcomment
#define SIM_SERIAL Serial1 // SIM Module (Pins 18 TX, 19 RX)
#define GPS_SERIAL Serial2 // GPS Module (Pins 16 TX, 17 RX)
int indi = 11;
TinyGPSPlus gps;
unsigned long previousMillis = 0;
const long interval = 60000; // Send data every 60 seconds
const char* apiKey = "AYDHNWSD93VRXC1S"; // ThingSpeak API Key
void setup() {
Serial.begin(115200); // Serial Monitor (PC)
SIM_SERIAL.begin(115200); // SIM Module (GSM)
GPS_SERIAL.begin(9600); // GPS Module
pinMode(indi, OUTPUT);
delay(3000);
Serial.println("Initializing GSM Module...");
// Check SIM module response
if (sendATcommand("AT", "OK", 5000)) {
Serial.println("SIM Module Ready.");
} else {
Serial.println("ERROR: SIM Module not responding!");
}
// Check network registration
if (sendATcommand("AT+CREG?", "+CREG: 0,1", 5000) || sendATcommand("AT+CREG?", "+CREG: 0,5", 5000)) {
Serial.println("Network Registered!");
} else {
Serial.println("ERROR: Not registered on network!");
}
}
void loop() {
// Read data from GPS module
while (GPS_SERIAL.available()) {
gps.encode(GPS_SERIAL.read());
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
sendGpsToThingSpeak();
}
}
void sendGpsToThingSpeak() {
if (!gps.location.isValid()) {
Serial.println("ERROR: GPS signal not available!");
return;
}
float latitude = gps.location.lat();
float longitude = gps.location.lng();
float speed = gps.speed.kmph(); // Fetch speed in km/h
Serial.print("Latitude: ");
Serial.println(latitude, 6);
Serial.print("Longitude: ");
Serial.println(longitude, 6);
Serial.print("Speed (km/h): ");
Serial.println(speed, 2);
// Construct URL with speed data
String url = "http://api.thingspeak.com/update?api_key=";
url += apiKey;
url += "&field1=" + String(latitude, 6);
url += "&field2=" + String(longitude, 6);
url += "&field3=" + String(speed, 2); // Sending speed data
// Send HTTP request via GSM
if (sendATcommand("AT+HTTPINIT", "OK", 2000) &&
sendATcommand("AT+HTTPPARA=\"URL\",\"" + url + "\"", "OK", 1000) &&
sendATcommand("AT+HTTPACTION=0", "200", 20000)) {
Serial.println("Data Sent to ThingSpeak!");
digitalWrite(indi, HIGH);
delay(1000);
digitalWrite(indi, LOW);
delay(1000);
sendATcommand("AT+HTTPREAD", "OK", 10000);
} else {
digitalWrite(indi, HIGH);
delay(5000);
digitalWrite(indi, LOW);
delay(1000);
Serial.println("ERROR: Failed to send data!");
}
sendATcommand("AT+HTTPTERM", "OK", 1000);
}
bool sendATcommand(String command, String expected_response, unsigned int timeout) {
String response = "";
// Serial.println(command);
SIM_SERIAL.println(command);
unsigned long start_time = millis();
while (millis() - start_time < timeout) {
if (SIM_SERIAL.available()) {
char c = SIM_SERIAL.read();
response += c;
if (response.indexOf(expected_response) != -1) {
Serial.println("success");
Serial.println(response);
Serial.flush();
return true;
}
}
}
Serial.println("SIM Response: " + response); // Print GSM module response for debugging
return false;
}