Read Serial Data from Python to Arduino with ArduinoJSON

82 views Asked by At

Python Code:

import serial
import time

# Open the serial port (adjust the port and baudrate as needed)
ser = serial.Serial('COM3', 9600, timeout=1)

def send_command(command):
    data = {"command": command}
    ser.write(str(data).encode('utf-8'))
    time.sleep(1)  # Give Arduino some time to process the data

# Example: Turn on the LED
send_command("ON")

# Close the serial port
ser.close()

Arduino Code:

#include <ArduinoJson.h>

const int ledPin = 12;
const char *command;

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

void loop() {
  if (Serial.available() > 0) {
    StaticJsonDocument<200> doc;
    DeserializationError error = deserializeJson(doc, Serial);

    if (error) {
      Serial.print(F("Error: "));
      Serial.println(error.c_str());
      return;
    }

    command = doc["command"];
  }

  if (strcmp(command, "ON") == 0) {
    digitalWrite(ledPin, HIGH);
  } else if (strcmp(command, "OFF") == 0) {
    digitalWrite(ledPin, LOW);
  }
}

For some reason it seems that the above code is not working properly. My intention is to have the LED consistently turned on when the 'ON' message is sent. Additionally, I expect the Arduino's Serial Monitor to continuously display the 'ON' message after it has been sent.


2

There are 2 answers

0
liaifat85 On

You can modify the python code like this and see if it works.

import serial
import json
import time

# Open the serial port (adjust the port and baudrate as needed)
ser = serial.Serial('COM3', 9600, timeout=1)

def send_command(command):
    data = {"command": command}
    ser.write(json.dumps(data).encode('utf-8'))
    time.sleep(10)  # Give Arduino some time to process the data

# Example: Turn on the LED
send_command("ON")

# Close the serial port
ser.close()

0
ChipChop Gizmo On

I don’t think you can use like that (at least not easily) the same Serial() to communicate with Python program and output to the Arduino IDE serial monitor. Basically if you are listening with some application to the serial port that your device is attached to then that application takes over the serial port (Arduino is a program as is your Python app)

If you have the serial monitor running when you upload the code I think then it will block Python from reading the port. Simply close the Arduino’s Serial monitor, restart the device and see if Python takes over and the led lights up.

If you implement in your Python code to also listen to the Serial port and output what it receives it will effectively also do the job of the arduino’s serial monitor.

On another note, in the main loop() you don’t need to constantly keep doing a digitalWrite, once you set a pin to HIGH or LOW it will keep that state until it’s explicitly changed to a different state.

void loop() {
  if (Serial.available() > 0) {
    StaticJsonDocument<200> doc;
     DeserializationError error = deserializeJson(doc, Serial);

    if (error) {
      Serial.print(F("Error: "));
      Serial.println(error.c_str());
      return;
     }

     command = doc["command"];

     if (strcmp(command, "ON") == 0) {
       digitalWrite(ledPin, HIGH);
     } else if (strcmp(command, "OFF") == 0) {
       digitalWrite(ledPin, LOW);
     }

   }
}