Created
May 13, 2020 00:08
-
-
Save erdemarslan/16a6f3ea3a93e4f755f5a85d043dc31a to your computer and use it in GitHub Desktop.
HTTP Post Analog Value With GSMSim Library. First add GSMSim Library to Arduino with using Library Manager.
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
/* | |
* GSMSimHTTP Post analog value Example | |
* | |
* GSMSim_HTTP.ino | |
* | |
* By Erdem ARSLAN | |
* Version: v.2.0.1 | |
* | |
* The MIT License (MIT) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
/* | |
### Example Serial Output ### | |
Set Phone Function... 1 | |
is Module Registered to Network?... 1 | |
Signal Quality... 14 | |
Operator Name... Turk Telekom | |
Connect GPRS... 1 | |
Get IP Address... xxx.xxx.xxx.xxx | |
Get... METHOD:GET|HTTPCODE:200|LENGTH:30 | |
Get with SSL and read returned data... METHOD:GET|HTTPCODE:200|LENGTH:30|DATA:This test data return from GET | |
Post... METHOD:POST|HTTPCODE:200|LENGTH:54 | |
Post with SSL and read returned data... METHOD:POST|HTTPCODE:200|LENGTH:54|DATA:This test data return from POST.<br>Hello Erdem Arslan | |
Close GPRS... 1 | |
*/ | |
#include <GSMSimHTTP.h> | |
#include <SoftwareSerial.h> | |
// You can use any Serial interface. I recommended HardwareSerial. Please use the library with highiest baudrate. | |
// In examples, i used HardwareSerial. You can change it anymore. | |
#define RESET_PIN 10 // you can use any pin. | |
#define analogPin = A0; | |
SoftwareSerial gsmSerial(7,8); | |
GSMSimHTTP http(gsmSerial, RESET_PIN); // GSMSimHTTP inherit from GSMSimGPRS. You can use GSMSim and GSMSimGPRS methods with it. | |
void setup() { | |
gsmSerial.begin(57600); // If you dont change module baudrate, it comes with auto baudrate. | |
while(!gsmSerial) { | |
; // wait for module for connect. | |
} | |
Serial.begin(115200); // Serial for debug... | |
// Init module... | |
http.init(); // use for init module. Use it if you dont have any valid reason. | |
Serial.print("Set Phone Function... "); | |
Serial.println(http.setPhoneFunc(1)); | |
//delay(1000); | |
Serial.print("is Module Registered to Network?... "); | |
Serial.println(http.isRegistered()); | |
//delay(1000); | |
Serial.print("Signal Quality... "); | |
Serial.println(http.signalQuality()); | |
//delay(1000); | |
Serial.print("Operator Name... "); | |
Serial.println(http.operatorNameFromSim()); | |
//delay(1000); | |
//Serial.print("GPRS Init... "); | |
//Serial.println(http.gprsInit("internet")); // Its optional. You can set apn, user and password with this method. Default APN: "internet" Default USER: "" Default PWD: "" | |
//delay(1000); | |
} | |
bool postData(int analogValue) { | |
bool isPostOK = true; | |
Serial.print("Connect GPRS... "); | |
isPostOK = http.connect(); | |
if (isPostOK) { | |
Serial.print("Post... "); | |
String postAnswer = http.post("localhost/index.php", "params=" + String(analogValue), "application/x-www-form-urlencoded"); | |
if(postAnswer.indexOf("HTTPCODE:200") != -1) { | |
isPost = true; | |
Serial.print("Close GPRS... "); | |
Serial.println(http.closeConn()); | |
} else { | |
isPost = false; | |
} | |
} | |
return isPostOK; | |
} | |
void loop() { | |
int analogValue = analogRead(analogPin); | |
if(postData(analogValue)) { | |
Serial.print("Analog value posted to Server"); | |
} else { | |
Serial.print("Analog value NOT posted to Server."); | |
} | |
delay(60000); // wait 1 minute | |
} |
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
<?php | |
$timestamp = date("d-m-Y - H:i:s"); | |
$datastring = $_POST["params"]; | |
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); | |
$txt = $timestamp . " - ".$datastring."\n"; | |
fwrite($myfile, $txt); | |
fclose($myfile); | |
echo $txt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment