2

This is a question about modifying sketches other than through the IDE.

I'm using an Arduino Uno r3 + Arduino WiFi Shield (not the 101). Up to now I've been using the IDE to manually change the ssids and passwords to connect to wireless networks. As a next step, I'd like to create something that will let update the ssid and password from a source other than an IDE, preferably a batch or bash file. Over the air updates are out of the question (way too advanced) so it is safe to assume that all updates will be done via the USB cable. Based on my limited knowledge of what I think needs to happen, I need to solve the following high-level problems:

  1. Get the computer to recognize that a gateway is plugged into a USB port
  2. Prompt the user to enter the new ssid and network password
  3. Write to the ssid and password fields
  4. Compile the code and save it (equivalent compile/upload in the IDE)
  5. Run the sketch to find and connect to the network

I'd like this to look like two blank fields and a "submit" button to the user.

My imaginary use case is that I'm installing a clone of my gateway/sensor network at a technically-illiterate grandparent's house. I have no idea where to begin or even if such a thing is possible!

Assuming that it is possible, how can I make it easy for them to configure the gateway for use on their home network without resorting to the IDE?

2 Answers 2

1

Yes, this is possible. However, you need to have some programming skills (Java, C, C++, anything really that can cope with serial comms) to write the computer-side program.

Forget 'automatic' running of a program without installing a service on the target computer to constantly monitor incoming serial comms. That said, there's nothing to stop you doing that but to me it's an unnecessary extra step. If the End User can handle connecting the Arduino to the computer without having a breakdown, then they can probably cope with launching the input program themselves.

Hence, write a program for the target computer that analyses COM ports for active connections. For each connection, listen for some kind of identification sequence. Perform a basic 'handshake' to verify that it's found the Arduino. Once it has, allow the User to enter the WiFi details, which are then passed on to the Arduino via serial. If you're feeling slightly adventurous (the necessary code is provided with the Arduino IDE as an Example, so it's no real challenge) you could have the Arduino scan for networks and offer the User a list to choose from.

On the Arduino, include within the sketch a method of opening serial comms with the target computer. This can be either:

  • Within loop(), at regular intervals, which becomes redundant after you've performed the setup correctly; or
  • A one-time affair within setup(). Each time the Arduino is powered on it attempts to establish comms with the computer, but times out after 30 seconds of 'silence', thus the User has to launch the program on the computer within a reasonable time.

Whichever approach you choose, the code in the sketch follows a similar essence - send a unique message to whatever is listening (if anything at all!) and wait for a specific response. If the computer is connected and the program is running, it should answer. Provided the answer received by the Arduino is expected (thus 'handshake' mentioned earlier), it should then wait for the SSID/pass to be entered by the User and sent by the computer. From there, connecting the Arduino to the network is a trivial process.

1

You could also make use of the Arduino serialEvent() function. Behind the scenes, every time the loop() function returns, this function (if you define it) is automatically run by the Arduino if the Serial buffer contains data. So all you have to do is define the serialEvent() function and have it read from the serial port and compare what it gets against some pre-defined handshake value. If the read data is a valid handshake, the Arduino should respond with a unique value too and then await the SSID, password and other stuff you wish to set. This means your can run your PC program anytime and simply have it send the handshake and wait for the Arduino's response before sending the wifi details. You can use serialEvent() like this:

    void serialEvent(){
        if (Serial.read() == 'Z'){ //if 'Z' is your handshake value
            Serial.write('X');   // if 'X' is the response expected by the PC
            getWifiDetails();   // the handler that gets the needed data
        }
        while (Serial.read() != -1);  // flush to get rid of any garbage
    }

The handshake value could be more than 1 byte to increase chances of uniqueness. This method assumes you aren't doing anything else with the serial port, since that could bring garbage to the forefront of Serial.read() calls and cause false triggering of serialEvent() and its IF condition. You could implement a timeout in your handler to handle such events though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.