Serial Monitor Problem (TX and RX only works after open serial monitor)

Hello,

I am trying to control the Arduino from a web page without a ethernet shield.
I can communicate with the Arduino (TX and RX working) , however I always have to open the serial monitor first.

How can I communicate with the Arduino without open the serial monitor?

Here is the code:

char c;
void setup()
{
  Serial.begin(9600);
  pinMode(30, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(28, OUTPUT);
}

void loop()
{
  if (Serial.available() > 0)
  {
    c = Serial.read();
  }
  else
  {
    delay(100);
  }
  if (c=='a')
  {
    digitalWrite(24, HIGH);
    digitalWrite(26, LOW);
    digitalWrite(28, LOW);
    digitalWrite(30, LOW);
  }

  if (c=='z')
  {
    digitalWrite(26, HIGH);
    digitalWrite(24, LOW);
    digitalWrite(28, LOW);
    digitalWrite(30, LOW);
  
  }

  if (c=='x')
  {
    digitalWrite(26, LOW);
    digitalWrite(24, LOW);
    digitalWrite(28, HIGH);
    digitalWrite(30, LOW);
  }

  if (c=='y')
  {
    digitalWrite(26, LOW);
    digitalWrite(24, LOW);
    digitalWrite(28, LOW);
    digitalWrite(30, HIGH);
  } 
  
  if (c=='p')
  {
    digitalWrite(26, LOW);
    digitalWrite(24, LOW);
    digitalWrite(28, LOW);
    digitalWrite(30, LOW);
  } 
  
  c='\0';
}

Best Regards,
Tiago

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).


The Arduino opens its serial line.
A program running on your computer needs to connect to the Serial port.
then you just exchange data

how you do this from a web page is another question I guess.. (where we need more info)

I am sending values from php to the arduino.

<?php
 
$port = fopen("COM6", "w+");

if($port == NULL){
    echo "bad";
}
if ($_POST['estado']=="forward")
{
    echo "forward";
    fwrite($port, "a");
}

The idea, is after clicking in a button made in html sending the value "a" to the arduino.
This is working, I can receive the values and control my OUTPUTS as I want.
However, to make this communication I have to open the serial monitor first. I am not understanding why I have to open the serial monitor to make the TX and RX working.

How do I communicate without open the serial monitor?

Regards,
Tiago

that reboots your arduino each time you ping your PHP script...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.