Hello everyone,
I intend to use Arduino Uno to read serial output from a Traditional analog telephone system (call center).
So when a call comes in, Arduino reads the caller's number from the serial port of the telephone system and sends it to a php page on my server.
The sketch i wrote, thanks to this forum, works fine on my computer.
What i am kindly asking you guys is to check this sketch for errors, as once i install it, it will run for months unattened.
Testing environment:
Arduino Uno(connected to USB port)
Ethernet shield (connected to the local lan through the router)
Serial shield (connected to the computer using a USB2Serial cable)
Ubuntu 13.04
here is the sketch
// June 14 2013
// Read data from serial and send over to a web server
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x3E, 0x48 }; //physical mac address
byte ip[] = { 192,168,1, 170 }; // ip assigned to arduino
byte server[] = { 192, 168, 1, 7 }; // web server IP address
EthernetClient client;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
char inChar;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
Serial.println("serial ON");
}
void loop()
{
while (Serial.available())
{
delay(1);
// get the new byte:
inChar = Serial.read();
inputString += inChar; // add it to the inputString:
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n')
{
sendGET(inputString); // call sendGET function below
stringComplete = true;
inChar= 0; // set inChar empty
inputString=""; // set inputString empty
}
}
}
void sendGET(String pp) // function to send GET request data.
{
// if you get a connection, report back via serial:
if (client.connect(server, 80))
{
Serial.println("connected");
// Make a HTTP request and send the string
client.print("GET /exa2.php?data=");
client.print(pp);
client.print(" HTTP/1.1");
client.println("Host: 192.168.1.7");
client.println("Connection: close");
client.println();
}
else
{
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) //connected or data available
{
char c = client.read(); // gets byte from ethernet buffer
Serial.print(c); // prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println();
client.flush();
client.stop(); //stop client
}
the php script on the server:
<html>
<head>
<title>Test receive and write </title>
</head>
<body>
<?php
echo "parameter: " . $_GET["data"] ; // show parameter
echo "
";
$file = "exa2.txt";
$value = $_GET["data"] ; // get value
$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
$numBytes = fwrite($fp, $value) or die("Couldn't write values to file! ".$value);
fclose($fp);
echo "Wrote $numBytes bytes to $file successfully!";
echo "
"."tNumber=".$value;
?>
</body>
</html>
On my computer's terminal i use this command to initialize comm port
stty -F /dev/ttyUSB0 9600 -parity cs8 -cstopb
And this command to send to the comm port a plain text file containing a number: 1234567890
cat arduino-test.txt >/dev/ttyUSB0
thank you for your time and help