Arduino restarting when starting python code execution

Asked

Viewed 100 times

0

I am new in the development part and mainly in using the Arduino. I am developing a python web application with some functions, one of them is to turn a light bulb on and off (my problem). I developed the code, used some ready-made web (for testing) and the big question is, when running python code, Arduino simply restarts and does not run the connection between the code and Arduin. I noticed that when starting the connection (connection = serial.Serial('COM3', 9600), the problem occurs. I tested with this piece of code inside Try and the problem persists. Does anyone know what might be going on and any idea how to fix it? ps: I am using Pyserial.

Python code:

import serial
import time
from time import sleep

conexao = serial.Serial('COM3', 9600) 
def escrever_porta():

   try: 
       valor = (raw_input("Digite o valor a ser enviado: "))
       conexao.write(valor)
       conexao.close()

   except serial.SerialException:
       print"ERRO: Verifique se ha algum dispositivo conectado na porta!"

   return valor

Arduino code (I’ve tried several):

#include "EmonLib.h"
#include <SPI.h>

int pin = 7;
char dados = 0;

void setup() {
  pinMode(pin, OUTPUT);
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
}

void loop() {
  if (Serial.available()) {
    while (Serial) {
      if (Serial.available()) {
        dados =(char)Serial.read();

        if (dados == "0") {
          delay(1);
          Serial.end();
          if (dados == "1") {
            digitalWrite(pin, HIGH);
          } else {
            if (dados == "2") {
              digitalWrite(pin, LOW);
            }
          }
          dados = "" ;
        }
      }
    }
  }
}

2 answers

0

Make sure the door is right, it is quite common for Sos to play Andy to another port, you are not actually using a virtual serial port (FTDI)?

  • Yes, the door is right, the problem is only in this code. In another code with another function, Arduino normally executes, the difference with what is described in the post is that the other code has a while while during its execution.

0

The python code is in Python2, would there be a chance that your python would be 3? If it is, the code should have some changes:

raw_input now is input and print will have parentheses ( counteúdo )

import serial
import time
from time import sleep

conexao = serial.Serial('COM3', 9600) 
def escrever_porta():

   try: 
       valor = (input("Digite o valor a ser enviado: ")) 
      #Esses parenteses antes do input é realmente necessário?
      #valor = input("Digite o valor a ser enviado: ")

       conexao.write(valor)
       conexao.close()

   except serial.SerialException:
   #tente também apenas "except:"
       print("ERRO: Verifique se ha algum dispositivo conectado na porta!")

   return valor

I hope I helped... If python really is 2.x, then the problem is just the valor

  • So Antony, first thank you for the answer. I made the corrections, my version is 2x, but it’s not the problem :/. I believe the problem is in the part of the code that creates a connection through the port com3, simply the Rduino restarts, the program continues running and without any error, but when sending the data to the board, nothing happens.

  • try to change everything that refers to the variable dados with just a single quote ( ' ) and not ( " ), maybe the error is in this second code. valor the command: conexao.open(), I looked a little bit at this pyserial and saw this command. Are just a few kicks I give, I have no knowledge in the area, just trying to help, hehe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.