Arduino uno with enc28j60 push serial data string to php

Hi everybody,

I have a Arduino uno with a enc28j60. I try to send serial data string to a php page
that emails the data to me. It works if I type something in the serial monitor
but the data string is not send. is there somebody how has a suggestion?

thanks in advance

This is the code I use

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
unsigned long timer;

const char website[] PROGMEM = "www.mydomain.com";
#define AD  =  Serial.read();

void setup () 
{
 Serial.begin(9600);
 Serial.println("03 - Basic Web Client");

 if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
   Serial.println( "Failed to access Ethernet controller");
   
 // DHCP Setup
 if (!ether.dhcpSetup())
   Serial.println("DHCP failed");

 ether.printIp("IP:  ", ether.myip);
 ether.printIp("GW:  ", ether.gwip);  
 ether.printIp("DNS: ", ether.dnsip);  

 // DNS Setup
 if (!ether.dnsLookup(website))
   Serial.println("DNS failed");
   
 ether.printIp("SRV: ", ether.hisip);
}

void loop () {
 ether.packetLoop(ether.packetReceive());
 
 if (Serial.available()) {
   (Serial.read());
   Serial.println("Request sent");
   
   // Send some test data to the server:
   ether.browseUrl(PSTR ("/mail.php?alarm="),  "", website,  0);
 }
}

Is this const char website[] PROGMEM = "www.mydomain.com"; really in your code or you did not want to disclose your domain?

no i just changed the url

an your DNS can resolve whatever you've put in there?

if i type something in the serial monitor it i receive a email only i want the data that i type in the email

well you do not pass any data to your PHP script

    ether.browseUrl(PSTR ("/mail.php?alarm="),  [color=red][b]""[/b][/color], website,  0);

whatever you receive is what your PHP does...


Please correct your 1st post above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

this is the php code that i use

<?php

$to = "my email adres";
$subject= "alarm";

$body = $_GET["alarm"];

 if (mail($to, $subject, $body)) {
   echo("<p>send</p>");
  } else {
   echo("<p>not send</p>");
  }
 ?>

exactly my point... $body = $_GET["alarm"]; what do you pass in your arduino code for the alarm value with your function call:

ether.browseUrl(PSTR ("/mail.php?alarm="),  [color=red][b]""[/b][/color], website,  0);

try with ether.browseUrl(PSTR ("/mail.php?alarm="),  [color=green][b]"HELLO"[/b][/color], website,  NULL); Your eMail should now say HELLO to you

and if you want to send a specific variable you need to store its representation in a string (char array)

int aVariable = 123; // or whatever
char myVariableAsString[7]; // 7 bytes is enough to contain any int, including minus sign and terminating zero
itoa(aVariable, myVariableAsString, 10); // convert the variable value to a string represented in base 10
ether.browseUrl(PSTR ("/mail.php?alarm="), (const char *) myVariableAsString, website, NULL);

Yes if i put HELLO there is receive it in the email.

I used your code and i receive 123 in the message now I changed 123 with (Serial.read());
than i receive a msg with -1

My code is now like this

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
unsigned long timer;

const char website[] PROGMEM = "my domain";
int aVariable =   (Serial.read()); // or whatever
char myVariableAsString[7]; // 7 bytes is enough to contain any int, including minus sign and terminating zero

void setup () 
{
  Serial.begin(9600);
  Serial.println("03 - Basic Web Client");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
    
  // DHCP Setup
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  // DNS Setup
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());
  
  if (Serial.available()) {
    (Serial.read());
    Serial.println("Request sent");
itoa(aVariable, myVariableAsString, 10); // convert the variable value to a string represented in base 10
    // Send some test data to the server:
    ether.browseUrl(PSTR ("/mail.php?alarm="),(const char *) myVariableAsString,  website,  NULL);
  }
}

Serial.read only reads 1 character… you need to build up a string and when you have received the full string, then send it over

You should study in details Serial Input Basics to understand how to handle the Serial input.

I hacked this quickly together, not even tested. basically I added a function that waits for character to arrive on the serial line until I hit a end of message character which I set at ‘\n’. when I get that the function returns true and you can send your mail.

→ put your console at 115200 and make sure to send CR/LF at the end when you press enter in the console

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };

byte Ethernet::buffer[700];
unsigned long timer;

const char website[] PROGMEM = "my domain";

const byte sizeMax = 50;
char messageBuffer[sizeMax + 1];
byte index=0;

boolean waitForMessage(const char endChar)
{
  boolean fullMessage = false;

  if (Serial.available()) { // if there is at least one char waiting
    int rec = Serial.read(); // -1 if error, otherwise our char
    if (rec != -1) {
      if (rec == endChar) fullMessage = true; // end of message
      else {
        if (rec != '\r') { // ignore CR
          messageBuffer[index++] = (char) rec;
          messageBuffer[index] = '\0'; // mark end of string
          if (index >= sizeMax) index = sizeMax - 1; // do not overflow
        }
      }
    }
  }
  return fullMessage;
}


void setup ()
{
  Serial.begin(115200);
  Serial.println("03 - Basic Web Client");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");

  // DHCP Setup
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  // DNS Setup
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");

  ether.printIp("SRV: ", ether.hisip);
}

void loop () {
  ether.packetLoop(ether.packetReceive());

  if (waitForMessage('\n')) {
    ether.browseUrl(PSTR ("/mail.php?alarm="), (const char *) messageBuffer,  website,  NULL);
    // clean up the buffer for next time
    index = 0;
    messageBuffer[0] = '\0';
    Serial.println("Request sent");
  }
}

this should work with a least one word typed in, I don’t remember how $body = $_GET["alarm"]; handles spaces (probably handled as %20 and decoded correctly but I’ve note tested)

URL also have a max length (but I limited the buffer to 50 so you should be safe)

Mind code injection through this approach…

--> if that does not work, concatenate what you read from the console with /mail.php?alarm= in front and use that as first parameter to ether.browseUrl() call, leaving the second param as NULL

very bad practice and not respectful to start a new thread. so this is my last answer. good luck.