3
votes

I'm building a growbox/terrarium with arduino uno as the temperature controller. Simple sketch for arduino: if DS18B20 sensor giv less than 25'C than turn on relay, which the heating cable is connected to. Loop 30s, every time Serial.print(temperature) to the PC, where I'm collecting data and make timelapse photos. ---> Here is the problem.

After some time (from 15 min up to 4 hours). Serial communication with PC stops. When I'm trying to upload a new sketch to arduino I got an error msg: avrdude: ser_open(): can't set com-state for "\.\COM3"

I need to unplug and plug in again USB cable (or turn it off and on in Windows Device Manager), also restart Python app that is collecting data. (Very unsatisfactory fix).

So my questions are: 1. Why? 2. How to fix it? 3. Or how to make some workaround, for example reset COM port from code (desirably python2.7)

PS. Example of what I'm doing and how it works(and do not works) here: Life of pepper

PS2. My goal is to make controllable habitate for plants, where I can see behaviours differs depending on the temp, day-duration, light-intensity, humidity.

Please help me :-) .

ARDUINO UNO SKETCH

    #include <OneWire.h>
    #include <DallasTemperature.h>

    // Data wire is plugged into port 2 on the A

rduino
    #define ONE_WIRE_BUS 2
    #define PIN_HEATING 6

    float temperatura = 30.0;

    // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);

    // Pass our oneWire reference to Dallas Temperature.
    DallasTemperature sensors(&oneWire);

    void setup(void)
    {
      // start serial port
      Serial.begin(9600);

      // Start up the library
      sensors.begin();

      pinMode(PIN_HEATING, OUTPUT);
    }

    void loop(void)
    {
      delay(30000);

      if(temperatura<25.0){
        digitalWrite(PIN_HEATING, HIGH);  
        }
      else{
        digitalWrite(PIN_HEATING, LOW);  
        }

      sensors.requestTemperatures(); // Send the command to get temperatures
      // After we got the temperatures, we can print them here.
      // We use the function ByIndex, and as an example get the temperature from the first sensor only.
      temperatura = sensors.getTempCByIndex(0);

      Serial.print("#");
      Serial.print(temperatura);
    }

PYTHON2.7 RECEIVING PART

import serial #pySerial

ser = serial.Serial()
ser.port = "COM3"
ser.open()

data = ser.read_all().split("#")
datasize = len(data)
if datasize>1:
    temp = data[datasize-1]
    tempstr = " / " + str(temp) + "'C"
else:
    tempstr=" / ----- "
2

2 Answers

2
votes

I've experienced a similar problem in the past where the serial port would become unresponsive. The solution in my case wasn't very intuitive but worked nonetheless. Here's what might help you too:

  • Make sure the USB port you're using to connect the arduino isn't shared with other devices (for example using a USB multipoint hub). If you're using a desktop with multiple USB ports (usually found at the back of the desktop case), unplug all other USB devices (keyboard, mouse etc) and reconnect them to a different USB. Use the fastest USB port available available on your computer (USB 3.0 if present) and have it dedicated for communication with the arduino. The reason the communication port goes unresponsive sometimes is because of the momentary fluctuations in the voltage and/or current if you have several devices connected to it.
  • Make sure you have a stable power supply for you computer, voltage drifts due to heavy loading can sometimes cause PC peripherals to disconnect/reconnect.

If this doesn't work, try this quick software fix. Just before the delay(30000) statement in your code, add the following:

 if(!Serial) {  //check if Serial is available... if not,
Serial.end();      // close serial port
delay(100);        //wait 100 millis
Serial.begin(9600); // reenable serial again
}

This might at least ensure continued operation operation of your application with out requiring your intervention.

0
votes

So in my case the problem was solved by connecting my computer through proper surge supressor, formerly i had connectected it to wall with a simple cable, and I have old 2 wire, non-grounded, electric installation in my flat.