Turn on/off relay

I'm trying to turn a relay on and off, from a program I made in C#. Me When I click on the button I see the "RX" led of the ArduinoBoard turning on, that is, something comes to the Arduino, but the relay never comes on... I don't know what I'm doing wrong

C# Code

Class forms:

private void ACClick_Click(object sender, EventArgs e)
    {
        if (AC)
        {
        
            Arduino.SendInfo(Convert.ToString(AC));
            this.ArCondicionado.BorderStyle = BorderStyle.FixedSingle;
            AC = false;
        }
        else
        {
            Arduino.SendInfo(Convert.ToString(AC));

            this.ArCondicionado.BorderStyle = BorderStyle.None;
            AC = true;
        }
    }
}

Class to control my arduino info:

 class Arduino
{
    static SerialPort Port;

    public static int ConnectArduino()
    {
        int ArduinoPort = 0;

        try
        {
            if (Port == null)
            {
                Port = new SerialPort();
                Port.PortName = "COM3";//Set your board COM
                Port.BaudRate = 9600;
                Port.Open();
                ArduinoPort = 1;
            }
        }
        catch (Exception e)
        {

            Console.WriteLine("{0} Exception caught.", e);
            MessageBox.Show("O Arduino not connected", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
        return ArduinoPort;

    }
   public static void SendInfo(string message)
    {
        try
        {
            if (Port != null && Port.IsOpen)
            {
                Port.Write(message);
                Console.WriteLine(message);
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Exception information: {0}", e);
            throw new Exception(e.ToString());
        }
    }
}

Arduino code:

 #include "ArduinoJson.h"
  StaticJsonDocument<200> doc;
  #define relayO 7

  void setup() {
    pinMode(relayO, OUTPUT);
    digitalWrite(relayO, HIGH);
    Serial.begin(9600);
  }

  void loop() {
    if (! Serial.available()) return;
    String json = Serial.readString();
    deserializeJson(doc, json);
    JsonVariant RelayInfo = doc["AC"];
    if(!RelayInfo.isNull()) {
      if(RelayInfo=="True") {
        digitalWrite(relayO,HIGH);
      }
      else{
        digitalWrite(relayO,LOW);
      }
    }
    delay(1000);
  }

Have you tried printing what the Arduino receives ?
Is it what you expect ?

verify that the code can toggle the realy

void setup() {
    Serial.begin(9600);
    pinMode(relayO, OUTPUT);

    while (1)  {
        digitalWrite(relayO, HIGH);
        delay (1000);
        digitalWrite(relayO, HIGH);
        delay (1000);
    }
}

and as bob suggested, verify what is received

Receives a json with an object "AC" that has an assumed value (true or false but the type is a string).
I can't open the serial port in arduino IDE and connect to arduino (with my c# code), because it don't let's me use the same COM port on 2 different things. I don't know if I'm making myself understood

The relay is working I made a code similar to that was working fine. The problem is the Arduino interpret the information receives

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