35 Replies Latest reply on Mar 20, 2014 2:05 AM by mcb1

    Help, my Processing code is restarting my Arduino code

    kjhart0133

      Hello all,

      I'm developing some Arduino code using XBees to sense environmental data at a remote location. My Arduino code is working great and I'm now ready to upload my remotely acquired data to my computer so that I can parse the data and send it via an SMS message to my smart phone. This way I can monitor my remote location from anywhere.

      I'm using a chunk of code I got from the "Getting Started with Processing" book by Reas and Fry. I am able to get the Arduino/XBee data into my computer this way, but I have a problem/issue:

      The Arduino code loops continually, but each time I start the Processing Code, it restarts (resets?) the Arduino code. The Arduino code starts over from the beginning going through setup and then into the loop. I have no idea why this is happening. Here is the Processing code I'm using:

      // Based on Example 11-07 from "Getting Started with Processing"

      // by Reas & Fry. O'Reilly / Make 2010

      // This sketch, as modified by kjh, reads data from an Arduino, stores it in an array

      // then saves the data to a file on disk.

       

      import processing.serial.*;

       

      Serial port;                     // Create object from Serial class

      int asize = 8;                 // define size of data array

      byte[] val = new byte[asize];    // Data received from the serial port

                                       // representing the setting of the pot

      int i = 0;                       // array index

      int x;                           // scratch value

      float y;

       

      void setup() {

        //size(440, 220);

        // IMPORTANT NOTE:

        // The first serial port retrieved by Serial.list()

        // should be your Arduino. If not, uncomment the next

        // line by deleting the // before it. Run the sketch

        // again to see a list of serial ports. Then, change

        // the 0 in between [ and ] to the number of the port

        // that your Arduino is connected to.

        println(Serial.list());

        String arduinoPort = Serial.list()[2];

        port = new Serial(this, arduinoPort, 9600);

      }

       

      void draw() {

          if (port.available() > 0) {        // If data is available,

            x = port.read();        // read it and store it in val

            val[i] = byte(x);

            y =  float(x);

            //println(y);

            println(val[i]);

            //val = map(val, 0, 255, 0, height);  // Convert the value

            i++;

          }

          if (i > asize-1){

            saveBytes("arduino_data.dat", val);

            println("Data written.");

            exit();

          }

      }

      My Arduino code does not read data from the computer or Processing, it only sends data to Processing. The Arduino code is quite long and involved using a fair amount of XBee interface code so I didn't include it here. But, as I said, it is running fine except for being restarted each time I run the Processing code.  I'm using an Mega Arduino board connected to my PC via the USB cable. 

      I greatly appreciate any assistance on this.

      Thanks,

      Kevin H.

        • Re: Help, my Processing code is restarting my Arduino code
          wyldhunt

          Nearly every time I've seen a problem with Arduino re-booting, it has turned out to be power issues.

          I'm not sure what the X-bee power requirements are, and I've no clue about what else you might have running off of the same power supply as the Arduino board. If you are running this whole thing on a 1 amp power supply, there is a good chance that you just need more power. The Arduino re-boots very easy any time it sees any kind of a power fluctuation.

          Double check the power requirements of everything that you have running off of the arduino and/or the arduino's power supply and make sure that your supply is rated at least about 50% higher than your estimate.

          If you have any motors that start whenever the connection is made, you need to make sure that they are not feeding noise in to the circuit.

           

          If it's not a power issue:

          Since the Arduino is not receiving any commands from the PC, I doubt it's the Processing code. The X-Bee itself does establish a connection (X-Bee attached to the PC via USB connects to other X-Bee's and the PC itself using Serial?), which might cause a small power spike as it initializes itself (See above). The only non-power related thing that I can think of which would cause a reset would have to be the x-bee code itself. Double check all of the x-bee code (I've never used an x-bee, so I can't be very specific here) that runs while it is attempting to send packets of info. If your Arduino code waits for a viable serial port connection with your PC before sending anything, that code would be a good place to start. If it ever gets stuck in an infinite loop or similar, it can crash and reset itself.

            • Re: Help, my Processing code is restarting my Arduino code
              kjhart0133

              Bill,

               

              You may be on the right track thinking it's a power supply issue.  I'm running a Mega, a 4x20 LCD and one XBee on a shield, all from the USB cable.  The Processing code reads the data from the Mega over the USB, so that has to draw extra current, and that's when the reset occurs.  I tried hooking up a fresh 9V battery to the Mega to supplement the USB power and the Arduino code still restarted, but I still agree with you it could be the power.  I'm going to buy a hefty power supply from Adafruit and see if that helps.

               

              It will be a few days before I get the supply so stand by for the results.  If this solves it, you'll get the points for "Correct Answer!"

               

              Thanks,

               

              Kevin H.

                • Re: Help, my Processing code is restarting my Arduino code
                  mcb1

                  Kevin

                   

                  Personally I haven't used processing, but the example here differs from yours

                  http://www.processing.org/reference/libraries/serial/Serial_available_.html

                  in that they aren't converting the port number to a string.

                   

                  // Example by Tom Igoe
                  
                  import processing.serial.*;
                  
                  // The serial port:
                  Serial myPort;
                  
                  void setup() {
                    // List all the available serial ports:
                    println(Serial.list());
                    // Open the port you are using at the rate you want:
                    myPort = new Serial(this, Serial.list()[0], 9600);
                  }
                  
                  void draw() {
                    while (myPort.available() > 0) {
                      int inByte = myPort.read();
                      println(inByte);
                    }
                  }
                  
                  

                   

                   

                  I seriously doubt that a Mega, LCD and Xbee are able to draw enough to drop the voltage of a 9v battery down to below 5.5 volts (or 7 if its not a LDO regulator).

                  You should be able to easily measure it, which will give you a clue.

                   

                  The boards detect the Vin and if present doesn't use the USB power, so it will run on your 9v battery if its connected.

                  If you think its spikes, then a 1000uF or larger across the 5v line should prove it.

                   

                  Cheers

                  Mark

              • Re: Help, my Processing code is restarting my Arduino code
                kjhart0133

                I'm out of town until March 13 when I hope to have a new PS to test out these theories.

                 

                Thanks for the great replies.

                 

                Kevin

                • Re: Help, my Processing code is restarting my Arduino code
                  logan_kiefer

                  That sounds like a really great product I would be more then up to try it out for you.

                  • Re: Help, my Processing code is restarting my Arduino code
                    ipv1

                    The XBee CTS pin is usually wired to the reset pin of the arduino... This has caused some havoc in my past projects...

                    Lemme know if this is a possible issue.

                    Cheers.

                      • Re: Help, my Processing code is restarting my Arduino code
                        kjhart0133

                        Mr. Singh, I think you have hit the nail on the head.  I hooked up an oscilloscope to the reset/ line on the Mega and observed that each time I started my Processing code the reset/ line was asserted low!!  Looking at the Arduino Mega schematic, it clearly shows the Mega's USART chip's CTS/ line wired to the ATmega chip's reset line.  Apparently, when the Processing code wants to read data on the Serial/USB port it sends control packets to the Mega's USART that somehow assert the Mega's CTS/ line low.  I've included a picture of the reset line being asserted when I run the Processing code.  This happens each time I start the Processing code.

                         

                        I guess my question now is: what do I do next?  Is there a way to 'turn off' the CTS/RTS flow control on the Mega?  The data sheet for the ATmega16u  USB controller shows a register, UCSRnD, that has a CTSEN (CTS enable) bit, but that register name is not recognized by the Arduino compiler so I don't know how to turn that enable bit off, or if that would even solve the problem.

                         

                        Any suggestions are appreciated.

                         

                        Thanks,

                         

                        Kevin H.

                        IMG_7330.jpg

                      • Re: Help, my Processing code is restarting my Arduino code
                        wyldhunt

                        With Singh's helpful post to guide me, I managed to track down a blog where someone else fixed this issue.

                        Take a read and see if it helps you:

                        Random Stuff - Adventures in Nerd Land: XBee woes..

                         

                        The important snippet is:

                        "Anyway, turns out the solution for Annoyance #1, Mysteriously Resetting Arduino is also extremely simple. When using X-CTU to configure your EndDevice, simply set IOD7=0 (or use ATD7=0, same thing). The XBee will no longer reset your Arduino simply because it wants to sleep."

                        • Re: Help, my Processing code is restarting my Arduino code
                          ipv1

                          I see the problem... Well you can always use the solution you are currently using aka "Magic switch" or... Linux?

                          • Re: Help, my Processing code is restarting my Arduino code
                            peteroakes

                            you are using different USB to serial chips, thats why the difference

                             

                            the configuration of drivers is not int he domain of windows itself but in the driver downloaded to control the hardware, one is using the ATMEGA 32U?? built into he board, the other is using the FTDI adapter chip presumably built into a cable or as a separate little board. this is why your not seeing the same settings.

                             

                            is the board re-booting really that much of an issue. By leveraging the built in flash of the arduino you can prevent the restarts from loosing your collected data, adding an SD card could also be used. I agree when uploading new code etc to the target would still need addressing if your making use of the same serial connections but that was not indicated int he original question, and unless the monitoring PC is going to be permanently connected to the Bluetooth module there will always be a connection issue of some kind. better to write code to avoid the problem entirly

                             

                            can you provide a simple diagram in the post showing how everything is connected together from the PC right through to the remote collection node

                             

                            Thanks

                              • Re: Help, my Processing code is restarting my Arduino code
                                kjhart0133

                                Peter,

                                 

                                That's great info about the ports.  I'm learning a lot in this thread.

                                 

                                Here's a block diagram of my system that shows all the components.  I run Processing code in the PC that uploads data from the Mega.  The Mega also executes Arduino code that operates the controller XBee, which reads/writes the two end point XBees.

                                 

                                After fixing the problem with Processing resetting the Mega, I now only have to write some code to parse the data into an email to Verizon; Verizon then sends an SMS to my cell phone.

                                 

                                Let me know if there are any questions on this.

                                 

                                Kevin

                                shop_control_block_diagram_1_140318.jpg

                                  • Re: Help, my Processing code is restarting my Arduino code
                                    peteroakes

                                    Excellent diagram, thanks, very helpful

                                     

                                    So based on your description, when you plug the 2560 into the PC and start your processing application it will cause the Mega to restart, this as already pointed out is due the the USB to Serial chip on the mega issuing a reset to the mega in anticipation of uploading code , it is pretty standard serial communication protocol behavior (Not the reset bit, just the setting of the additional handshaking lines), the up-loader code on the IDE uses this feature to switch the Arduino into the boot-loader and ready it for code upload.

                                     

                                    My first question is :- Is the resetting of the 2560 also causing a reset of the remote Xbee devices due to re-establishing the links ?, if so then connecting the reset line of the XBee #1 to an output pin rather than the same reset as the Arduino would allow this to stop, you would add code to reset the Xbee if needed via the port or simply tie it to the correct logic level. You could add code to the mega to respond to a command to issue a reset instead.

                                     

                                    If it is not causing the remote controllers to reset and it is simply the local Mega 2560 then are you data logging on it or storing certain code into ram until the PC is connected, you could try saving it into the flash ram if that is an option. What I am asking here is why is the resetting an issue ?, what is it doing that is causing a problem for you.

                                     

                                    Perhaps posting your code as a PDF or zip file will be possible, I will be happy to have a look and provide suggestions

                                      • Re: Help, my Processing code is restarting my Arduino code
                                        peteroakes

                                        To add to my previous post, with the 2560, you could also have the XBee connected to serial 2 or 3 rather than the one connected to the same USB interface, you would have to code the serial yourself but that should not be too difficult. the XBee pictures I have been looking at have jumpers rather than the switch to select how the XBee is connecting to the Arduino, simply remove them both and hook into the second serial port instead and adjust your code appropriately... just a thought

                                      • Re: Help, my Processing code is restarting my Arduino code
                                        ipv1

                                        Things just became a whole lot clearer. I thought you were programming the arduino over the Xbee. I think at this point you are sharing a serial port between the PC/USB/Atmega8u and the xbee. Of course I am possibly wrong.

                                        There is a better way to do what you are doing... cut out the middle man. If you are going to use the PC with the processing app anyways, get an XBee ftdi adaptor for the Xbee module. Use the processing app to directly access the Xbee and talk to the nodes. This way you cut out the MEGA2560 entirely. This is good if you are not looking to use any other feature of the mega.

                                        If this makes sense, then we can move onto how to do the above. If not, you might need to give us a better idea of what you are trying to accomplish and more importantly what are your future plans.

                                        Sincerely,

                                        IP

                                    • Re: Help, my Processing code is restarting my Arduino code
                                      kjhart0133

                                      OK, I'll respond to several points:

                                       

                                      1.  I am not sharing the Serial/USB port between the Processing code on the PC and the XBees.  If you look closely on the diagram you'll see that XBee #1 is connected to SERIAL 1 on the Mega.  The PC is connected to the Mega via the usual Serial/USB interface.  I'm sorry it wasn't clear on the diagram; I should have made the little Serial 1 box bigger.  Anyway, this is why I chose the Mega, because it had additional serial ports.  And it all works well now.

                                       

                                      2.  As to the question of why do I care about repeated resets.  Technically I shouldn't; the resets don't bother anything, more a nuisance.  On the other hand it's not really good programming.  Somewhere down the road I might want to add code that might be adversely affected by repeated resets -- I don't know, just thinking out loud.  Also, I will want to poll my remote sensors repeatedly to monitor conditions in the remote location.

                                       

                                      3.  I really like the suggestion to use Processing directly to run XBee #1 and eliminate the need for the Mega altogether.  Equally interesting is the suggestion to use GMS or a modem to eliminate need for the PC.  I'm going to have to give these two ideas some thought and some research.  I've never heard of GMS so I'm off to Google it.  Maybe I'll code up a simple test to try them out.  Either of these two plans would significantly simplify my system.

                                       

                                      4.  Uploading my Mega code.  Hmmm.  First, I can't see how to upload files.  There doesn't seem to be a button to do that.  Second,  I'm embarrassed to show my code.  I'm not a programmer so my code is not what anyone would call pretty.  Still, if someone really wants to see it, let me know and send me a hint on how to upload it.  I can just send it as an Arduino .ino file and that will preserve all the colors, etc., when you open it in the Arduino IDE.

                                       

                                      Thanks again for great ideas and helpful suggestions.

                                       

                                      Kevin

                                        • Re: Help, my Processing code is restarting my Arduino code
                                          ipv1

                                          Wanna share code? Social Coding? want people to contribute? Share and help?

                                          The answer http://www.github.com

                                          • Re: Help, my Processing code is restarting my Arduino code
                                            peteroakes

                                            I've been programming for over 35 years and some of mine still looks ugly, what matters in your position is "does it work "and "is it bug free", the rest will come with experience, and were not here to criticize, were hear to help you and I can guarantee any seasoned programmer will have seen worse.

                                             

                                            Sorry about the serial confusion, I did not look that closely at the diagram and assumed you where plugging the xBee via a shield onto the Mega and therefor using the standard connections. My Bad

                                             

                                            GMS, I think that may be a typo, and possibly should be GSM as mcb1 stated, it is a standard Cell phone network system, the only issue there (Not technical) is needing an carrier account ( Phone Number) for each node with a GSM module. you could get away with just the primary node and have it relay to the rest and its worth considering. if the nodes are spread out enough to justify it.

                                             

                                            Alternate Suggestion

                                            As an alternate approach if your open to suggestions, my home network of nodes use simple radios to talk to each other (NRF24L01) in a mesh, a master node (Mega or similar) has an ENC28J60 to provide access from the internet.  http://www.google.ca/search?q=enc28j60

                                             

                                            then NETIO (Available for Android and Iphone) is used to provide remote monitoring from anywhere that has internet access which if using a cell phone with a data plan is pretty much everywhere  http://netio.davideickhoff.de/ this in inexpensive and works very well,

                                             

                                            A friend and buddy developer in England uses the above setup to remotely monitor his home in Spain from the UK, very reliable and inexpensive to setup,

                                             

                                            In your setup, if the actual monitored nodes are close together (Bluetooth range so less than 30 feet or so) so you do not walk around with the PC to get into range of them this would also work as the Radios have more range than Bluetooth and will also relay to nodes not in range. supports hundreds of nodes if you want to.

                                             

                                            This mirrors what I think your trying to achieve, certainly functionally, and at a lower cost. You could always add instant messaging to the master controller for alerts etc. My friend and I have been working on this for a while and he has a great blog describing it all  http://www.scargill.net/tag/netio-comes-to-the-rescue-home-control/

                                             

                                            either way you want to take this, i'm happy to continue assisting as best I can. I hope you are finding the feedback useful

                                             

                                            If there are enough people interested I will see if I can post the code we use and any other technical details on the forum, (I will bounce it of my friend first as he has written quite a bit of the code). It supports PWM, several temp sensor types, other Analog inputs, relay outputs, schedules, and we are adding features all the time.

                                             

                                            Peter

                                            • Re: Help, my Processing code is restarting my Arduino code
                                              mcb1

                                              Kevin

                                              here is a link to one version ... sorry we refer to them as GSM, but overseas they use GRPS.

                                              http://www.seeedstudio.com/depot/gprs-shield-v20-p-1379.html?cPath=132_134

                                              wiki http://www.seeedstudio.com/wiki/GPRS_Shield_V2.0

                                               

                                              As peter eluded you do need a SIM, however depending on your providers you may be able to get a data sim, or the equivalent which means a minimal monthly cost.

                                              From your block diagram there is only one connection to worry about.

                                               

                                              The other option to consider is adding an Ethernet connection and eliminating the PC.

                                               

                                              Anyway, now your resetting issue is resolved, you can continue testing and ironing out any small issues.

                                               

                                              Mark