Arduino send Counter data to webserver

Hi everybody,

Have a nice day :slight_smile:

I need your help. I have a small project with Arduino Uno.

My ideal: Arduino will count signal (like counter), display and send it to Web.

  1. Arduino counter and display LCD: ~> done :slight_smile:
  2. Send Counter data to Web: ~> NOT YET :sob:

Pls refer to attached file. It took me over 2 weeks to solve this problem but the result still ZEZO. May you to take a moment to help me to solve this case, please.

Thank you so much.
Junnh
P/S: I’m sorry, my English is bad.

Trial.ino (1.58 KB)

add.PNG

Welcome to the forum! Please include your code within code tags (</>) in the post as it makes it easier for people to read.

You are trying to connect to 127.0.0.1 (localhost) but this address refers to the arduino itself. Please use the address of your web server instead. If you are using a PC on your local network it will probably start with 192.168.

You also need to give a name as well as the data so your web server can deal with it properly. Instead of “temp1=String(data)” you should use “temp1=‘counter:’+String(data)”

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>                
#include <LiquidCrystal_I2C.h>   
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

const int pin = 11;          
int data = 0;                     
int buttonstate = 0;        
int prevbuttonstate = 0;    

byte mac[] = { 0x00, 0xAB, 0xBA, 0xBC, 0xDD, 0x02 };
char server[] = "127,0,0,1 "; // Localhost
EthernetClient client;
String temp1;
void setup ()  {

Serial.begin(9600); 
if (Ethernet.begin(mac) == 0){
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(1000);
 
  
lcd.begin (16,2);                
pinMode (pin, INPUT_PULLUP); 
lcd.clear();
lcd.setCursor (0, 0);
lcd.print ("Counter");
lcd.setCursor (0, 1);
lcd.print ("Testing ^_^");
}

//****************//

void loop ()  {
  
buttonstate = digitalRead (pin);        
if (buttonstate != prevbuttonstate){   
if (buttonstate == LOW) {
  data = data + 1;                                 
  lcd.clear ();
  lcd.setCursor (0, 0);
  lcd.print ("Counter testing");
  lcd.setCursor (0, 1);
  lcd.print ("Q'ty   : ");
  lcd.print (data);                               
  delay (500);
}}
prevbuttonstate = buttonstate;         

temp1 = String(data);
if (client.connect("127,0,0,1",80)) { // Localhost
Serial.println("Server connection OK");
client.println("POST/add.php HTTP/1.1");
client.println("Host:127,0,0,1");// Localhost
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(temp1.length());
client.println();
client.print(temp1);
}
if (client.connected()) {
client.stop();
}
delay(500); 
}

Hi rw950431,

Thank you for your information. I revised my code as your instruction. When I replace 127.0.0.1 (Localhost) to 192.168.1.100 (PC’s address which setup WAMP) then Arduino don’t work, it don’t display LCD and don’t sending also…

Pls help me to checking it.

Thank you so much.

Here are my code:

Arduino code:

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>                
#include <LiquidCrystal_I2C.h>   
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

const int pin = 11;          
int data = 0;                     
int buttonstate = 0;        
int prevbuttonstate = 0;    

byte mac[] = { 0x00, 0xAB, 0xBA, 0xBC, 0xDD, 0x02 };
char server[] = "192,168,1,100 "; // Localhost
EthernetClient client;
String temp1;
void setup ()  {

Serial.begin(9600); 
if (Ethernet.begin(mac) == 0){
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(1000);

 
lcd.begin (16,2);                
pinMode (pin, INPUT_PULLUP); 
lcd.clear();
lcd.setCursor (0, 0);
lcd.print ("Counter");
lcd.setCursor (0, 1);
lcd.print ("Testing ^_^");
}

//****************//

void loop ()  {
 
buttonstate = digitalRead (pin);        
if (buttonstate != prevbuttonstate){   
if (buttonstate == LOW) {
 data = data + 1;                                 
 lcd.clear ();
 lcd.setCursor (0, 0);
 lcd.print ("Counter testing");
 lcd.setCursor (0, 1);
 lcd.print ("Q'ty   : ");
 lcd.print (data);                               
 delay (500);
}}
prevbuttonstate = buttonstate;         

temp1 = 'Counter' + String(data);
if (client.connect("192,168,1,100",80)) { // Localhost
Serial.println("Server connection OK");
client.println("POST/add.php HTTP/1.1");
client.println("Host:192,168,1,100");// Localhost
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(temp1.length());
client.println();
client.print(temp1);
}
if (client.connected()) {
client.stop();
}
delay(500); 
}

Add.php

<?php
   	
include("connect.php");
   	
   	
$link=Connection();

	
$temp1=$_POST["temp1"];
	

	
$query = "INSERT INTO `tempLog` (`temperature`, `humidity`) 
VALUES ('".$temp1."')"; 
   	
   	
mysql_query($query,$link);
	
mysql_close($link);

   	
header("Location: index_2.php");

?>

connect.php

<?php


function Connection()
{
		
$server="localhost";
		
$user="root";
		
$password="";
		
$db="abcd";
	   
	   
$con = mysql_connect($server, $user, $password, $db);

		
if (!$con) {
	    	
die('MySQL ERROR: ' . mysql_error());
	
}
		
		
mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

		return $con;
	}

?>

index_2.php

<?php

	
include("connect.php"); 	
	
	
$link=Connection();

	
$result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link);

?>



<html>
   
<head>
      
<title>
Counter testing</title>
   
</head>
<body>
   
<h1>Counter testing</h1>

   
<table border="1" cellspacing="1" cellpadding="1">
		
<tr>
			
<td>&nbsp;Time&nbsp;</td>
			
<td>&nbsp;Counter 1&nbsp;</td>
			
<td>&nbsp;Counter 2&nbsp;</td>
</tr>

      
<?php 
		  
if($result!==FALSE){
		     
while($row = mysql_fetch_array($result)) {
		        
printf("<tr><td> &nbsp;%s </td>
<td> &nbsp;%s&nbsp; 
</td>
<td> &nbsp;%s&nbsp; </td></tr>", 
$row["timeStamp"], $row["temperature"], $row["humidity"]);
		     
}
		     
mysql_free_result($result);
		     
mysql_close();
		  }
      
?>

   
</table>

</body>

</html>

Spotted a couple more errors in your code:

temp1 = 'temp1:' + String(data); // needs : to seperate key from value, also needs to match key in php script
if (client.connect("192,168,1,100",80)) { // Localhost
Serial.println("Server connection OK");
client.println("POST /add.php HTTP/1.1"); // needs space between POST and url
client.println("Content-Type: application/x-www-form-urlencoded;"); // added semicolon
client.print("Content-Length: ");
client.println(temp1.length());
client.println();
client.print(temp1);
}

What do you mean by "Arduino don't work, it don't display LCD and don't sending also"- does anything get printed to the LCD or serial monitor? Any error messages? Any entries in the webserver logs?

There is an example of working POST code at https://forum.arduino.cc/index.php?topic=155218.0 which you might find useful.

HTTP/1.1 request without Host: header is an error.

Change to POST /add.php HTTP/1.0.

Hi rw950431, Chagrin,

Thank you for your help. I'll try it as your instruction.

Thanks and B.R. Junnh