I'm new to development and especially to using Arduino. I'm developing a web application with python with some functions, one of them is turning on and off a light bulb (my problem). I developed the code, I used some already ready from the web (for tests) and the big question is, when executing the python code, the arduino simply restarts and does not execute the connection between the code and the arduino. I noticed that when I start the connection (connection = serial.Serial ('COM3', 9600), the problem occurs, I tried that part of the code inside Try and the problem still persists. solve? ps: I'm 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 (already 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 = "" ;
}
}
}
}
}