Moving code from ESP8266 to ESP32

Image
A while ago I made a mashup of Dan Royer's code CNC 2 Axis Demo with my own code for trapezoidal motion stepper and servo control for ESP8266.

I assumed porting the code to the ESP32 would be trivial, and that was true for the most part: changes like library name being Wifi.h instead of Wifi8266.h were not a problem. UDP now does not like multicharacter writes but you can use print instead. So far so good.

However, when it came to the interrupt code I was stuck with the stepper interrupt causing an exception sometimes. And to make things weirder, the servo interrupt worked flawlessly (both of them had the IRAM_ATTR directive if you ask me).

Going little by little, I could narrow down the culprit to a floating point operation during the interrupt, that would cause problems sometimes but not always. Browsing around I found this post. Where the solution was simple: do not use floats within the interrupt routines but doubles. The reason was the float calculation would be performed by…

Wireless 3D printing

Last week I had a "USB cable incident" with my Prusa (computer and printers are on different tables in my office). It was entirely my fault as I walked across the USB cable and I almost threw my printer to the floor. As a result, the ferrite torus I put in the USB cable to prevent EMI problems broke. Without it, I know prints may interrupt anytime due to interference.

While on eBay looking for a replacement, I realized that a Bluetooth module was costing less than $15 now. So I thought I could give it a chance to the idea of operating my printers wirelessly.   Later, I learned of an even cheaper module at $6.60, but this one is 3.3v only (which is ok if you are using this voltage for your Arduino, but I am using 5v).

A quick look at Sprinter firmware and Arduino Mega and RAMPS board specs revealed that Serial2 port using 16 (TX) and 17 (RX) pins was easily available on the ext4 connector of RAMPS board. And so was GND and +5V. Making it very easy to connect the Bluetooth module.



And for the firmware changes, I do have an easy solution if you can give up the usage of the USB serial communication for printing (which I was). Just do a search and substitution of string "Serial." by "Serial2." in Sprinter.pde (same idea could be applied to other firmwares) and that is all. This way your printer will no longer be usable from the USB but you can always go back by reloading the former version of firmware. Please note that if you enable SD printing you may need to do the string replacement in some other source files too (same simple replacement).


You need to configure the module to use 115200bps (or change the firmware to 9600bps which is the default module speed) and you can do that by selecting the AT mode and using serial interface (3.3 volts only!) you use AT+UART=115200,0,0 command. Please note this module uses 38400 bps when in AT mode. For this job I used Spakfun's FTDI basic breakout module.

Once configured, the module can be wired to the RAMPS board with four wires (GND, +5V, RX, TX).  Select the board in your favorite software (I use Pronterface) and you can connect and interact with your printer.

Problems ahead

Unfortunately, not all was a success (yet). For some weird reason, still to be determined (defective Bluetooth module, bad module firmware, ...), my happiness is not complete as there is a problem. Once I start printing, the print job is interrupted after a few moves.

The cause of the problem is that my Bluetooth module misses some characters in the RX line if Mega is not transmitting a character for more than ten seconds. The end result is that "ok" responses are damaged or lost entirely whenever the motion takes longer than ten seconds, so printing of large objects becomes a problem. But Bluetooth connection is never lost, so I guess the cause it is a poor implementation of power management in the Bluetooth module.

I hope that by replacing the module firmware or by adjusting some parameter I will be able to fix the problem. If not, I will be forced to buy a different module and keep this one for another task where this behavior won't be a problem.

Update: Once I replaced the Bluetooth module behaving badly from another [cheaper] supplier I have got it all working nicely and wirelessly at last.  It works in Linux, Windows and OSX.

Comments

Justblair said…
Seems to me that neither bluetooth or USB are going to give you the secure connection that you require.

Better option is to use a SD card and use your bluetooth to start the print.

I have been using SD for a while now and it never skips a beat. I am using an LCD display and rotary encoder so there is no need for any serial connection at all.
Miguel Sánchez said…
Bluetooth and SD sounds like a good combination.

I have ordered a new module from another manufacturer to give it a try.
julianh72 said…
Nice work! Definitely agree with the suggestion to use Bluetooth + SD-Card. That way, the connection is only required for initiating the print and keeping an eye on progress. If the connection drops out for any reason, it doesn't really matter coz' the RAMPS can just carry on printing from the Gcode on the SD-Card. I did something similar, only using XBree instead of Bluetooth - see my blog entry at: http://julianh72.blogspot.com.au/2011/11/look-ma-no-wires.html
Miguel Sánchez said…
I saw your post Julian and I did have some Xbee around too but I thought my fellow reprapers could do it even cheaper (many computers do have BT interface, and those who don't can use a $2 BT dongle). Besides, the XBee way was already taken ;-)
Soundwave said…
Why dont you use AUX-1? You could keep the Serial command. Plug and unpluge the module for use or not.
Miguel Sánchez said…
I was not aware of AUX-1 port.

But do you mean using it as a Serial port? Arduino doc does not state these pins as a hardware serial port.

Can you elaborate on the idea?

Thanks.
Miguel Sánchez said…
Ok, I'll answer myself: RAMPS 1.4 schematic has the serial tag over a socket with outputs A3 & A4, these are not a serial port, but the other socket is D1 and D2 that are RX & TX of serial port.

So yes, using that would be a better idea, in fact I am just going to switch to this output.

Thanks for the tip!
Soundwave said…
Yes, thats what I mean. I just ordered on of this cheap Bluetooth myself from China.

As far as I understand it you can also programm them using an Arduino. Just connect them to some pins and the arduino to pc with usb and run some easy code.

I ll blog about it too, as soon as my item arrives from china.
ThePCaid.com said…
Great work,

have you tried it with the android app?
https://play.google.com/store/apps/details?id=com.hermit.btreprap

the needed sparkfun dongle is quite costly, just wondering if you can control it with your "random" dongle aswell... ?
Miguel Sánchez said…
Thanks for the tip. I am going to try this out. It seems it is going to work ok ... as I have managed to make it work from Windows, OSX and Linux.
Sasi said…
Did you have any luck with the app?

I've tried it with an android ice 4.04 but it gets stuck at the connecting stage in the app, while the lights turn green on the bluetooth indicating "connection"
Miguel Sánchez said…
I could not find the APK file. Unfortunately Google Play stopped working on my 4.0.3 Galaxy S. I cannot make it work then.
Sasi said…
It works like a charm on the S3. I'm using the bluemate silver as bluetooth module.

Miguel Sánchez said…
I've downgraded my phone OS to Froyo (long story, don't ask) and I have been able to test the android app with this Bluetooth dongle. It works nicely!
Jorge S. Anis said…
Good Job, any wait i get "conecting..." anda nothing happends :(

Popular posts from this blog

VFD control with Arduino using RS485 link

4xiDraw: Another pen plotter

Software I2C for Arduino