Pages: [1]   Go Down
Author Topic: Arduino Uno to read serial input and send over to internet  (Read 1161 times)
0 Members and 1 Guest are viewing this topic.
Rhodes - Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
// 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:
Code:
<html>
<head>
<title>Test receive and write </title>
</head>
<body>

<?php

echo "parameter: " $_GET["data"] ; // show parameter
echo "<br/>";

$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 "<br/>"."tNumber=".$value;

?>


</body>
</html>

On my computer's terminal i use this command to initialize comm port
Code:
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
Code:
cat arduino-test.txt >/dev/ttyUSB0

thank you for your time and help
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
No. Once you install it, the testing begins. Once the testing is satisfactory, then the device can run unattended for months.

Code:
  while (Serial.available())
  {
    delay(1);
    // get the new byte:
    inChar = Serial.read();
Once you know that there is data to read, there is no reason to stand around twiddling your thumbs. Read it!

Code:
    client.print("GET /exa2.php?data=");
    client.print(pp);
    client.print(" HTTP/1.1");
    client.println("Host: 192.168.1.7");
The Host line is NOT part of the same record as the GET statement. The 3rd line should be a println() statement.

Code:
    Serial.print(c);        // prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
What did you say the serial port was connected to? What, exactly, is the telephone system going to be doing with this data?

Logged

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
No. Once you install it, the testing begins. Once the testing is satisfactory, then the device can run unattended for months.


He's not asking you to test it. He's asking you to check it. For errors. You know. Another set of eyes.
Logged

Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Melbourne, Australia
Offline Offline
God Member
*****
Karma: 8
Posts: 567
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you thought of doing a retry if the server is unavailable the first time you try to connect?
How important are the numbers? Have you considered buffering them somewhere if you cannot connect?
If you can connect to the server but there is an error with the script (eg: db is down), how are you going to handle that?
Logged

Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Rhodes - Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you PaulS

I changed sketch according to your 2 suggestions. It works fine !

Once Arduino will be installed, will be connected to the serial output of the telephone system receiving the caller's telephone number. This number will be sent to a server on the internet and saved to a mysql table together with additional data (operator's input) about the specific caller. It's like a customers database.
Logged

Rhodes - Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you thought of doing a retry if the server is unavailable the first time you try to connect?
How important are the numbers? Have you considered buffering them somewhere if you cannot connect?
If you can connect to the server but there is an error with the script (eg: db is down), how are you going to handle that?

Good point aarondc.   I should insert a retry as you suggested. Thank you !

As for the numbers, (actually is the caller's telephone number) are important at the time of the call. I still have to think what to do in case there are multiple operators receiving multiple calls. As PaulS said i will have to install it and do more tests.
This will be tough as i am new to Arduino.   smiley-confuse
I am thinking to send every single caller number to the server, store to a temp table and let php and mysql handle them.
Logged

Pages: [1]   Go Up
Jump to: