I need help with code that can receive TCP data into a PHP file
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi friends

I'm receiving data from an arduino hardware device and the manufacturer says he has set it up to post to my server via a URL.

How can I read the data using PHP?

He says on the devices' logs the data is like this
$$0121AAKCQ 007A|128008001401010101013812000000000000000000000000100C25A2000000000000.000000000000.0000N00000.0000E00116D

How can I get this data via PHP? Is there PHP code that reads TCP data? Is it read via POST or via GET or via REQUEST or via sockets?

Please help a newbie

Thanks

Have you seen this thread? https://arduino.stackexchange.com/questions/28481/post-data-from-arduino-to-website Looks to me like a good start.
kostasx 4 months ago
Hi jobokoth, have you worked with sockets before ?
SilverHood Apps 4 months ago
No, I haven't worked with sockets before
jobokoth 4 months ago
Well.. I think you'll be needing that.. may I know to what IP address and port does your Arduino points to?
SilverHood Apps 4 months ago
Its posting to a domain name port 80
jobokoth 4 months ago
Ok i have a code in handly.. I'll modify and post it according to your need.. Since I do not have the environment setup for testing I'll need your help in testing, together this will get complete if thats ok with you? I'll be with you the whole time
SilverHood Apps 4 months ago
Hi, any progress??
SilverHood Apps 4 months ago
hi, just to understand your requirements, are you trying to get the arduino host to do a HTTP POST to your PHP script? what are you trying to do with the data once it is posted, are there any details on the format of the data "$$01......00116D"?
kush 4 months ago
Yes Kush, you are right, once the data is posted, I'll insert it into my database as normal
jobokoth 4 months ago

Crowdsource coding tasks.

1 Solution


Hi, the main part here is to read

$buf = socket_read($msgsock, 2048, PHP_NORMAL_READ)
echo $buf

This is put into an infinite loop so that it keeps listening to the socket inside

do{
}while(true)

Complete code, do test and let me know..

<?php

/* Allow the script to hang around waiting for connections. */

set_time_limit(0);

/* Turn on implicit output flushing so we see what we’re getting

* as it comes in. */

ob_implicit_flush();

$hostname = "www.example.com";

$address = gethostbyname($hostname); //eg. ‘100.220.110.5’;

$port = 80;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {

    echo “socket_create() failed: reason:  . socket_strerror(socket_last_error()) . “\n”;

}

if (socket_bind($sock, $address, $port) === false) {

    echo “socket_bind() failed: reason:  . socket_strerror(socket_last_error($sock)) . “\n”;

}

if (socket_listen($sock, 5) === false) {

    echo “socket_listen() failed: reason:  . socket_strerror(socket_last_error($sock)) . “\n”;

}

do {

    if (($msgsock = socket_accept($sock)) === false) {

        echo “socket_accept() failed: reason:  . socket_strerror(socket_last_error($sock)) . “\n”;

        break;

    }

    do {

        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {

            echo “socket_read() failed: reason:  . socket_strerror(socket_last_error($msgsock)) . “\n”;

            break 2;

        }
        //trim the message
        if (!$buf = trim($buf)) {
            continue;
        }

        //if device said quite then exit
        if ($buf == 'quit') {
            break;
        }

        //if server needs to be shut down
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }

        //Use only for taking back to your device

        //$talkback = “PHP: You said ‘$buf’.\n”;

        //socket_write($msgsock, $talkback, strlen($talkback));

        echo $buf\n”;

    } while (true);

    socket_close($msgsock);

} while (true);

socket_close($sock);

?>
Hi Silverhood, I'll be testing later today and give you feedback. Thanks
jobokoth 4 months ago
Alrighty 👍
SilverHood Apps 4 months ago
Hi I have implemented the code but its not picking the data.... Or can we send to your server? Job
jobokoth 4 months ago
Hi job, did you receive my private message?
SilverHood Apps 4 months ago