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 = "" ;
}
}
}
}
}
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.
– Wendell Mosquini Pozzatti