Yoctopuce and ChatGPT

Yoctopuce and ChatGPT

The advance of AI in recent years is revolutionizing many fields, including IT development. We wanted to do a little experiment on the topic. Is it possible to create an application using our modules without knowing how to program, but using ChatGPT? As we shall see, the answer is yes, but no...





The objective is a Windows application that displays the room temperature using a Yocto-Meteo-V2 and changes the state of output 1 of a Yocto-MaxiPowerRelay according to the temperature. If the temperature exceeds 25°, the relay must be opened, otherwise it must be closed. The Yocto-Meteo-V2 and Yocto-MaxiPowerRelay are plugged into the computer's USB ports, the application must display an error message if one of the modules is missing, and it must support hot-plugging of the modules. The program must be written in Python.

We put ourselves in the shoes of someone who doesn't know how to code, and who simply copies and pastes code between ChatGPT and the editor. When the code didn't work, we explained to ChatGPT what went wrong, including any error messages returned by the application.

We performed the same test in French and English. Finally, we used one chat per language throughout, with the discussion using the same context.

Feedback

In the end, we came up with a graphical application that works, but as we'll see, it wasn't all plain sailing.


The French version The English version
A screenshot of the two final applications.



We're not going to transcribe the two complete conversations with ChatGPT, as that would be way too much. We'll just give you a summary of the problems we encountered. However, if you're interested, the two conversations and the generated code are available in this Zip file.

French vs. English

We started from the same instruction (the first paragraph of this post), but once we wrote it in French and the other time in English. The final application is very different, but ChatGPT encountered the same problems during the implementation of this application.

During "development", ChatGPT made exactly the same mistakes (see below), but depending on the language in which we interacted, the proposed code is different. In both cases, the solution is valid but slightly different. Neither is better than the other, just different. It's as if two people were answering the same question.

Whatever the language used, ChatGPT made 4 errors that it managed to correct with 1 or 2 interactions.

Problem #1: including the Yoctopuce library

The first is the installation of the Yoctopuce Python library. For a Python program to use our modules, it must use our programming library. There are two ways of installing this programming library: with pip or manually. Depending on the chosen method, the Python code is not quite the same (for more details, see our tutorial).

The first version of the code proposed by ChatGPT doesn't work because it recommends installing the library using pip, but proposes code that only works if the library is installed manually in the same directory as the application.

By explaining to ChatGPT that the code doesn't work and including the error, it changes its mind and suggests that we install the library manually. This solution is not wrong, but it is more complicated than simply modifying the 3 lines of code to use the version installed by pip. What's more, the explanations concerning the files to be copied are incomplete and not very clear. In the end, after 2 or 3 interactions with ChatGPT, we managed to install the Yoctopuce library.

Once the library is installed, we have a code that displays the temperature on the terminal and switches a relay.

Problem #2: Hot-plug management

As we'd expressly asked for the code to support hot-plugging, and we're a bit of a tease, we unplugged a module to check, and the code crashed or stopped working once the module was plugged back in.

Again, we needed one or two interactions with ChatGPT for it to fix the problem. In both languages, the first version of the code was too naive and the AI simply hadn't dealt with the case of an absent module.

Once this error had been corrected, we tested the application and realized that two major errors remained.

Problem #3: Using the wrong relay

Note: If you're not used to using Yoctopuce modules, we recommend you start by reading our post "Logical structure of Yoctopuce devices" to understand the error ChatGPT made.

At this point, the application works, but switches output number 5 of the Yocto-MaxiPowerRelay when we had specified that output number 1 should be used. In fact, the code used the FirstRelay method, which allows all relays to be enumerated. ChatGPT made the assumption that the first call returns relay number 1, but there's no guarantee of the order in which the FirtXXX and NextXXX functions are enumerated.

On escalating the problem to ChatGPT, it realizes the problem and suggests we solve it by hard-coding the Yocto-MaxiPowerRelay serial number in the project, to enable it to use the hardwareID of relay 5. Technically, this solves the problem, but it's a bad idea as the application will only work with this Yocto-MaxiPowerRelay. As the serial number is by definition unique, all other Yocto-MaxiPowerRelays will be ignored.

The correct solution would have been, for example:

...
any_relay = YRelay.FirstRelay()
if any_relay is None:
    return "No Yoctopuce device with a relay found"
hwid = any_relay.get_serialNumber() + ".relay1"
relay_to_use = YRelay.FindRelay(hwid)
...



Now we're beginning to think that's a lot, but there's one last bug...

Problem #4: Relay state inversion

Testing the code, we realize that what is displayed in the logs is correct, but that the relay state is inverted, in other words when the application says the relay is open, it is actually closed, and vice versa. ChatGPT has inverted the direction of the STATE_A and STATE_B constants. Surprisingly, this was the error that it took the longest to correct. First, it inverted the log messages to make them consistent with the relay state (but therefore false). And it took several interactions for ChatGPT to correct the problem.

Positive points

Out of curiosity, we asked ChatGPT to make a windowed version of this application, and to be honest we were happily surprised. In a single sentence, it modified the code to make a graphical application using Python and Tkinter. The application isn't very pretty, but it works well (see screenshot).

On another positive note, we asked ChatGPT to create a README.md file to attach to the code. Again, in just a few seconds, we had a file that was perfectly usable. Funnily enough, it even mentions that the code won't work on other Yoctopuce modules because the serial number is hard-coded!

Conclusion

It's easy to list the errors of this AI and conclude that it will never replace a real developer (and to be honest, we agree). However, this technology is constantly evolving and every month ChatGPT improves. And when you compare where AI was when Yoctopuce was founded, you wonder what they'll be able to do in 2, 5 or 10 years' time.

To return to the basic question "Is it possible to build an application using our modules without knowing how to program, but using ChatGPT?", the answer is no. At present, ChatGPT isn't capable of generating reliable, maintainable code if you don't know how to program.

It's also likely that ChatGPT will perform better on more universal and better-documented problems. Yoctopuce is a very small company, and although documentation is abundant on our site, on a global scale it is insignificant compared with Google, Microsoft, and so on.

However, we have to admit that it's a tool that can save an enormous amount of time if you know what you're doing. For example, when writing the README.md file.

We've also read that some software developers have ChatGPT write a first version of the code and then correct it. I'm not convinced of this strategy. It may save time in the short term, but I think we run the risk of missing bugs that will appear much later and take longer to fix.

Finally, there's something emotional. Programming is fun! Outsourcing this part to a machine takes all the fun out of the job. If, instead of asking ChatGPT to create this code, the users had read our tutorials and tried it out for themselves, they would probably have taken longer, but they would also have learned something they could use again in the future.

We use ChatGPT from time to time, but mainly to obtain examples of use or documentation. And it's often more efficient than a Google search. It's able to synthesize several sources of information into a very easy-to-read summary. But we ALWAYS check this information against official documentation, and ChatGPT regularly simply invents new information.

In short, it's a very interesting tool, once you know its limitations and check what it does.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.