0% found this document useful (0 votes)
151 views

GSM PHP MySQL Datatransfer - Ino

This Arduino sketch logs sensor data over the GSM network by taking temperature readings from a thermistor, connecting to a server using GPRS, and sending the temperature values to a PHP script. It takes an average of 60 samples over 10 minutes, calculates the temperature, establishes a GSM connection if needed, then makes an HTTP GET request to pass the data and a security key to the server script. It will reconnect every 10 minutes to send new readings.

Uploaded by

ZayNab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

GSM PHP MySQL Datatransfer - Ino

This Arduino sketch logs sensor data over the GSM network by taking temperature readings from a thermistor, connecting to a server using GPRS, and sending the temperature values to a PHP script. It takes an average of 60 samples over 10 minutes, calculates the temperature, establishes a GSM connection if needed, then makes an HTTP GET request to pass the data and a security key to the server script. It will reconnect every 10 minutes to send new readings.

Uploaded by

ZayNab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

Arduino Sketch for logging Data over the GSM Network.


Code inspired and snippets taken from:
Tom Igoe:
http://arduino.cc/en/Tutorial/GSMExamplesWebClien
User Poldi:
http://jleopold.de/2011/03/16/arduino-daten-logger-mysql-php
Anders Hedberg:
http://forum.arduino.cc/index.php?topic=163449.0
*/
// libraries
#include <GSM.h>
// PIN Number
#define PINNUMBER "" // SIM card PIN Number
// APN data
#define GPRS_APN
"" // replace your GPRS APN
#define GPRS_LOGIN
""
// replace with your GPRS login
#define GPRS_PASSWORD "" // replace with your GPRS password
// Pin that the thermistor is connected to
#define PINOTERMISTOR A0
// Nominal temperature value for the thermistor
#define TERMISTORNOMINAL 10000
// Nominl temperature depicted on the datasheet
#define TEMPERATURENOMINAL 25
// Number of samples
#define NUMAMOSTRAS 60
// Beta value for our thermistor
#define BCOEFFICIENT 3977
// Value of the series resistor
#define SERIESRESISTOR 10000
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// URL, path & port (for example: arduino.cc)
char server[] = "";
char path[] = "/datatransfer/pass.php";
char key[] = "";
// Security key from pass.php
int port = 80; // port 80 is the default for HTTP
long delay_intervall = 600000; //Intervall in ms (10min)
int amostra[NUMAMOSTRAS];
int i;
float temperature;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
analogReference(EXTERNAL);
/*
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}*/

Serial.println("Starting Arduino remote data logger.");


}
void loop() {
Serial.print("--Read Temperature "); //Read temperature
Serial.print("(Average of ");
Serial.print(NUMAMOSTRAS);
Serial.println(" samples over 60 seconds)--");
Read_sensor();
Serial.println("--Check Connection--"); //Check if connection to server possib
le
if (!client.connect(server, port)) {
Serial.println("No connection to GSM Network. Connecting...");
establish_connection();
}
//Connect to server and pass data and key to PHP script
Serial.println("Connect to server and send data");
Daten_senden();
//Read answer from server (NOT WORKING YET)
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// If server has disconnected stop client
if (!client.available() && !client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
client.flush();
}
delay(delay_intervall); // Wait for time stated in setup
}
void establish_connection() {
GSM gsmAccess(true); // True for debugging
Serial.println(F("GSM start"));
unsigned long myTimeout = 120000; // YOUR LIMIT IN MILLISECONDS
theGSM3ShieldV1ModemCore.println("AT");
gsmAccess.begin(PINNUMBER,true,false); //Use async mode and requires that GSM
debug mode has been set on GSM object creation
boolean notConnected = true;
unsigned long timeConnect = millis();
// connection state
while(notConnected && (millis()-timeConnect < myTimeout)) {
int ok = 0;
gsmAccess.ready(); //Call this if debugging is on. Otherwise we will never r
each GSM_READY...?!?
delay(1000); //might not call ready too often.??? See GSM3ShieldV1AccessProv
ider.cpp, GSM3ShieldV1AccessProvider::begin
ok = gsmAccess.getStatus();
if (ok != GSM_READY && ok != GPRS_READY) {
Serial.print(F("GSM status: "));
Serial.println(ok);
continue;
}
if(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY) {
notConnected = false;

}
}
}
void Read_sensor( ) {
float media;
for (i=0; i< NUMAMOSTRAS; i++) {
amostra[i] = analogRead(PINOTERMISTOR);
delay(10);
}
media = 0;
for (i=0; i< NUMAMOSTRAS; i++) {
media += amostra[i];
delay(1000);
}
media /= NUMAMOSTRAS;
// Convert the thermal stress value to resistance
media = 1023 / media - 1;
media = SERIESRESISTOR / media;
//Calculate temperature using the Beta Factor equation
temperature = media / TERMISTORNOMINAL;
// (R/Ro)
temperature = log(temperature); // ln(R/Ro)
temperature /= BCOEFFICIENT;
// 1/B * ln(R/Ro)
temperature += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
temperature = 1.0 / temperature;
// Invert the value
temperature -= 273.15;
// Convert it to Celsius
Serial.print("The temperature is: ");
Serial.println(temperature);
}
void Daten_senden() {
//Connect to server and pass the data to php script
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
// Make a HTTP request:
client.print("GET ");
client.print(path);
client.print("?data=");
client.print(temperature);
client.print("&check=");
client.print(key);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println();
Serial.println("Done!");
}
else {
Serial.println("No Connetion");
}
}

You might also like