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);
}