Hello again,
I have a perl server running waiting for a connection from the Arduino to receive lat and long coordinates to be inserted into a mysql database. The Arduino is connecting with the server ok but I can't work out how to actually send to the server any data never mind the data I want. I know the lat and long data is there as I previously had it outputting to a web page. I did modify this code to work with my perl server, so I may have started off on the wrong foot all together?
Really struggling so any help as always is much appreciated:
server.pl (perl server)
sub listener
{
my $dbh = shift || return;
# auto-flush on socket
$| = 1;
# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '0.0.0.0',
LocalPort => '7777',
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on port 7777\n";
print "\n";
while(1)
{
my $client_socket = $socket->accept(); # waiting for a new client connection
# get information about a newly connected client
my $client_address = $client_socket->peerhost();
my $client_port = $client_socket->peerport();
print "connection from $client_address:$client_port\n";
# read up to 1024 characters from the connected client
my $data = "";
$client_socket->recv($data, 1024);
print "Received: $data";
@array = unpack ("sff","$data");
my $deviceid = $array[0];
my $latitude = $array[1];
my $longitude = $array[2];
print "DeviceID: $deviceid \n";
print "Latitude: $latitude \n";
print "Longitude: $longitude \n";
print "\n";
# write response data to the connected client
$data = "message received from server";
$client_socket->send($data);
insertGPSData($dbh, $deviceid, $latitude, $longitude); #send data to INSERT function
shutdown($client_socket, 1); # notify client that response has been sent
}
$socket->close();
}
Arduino Loop Code:
void loop()
{
// GPS LOOP START
while(uart_gps.available()) // While there is data on the RX pin...
{
int c = uart_gps.read(); // load the data into a variable...
if(gps.encode(c)) // if there is a new valid sentence...
{
getgps(gps); // then grab the data.
}
}
// Define the variables that will be used
float latitude, longitude;
// Then call this function
gps.f_get_position(&latitude, &longitude);
//GPS LOOP FINISH
//ETHERNET LOOP START
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
client.write("123");
/*
client.print(latitude,5);
client.print(longitude,5);
*/
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}