HttpClient for PHP and MySQL on arduino using ENC28J60

Hi Guys,

I am developing a network based sensor system that writes some data onto MySQL database that my laptop is hosting on the local router. I have my ethernet ENC28J60 breakout board conected and running. I have also got the DHCP etc and all the rest working. On the database, I have a column that holds integer values. I already have programmed a script to change the values.

<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$sql = 'UPDATE `person information` SET Status=1 WHERE ref_no=2';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db('sudhtan');
$update = mysql_query( $sql, $conn);

if(! $update )
{
  die('Could not update data: ' . mysql_error());
}

mysql_close($conn);
?>

I want to run this script in order to change the database value. Can anyone please guide me.

PS: I am 15 years old but it does not mean I am a noob but please, I will surely appreciate a simple and effective answer.

Rochan

I want to run this script in order to change the database value.

So, you need to make a GET request. What part of that do you need help with?

Where is your code?

I am using the ethercard library. Here is the documentation

http://jeelabs.net/pub/docs/ethercard/

I am not sure which example program I am supposed to change in order for it to work.

Thanks for the quick reply

Hi!

You need use ethercard?

Can be UIPEthernet for ENC28J60?

I have an example here:

//#include <SPI.h> // Uncomment to use the W5100
//#include <Ethernet.h> // Uncomment to use the W5100
#include <UIPEthernet.h> // Comment to use the W5100
#include <Base64.h>

#include <DS1307.h> // Henning's library

byte mac[] = {0xDE, 0xAD, 0xBE, 0xAF, 0xCE, 0xDD}; // whatever mac you want
byte ip[] = {192, 168, 0, 177};

DS1307 rtc(20, 21);

char *Username  = "Username";
char *APIKEY = "12345678";

EthernetClient client;
char *token = ":";

char Auth[50];
char Auth1[50];

void setup()
{
  Serial.begin(9600);

  strcpy(Auth,Username);
  strcat(Auth,token);
  strcat(Auth,APIKEY);
  base64_encode(Auth1, Auth, strlen(Auth));

  Ethernet.begin(mac, ip);

  Serial.print("Ip Server: ");
  Serial.println(Ethernet.localIP());

}

void loop()
{
  delay(30000);
  enviar_dados();
}

void enviar_dados()
{
  EthernetClient client;
  Time t = rtc.getTime();
  float tempC = 25.55;
  float tempH = 22.55;
  float tempA = 20.55;
  float PHA = 8.4;
  float PHR = 6.4;
  int ORP = 400;
  int DEN = 1025;
  byte status_parametros = 255;
  byte temporizador_status = 255;
  byte Status = 255;
  boolean nivel_status = true;
  byte tpa_status = 255;
  char dados[6];

  Serial.println("Connecting...");

  if (client.connect("www.mySite.com", 80)) 
  {
    Serial.println(">> Connected <<");
    
    client.println("POST /MyFolder HTTP/1.1");
    client.println("Host: www.mySite.com");
    client.print("Authorization: Basic ");
    client.println(Auth1);
    client.println("Cache-Control: no-cache");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    String PostData =
    "{\"userName\":\""+
    String(Username)+
     "\",\"minute\":\""+
    String(t.year)+
    "-"+   
    String(t.mon)+  
    "-"+
    String(t.date)+
    " "+
    String(t.hour)+
    ":"+  
    String(t.min)+
    ":"+
    String(t.sec)+
    "\",\"A\":"+ // Temp. da água
    dtostrf(tempC,5,2,dados)+
    ",\"B\":"+ // Temp. dissipador
    dtostrf(tempH,5,2,dados)+
    ",\"C\":"+ // Temp. ambiente
    dtostrf(tempA,5,2,dados)+
    ",\"D\":"+ // PH do aquário
    dtostrf(PHA,4,2,dados)+
    ",\"E\":"+ // PH do reator de cálcio
    dtostrf(PHR,4,2,dados)+
    ",\"F\":"+ // Densidade
    String(DEN)+
    ",\"G\":"+ // ORP
    String(ORP)+
    ",\"H\":"+ // Status chiller, 0 = desligado e 1 = ligado
    String(bitRead(status_parametros,0))+
    ",\"I\":"+ // Status aquecedor, 0 = desligado e 1 = ligado
    String(bitRead(status_parametros,1))+
    ",\"J\":"+ // Status reator de cálcio, 0 = desligado e 1 = ligado
    String(bitRead(status_parametros,5))+
    ",\"K\":"+ // Status ozonizador, 0 = desligado e 1 = ligado
    String(bitRead(status_parametros,7))+
    ",\"L\":"+ // Status reposição de água doce, 0 = desligado e 1 = ligado
    String(bitRead(Status,1))+
    ",\"M\":"+ // Status niveis, 0 = baixo e 1 = normal
    String(nivel_status)+
    ",\"N\":"+ // Status TPA, 0 = desligado e 1 = ligado
    String(bitRead(tpa_status,0))+
    ",\"O\":"+ 
    String(bitRead(temporizador_status,1))+ // Status timer 1, 0 = desligado e 1 = ligado
    ",\"P\":"+ 
    String(bitRead(temporizador_status,2))+ // Status timer 2, 0 = desligado e 1 = ligado
    ",\"Q\":"+
    String(bitRead(temporizador_status,3))+ // Status timer 3, 0 = desligado e 1 = ligado
    ",\"R\":"+ 
    String(bitRead(temporizador_status,4))+ // Status timer 4, 0 = desligado e 1 = ligado
    ",\"S\":"+ 
    String(bitRead(temporizador_status,5))+ // Status timer 5, 0 = desligado e 1 = ligado
    ",\"T\":"+ // Sinaliza falha na TPA
    String(bitRead(tpa_status,2))+ 
    "}";
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);   
    
    delay(5);
    client.stop();
  }
  else
  {
    Serial.println("Connection failed");
  } 
}

I can’t help you with PHP unfortunately.

Best regards.

Fernando Garcia

Thanks a lot, I will surely try this out and get back to you.

Rochan