Last active
November 10, 2018 06:36
-
-
Save arn-ob/59a990afdb1ff898055ed078a306249c to your computer and use it in GitHub Desktop.
Sending GPS data to server using ESP8266 nodemcu
This file contains hidden or 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
| <?php | |
| date_default_timezone_set("Asia/Dhaka"); | |
| header_remove('Access-Control-Allow-Origin'); | |
| header('Access-Control-Allow-Origin: *'); | |
| $servername = ""; | |
| $username = "";; | |
| $password = ""; | |
| $dbname = ""; | |
| $conn = new mysqli($servername, $username, $password, $dbname); | |
| $data = file_get_contents('php://input'); // put the contents of the file into a variable | |
| $receive = json_decode($data); // decode the JSON feed | |
| // Data Store for Send | |
| $return = array(); | |
| // Connection Check | |
| if ($conn->connect_error) { | |
| die("Connection failed: " . $conn->connect_error); | |
| } | |
| // Check POST METHOD | |
| if ($_SERVER["REQUEST_METHOD"] == 'POST') | |
| { | |
| $id = base64_encode(rand(10,100)); | |
| // Data Receive | |
| $a = $receive->lat; | |
| $b = $receive->lng; | |
| $c = $receive->coin; | |
| // mysql Store qry | |
| $sql_sp = "INSERT INTO gsm(lat, lng, msg, created, time) VALUES ('". $a ."','". $b ."','". $c ."', CURRENT_DATE(), CURRENT_TIME())"; | |
| if ($conn->query($sql_sp) === TRUE) | |
| { | |
| // send response | |
| echo 200; | |
| } | |
| // JSON Encoding to send | |
| }else{ | |
| echo "Problem"; | |
| } | |
| $conn->close(); | |
| ?> |
This file contains hidden or 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
| #include <ESP8266HTTPClient.h> | |
| #include <ESP8266WiFi.h> | |
| #include <TinyGPS.h> | |
| #include <SoftwareSerial.h> | |
| TinyGPS gps; | |
| SoftwareSerial ss(0, 2); | |
| int big = 14; | |
| int small = 12; | |
| float flat, flon; | |
| void setup() { | |
| Serial.begin(115200); //Serial connection | |
| ss.begin(9600); | |
| pinMode(big, INPUT); | |
| pinMode(small, INPUT); | |
| WiFi.begin("EEE", "weakpassword"); //WiFi connection | |
| while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion | |
| delay(500); | |
| Serial.println("Waiting for connection"); | |
| } | |
| Serial.println("Wifi Connected"); | |
| } | |
| void loop(){ | |
| // server(flat, flon, "Big"); | |
| if (digitalRead(big)) | |
| { | |
| Serial.println("Big Coin Detect"); | |
| gpsfunc(); | |
| server(flat, flon, "Big Coin"); | |
| } | |
| if (digitalRead(small)) | |
| { | |
| Serial.println("Small Coin Detect"); | |
| gpsfunc(); | |
| server(flat, flon, "Small Coin"); | |
| } | |
| } | |
| void gpsfunc(){ | |
| Serial.println("GPS Detect"); | |
| unsigned long chars; | |
| unsigned short sentences, failed; | |
| // For one second we parse GPS data and report some key values | |
| for (int c = 0; c <= 30; c++) | |
| { | |
| while (ss.available()) | |
| { | |
| char c = ss.read(); | |
| // Serial.write(c); // uncomment this line if you want to see the GPS data flowing | |
| if (gps.encode(c)) // Did a new valid sentence come in? | |
| { | |
| Serial.println("New Data Found"); | |
| unsigned long age; | |
| gps.f_get_position(&flat, &flon, &age); | |
| Serial.print("LAT="); | |
| Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6); | |
| Serial.print(" LON="); | |
| Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6); | |
| Serial.print(" SAT="); | |
| Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites()); | |
| Serial.print(" PREC="); | |
| Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop()); | |
| } | |
| } | |
| } | |
| Serial.println("GPS Detect Finish"); | |
| } | |
| void server(float lat, float lng, String msg) { | |
| Serial.println("Sending Data To Server"); | |
| String s = "\""; | |
| String b = "{\"lat\":"; | |
| String bV = String(lat, 6); | |
| String c = ",\"lng\":"; | |
| String cV = String(lng, 6); | |
| String d = ",\"coin\":"; | |
| String dV = msg; | |
| String e = "\"}"; | |
| String url = b + s + bV + s + c + s + cV + s + d + s + dV + e; | |
| if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status | |
| HTTPClient http; //Declare object of class HTTPClient | |
| http.begin("http://stupidarnob.com/phpAPI/gsm.php"); //Specify request destination | |
| http.addHeader("Content-Type", "text/plain"); //Specify content-type header | |
| int httpCode = http.POST(url); //Send the request | |
| String payload = http.getString(); //Get the response payload | |
| Serial.println(httpCode); //Print HTTP return code | |
| Serial.println(payload); //Print request response payload | |
| http.end(); //Close connection | |
| }else{ | |
| Serial.println("Error in WiFi connection"); | |
| } | |
| delay(10000); //Send a request every 30 seconds | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment