Arduino Client not able to Insert Data to a Cloud Server Instance

Hello everyone,

I have a current project that uses the EnOcean TCM310 receiver gateway, receiving sensor data from different STM3xx wireless sensor modules.
I have successfully implemented reading data from the TCM310 through Arduino UART using the EnOcean Serial Protocol (ESP3).
I wanted to transfer these data to a webserver on the cloud (using Amazon Web Services EC2 instance). The webserver is a Bitnami LAMP server on Ubuntu. I have written code based on similar implementations here in the forums. The connection is established between the Arduino client and the cloud web server, but there is a problem with inserting data on the mySQL server. I know that data is not inserted by viewing the database in the phpmyadmin. I had almost a week trying to figure out what's the problem to no avail: tried changing the location of the PHP file (not in the cloud server but on an Xerver local server, specifying the database host with the IP address of the cloud server in the PHP file); tried changing the database host name to localhost:3306, localhost, 127.0.0.1, and the IP address of the cloud server, with the PHP file located on the same machine with the mySQL database; tried changing the way the directory of the PHP file is written in the Arduino code (specifying the file location directly after the DocumentRoot).

Just a note, I'm using PuTty to access SSH for the cloud server instance since my local machine is Windows. Also, whenever I access phpmyadmin, I use tunneling to access via 127.0.0.1:8888, since Bitnami cloud server instances can't be accessed directly on the browser.

Kindly help if there are problems with my code.... or do you think this is a problem with the server configuration? Or is there an issue with using the GET function using the cloud? Somewhat confused on my next steps right now... Thanks!

Arduino Code:

#include <Ethernet.h> //library for ethernet functions
#include <SPI.h> //library for SPI communication (Arduino to Ethernet Shield)

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0x41 };
byte ip[] = { 192, 168, 1, 3 };
byte gw[] = { 192, 168, 1, 1 };
//byte server[] = { 182, 18, 208, 3 };
//byte server[] = { 127, 0, 0, 1 };
byte server[] = { 54, 245, 100, 105 }; // Server IP
byte subnet[] = { 255, 255, 255, 0 };

int ledPin=13;
int packet[24]; //used as a rotational buffer
int j=0;
int data=0;

void setup()
	{
	Serial.begin(57600);
	pinMode(ledPin,OUTPUT);
	digitalWrite(ledPin,HIGH);
	delay(500);
	digitalWrite(ledPin,LOW);
	Serial.println("EnOcean TCM310 reader");
	//send_packet(0xAB,0x48,0,0,0,0,0,0,0,0);
	}

void loop()
	{	
        Serial.println("Program running...");
		starttime = millis();
        while ( (Serial.available()<25) && ((millis() - starttime) < MAX_MILLIS_TO_WAIT) )
            {}      
        if(Serial.available() < 25) //the data didn't come in - handle that problem here
            {
            Serial.println("ERROR - Didn't get 24 bytes of data!");
            }
        else 
            {
            for (int j=0; j<24; j++)
                {
                packet[j] = Serial.read(); // Get data. 
                if (packet[0] == 0x55)
                    {
                    switch (j)
                        {
                        case 0: Serial.print("SYNC:"); break;
                        case 1: Serial.print(", Length:"); break;
                        case 3: Serial.print(", OpLength:"); break;
                        case 4: Serial.print(", Type:"); break;
                        case 5: Serial.print(", CRC8H:"); break;
                        case 6: Serial.print(", RORG:"); break;
                        case 7: Serial.print(", Data:"); break;
                        case 10: Serial.print(", LRNbit:"); break;
                        case 11: Serial.print(", Sender ID:"); break;
                        case 15: Serial.print(", Status:"); break;
                        case 16: Serial.print(", SubTel#:"); break;
                        case 17: Serial.print(", Dest ID:"); break;
                        case 21: Serial.print(", dBm:"); break;
                        case 22: Serial.print(", Security:"); break;
                        case 23: Serial.print(", CRC8D:"); break;
                        }
                    Serial.print(packet[j],HEX); Serial.print(" ");
                    }
                else
                Serial.println("Eror: Not a valid sensor telegram");
                }
            }
        digitalWrite(ledPin,HIGH); //LED ON to indicate that a packet is read
		delay(500);
        digitalWrite(ledPin,LOW ); //LED OFF
        senddata();
	}
	
void senddata()
	{
	
		data = packet[9];
		EthernetClient client;
		Ethernet.begin(mac, ip, subnet);
		Serial.println();
		Serial.println("Connecting to Web Server...");
		delay(1000);						//Keeps the connection from freezing

		if (client.connect(server, 80)) 
			{
			Serial.println("Connected!");
			client.print("GET /avislinktest.php?t0=");
                        Serial.print("GET /avislinktest.php?t0=");
                        client.print(data);
                        Serial.print(data);
                        client.println(" HTTP/1.1");
                        Serial.println(" HTTP/1.1");
                        client.println("Host: 54.245.100.105");
                        Serial.println("Host: 54.245.100.105");
                        client.println("User-Agent: Arduino");
                        Serial.println("User-Agent: Arduino");
                        client.println("Accept: text/html");
                        Serial.println("Accept: text/html");
                        client.println();
			Serial.println();
                        //readPage();
			delay (9800);
			}

		else
			{
			Serial.println("Connection unsuccesfull");
			}
  			//}
 			//stop client
 		client.stop();
 		while(client.status() != 0)
			delay(5);

	}

PHP file:

<?php

$value = $_GET['t0'];
echo date("d.m.Y-H:i:s") . " Temperature 1 = " . "$value";

$link = mysql_connect("localhost:3306", "username", "password") or die("Error: Cannot connect to Database" . mysql_error());
mysql_select_db("avislink") or die("Error: Cannot connect to Data Table" . mysql_error());
$query = "INSERT INTO temp (dataID, sensorID, time, sensorvalue) VALUES (1, 'AL-01-0001-UT', NOW(), '$value' )";
$result = mysql_query($query) or die("Unsuccessful writing to Database " . mysql_error());

mysql_close($link);
?>

Can you access the server 54.245.100.105 from your browser? (I couldn't)

I have just started the server instance again.... you can try again....
I also can't access 54.245.100.105 in the browser. It always takes too long until connection is timed out..
Bitnami documentation (Manage Bitnami Cloud Hosting Servers from the AWS Console) says can only access cloud machine via SSH in Linux or Putty on Windows.....

From your code, I see you are trying to make an http request (GET).
Maybe the port number is not (the default) 80, so it needs to be specified.
The first step in debugging your code is to get a connection to the web server through your browser.
Is that your IP address? Are you running a web server on it?

florinc, i checked the port in which the server is connecting and it's in the default Port 80.
Yes I'm running an apache web server on 54.245.100.105.
This is the first time I will be using this server.
The IP is an elastic IP address I associated with the server in AWS.
Its private IP address is 10.244.23.188, and should be working just the same with the elastic IP right?

I am not clear exactly which parts are working and which ones are broken, but it seems to me that currently you are trying to get the whole solution to succeed. This 'big bang' approach is the hardest way to tackle problems like this. I suggest you break the problem down.

Take the Arduino out of the equation first, and use a plain old web browser to send your HTTP requests.

Remove all the database code from the PHP servlet and confirm that you can successfully send a request to it and get back a response.

Then add code to the servlet to extract your request parameters from the URL and echo them back to the client, and confirm that works.

Then add code to the servlet to open and close a connection to the database and confirm that works.

Then add code to execute an innocuous hard-coded query such as select count(*) from a table you expect to exist and be accessible to you, and confirm it succeeds.

Then add code to print out your SQL query string.

Then change that to execute your query and confirm it succeeds.

Then code your Arduino sketch to submit the same HTTP request and confirm it produces the same behaviour.

thanks PeterH for your inputs... I'll try to break it down...

I found the culprit (with the help of an experienced cloud server guy) :smiley:
There was no problem with the Arduino code nor the PHP file...
The reason was very obvious from the start (but I didn't noticed it :astonished:)
and even before my problem went from confusing to worstly confusing...
:0
I can't get through with the ever simple first step of accessing the web server through my web server
due to the Security Group in which my AWS server was initially set.
The Security Group in AWS is the one which allows and blocks ports and firewalls in the network...
The default Security Group I was into had some restrictions and rules on ports/firewalls, thus
the problems I had in actually writing data from Arduino to the server..
A new security group with all filters disabled, was made available into my Options.
I ran a new instance of my server and used the new security group, and tried the same code and php file..
Now my browser is able to access the IP address, and insert the data from my sensor
to the cloud server...

Lesson learned: always check security groups before deploying AWS server images.. 8)