Electronics

How to pass firewall using PlatformIO espota ArduinOTA upload

ArduinoOTA’s protocol tries to connect to the host which is trying to program the device on a randomly chosen port. This often leads to the packets being filtered in a firewall since no rule exists to pass the packet and they are not related to an existing connection.

You could create a firewall rule to pass all traffic from the ESP8266/ESP32 to the programming host, but that is extremely insecure since it allows a hacked IoT device to hack your devices.

In order to fix it, add a fixed host port in platformio.ini using

upload_flags = --host_port=55910

and add these firewall rules:

allow from <programming host> to <IoT device> port 55910 TCP
allow from <IoT device> to <programming host> port 55190 TCP

Complete platformio.ini example:

[env:d1_mini_ota]
extends = env:d1_mini
upload_protocol = espota
upload_port = 192.168.178.25
upload_flags = --host_port=55910

 

Posted by Uli Köhler in Embedded, ESP8266/ESP32, PlatformIO

How to use ESP32 as USB-to-UART converter in PlatformIO

The ESP32 can easily be used as USB-to-UART converter. Note that the ESP32 does not feature an USB interface by itself and ESP32 boards with an onboard USB connector just use an USB-to-UART converter (this is a separate chip on the board). Hence, from the ESP32 perspective, it

You can map UART TX and RX to any GPIO pin on the ESP32. While there are very, very slight performance advantages to using a set of predefined pins, this does not really matter in practice. In this example, we will use pin GPIO2 for UART RX and pin GPIO4 for UART TX.

Basically, the code is just copying bytes between Serial2 and Serial (connected to USB):

// Copy bytes incoming via PC serial
while (Serial.available() > 0) {
  Serial2.write(Serial.read());
}
// Copy bytes incoming via UART serial
while (Serial2.available() > 0) {
  Serial.write(Serial2.read());
}

Full example

#include <Arduino.h>

#define UART_RX_PIN 2 // GPIO2
#define UART_TX_PIN 4 // GPIO4

void setup() {
  // Serial connects to the computer
  Serial.begin(115200);
  // Serial2 is the hardware UART port that connects to external circuitry
  Serial2.begin(115200, SERIAL_8N1,
                UART_RX_PIN,
                UART_TX_PIN);
}

void loop() {
  // Copy byte incoming via PC serial
  while (Serial.available() > 0) {
    Serial2.write(Serial.read());
  }
  // Copy bytes incoming via UART serial
  while (Serial2.available() > 0) {
    Serial.write(Serial2.read());
  }
}

Regarding platformio.ini, we just need to set the monitor_speed to match the value in Serial.begin(115200);:

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200

 

Posted by Uli Köhler in C/C++, Electronics, ESP8266/ESP32, PlatformIO

ESPAsyncWebserver: How to run code in main loop thread

When working with ESPAsyncWebserver, you will soon encounter one of its most fundamental limitations: Its code will always run in interrupts, hence you can’t use delay() or any function that uses delay() or similar functions internally.

However, there’s a simple programming pattern that will enable you to execute code in the main thread if requested by the webserver. Note: In this example, we will only cover how to activate code in the main loop from the webserver, but the response will be sent before the code in the main loop is run – hence, the response is just {"status": "ok"} and it doesn’t depend on whatever is happening on the main loop.

The idea is to have a global flag:

bool request_serial_output = false;

which is set in the webserver handler code, whereas the main loop checks the flag repeatedly:

void loop() {
  if(request_serial_output) {
    request_serial_output = false; // Clear flag
    // TODO Add handling code here
  }
}

Full example

This example uses PlatformIO and should work on any ESP32 board.

#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>

AsyncWebServer server(80);

bool request_serial_output = false;

void setup() { 
  Serial.begin(115200);
  // Connect Wifi, restart if not connecting
  // https://techoverflow.net/2021/01/21/how-to-fix-esp32-not-connecting-to-the-wifi-network/
  WiFi.begin("WLAN", "privat2014");
  uint32_t notConnectedCounter = 0;
  while (WiFi.status() != WL_CONNECTED) {
      delay(100);
      Serial.println("Wifi connecting...");
      notConnectedCounter++;
      if(notConnectedCounter > 50) { // Reset board if not connected after 5s
          Serial.println("Resetting due to Wifi not connecting...");
          ESP.restart();
      }
  }
  Serial.print("Wifi connected, IP address: ");
  Serial.println(WiFi.localIP());

  // Initialize webserver URLs
  server.on("/api/test", HTTP_GET, [](AsyncWebServerRequest *request) {
      // Send request to main loop
      request_serial_output = true;
      // Send JSON "ok" response.
      // NOTE: The response will be sent BEFORE the main loop has finished
      // processing the request
      AsyncResponseStream *response = request->beginResponseStream("application/json");
      DynamicJsonDocument json(1024);
      json["status"] = "ok";
      serializeJson(json, *response);
      request->send(response);
  });

  // Start webserver
  server.begin();
}

void loop() {
  if(request_serial_output) {
    request_serial_output = false; // Clear flag
    // Output serial
    Serial.println("First message. Now waiting one second...");
    // Wait 1s. You can't do that in the webserver handler!
    delay(1000);
    // Output another message on Serial
    Serial.println("Second message!");
  }
}
[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 115200
lib_deps =
    ESP Async [email protected]
    [email protected]

 

Posted by Uli Köhler in ESP8266/ESP32, PlatformIO

How to enter serial bootloader on the ESP32

In order to enter the serial bootloader on the ESP32, connect both GPIO0 and GPIO2 to GND while the ESP32 is starting up.

A typical hardware sequence to enter the bootloader is:

  1. Connect GPIO0 and GPIO2 to GND
  2. Connect the EN or RST pin to GND for at least 100ms, then connect to 3.3V

The second step will restart the ESP32.

Posted by Uli Köhler in ESP8266/ESP32

What is “Download Mode” on the ESP32?

The ESP-WROOM-32E datasheet Table 3 defines that when GPIO0 and GPIO2 are low during startup, you will enter Download mode without Download mode being defined anywhere in the datasheet:

Download mode is the factory-integrated ROM serial bootloader. By strapping GPIO0 and GPIO2 low at the same time, you can flash a new firmware using the serial port.

 

Posted by Uli Köhler in Embedded, ESP8266/ESP32

How to check a stepper motor for open/short circuit?

In order to correctly check a stepper motor for a hard short circuit, you need an LCR meter. If you only have an multimeter, skip the winding inductance check.

Measuring the phase resistance

First, check the resistance of each phase winding (pair A and pair B). The resistance should be between 0.5 Ω and 5 Ω, but note that it is also influenced by your multimeter probes and the contact resistance from the probe to the pin. If possible, compare the measurement to the datasheet of your stepper motor or to the datasheet of your motor or a good motor of the same model. If the datasheet lists 1.5 Ω, I would accept anything from 1.0 Ω to 2.0 Ω. If you see an infinite resistance (typically indicated by OL [overload] on your multimeter), either you measured the wrong pins or your probes do not electrically contact the pins – or your motor has an open winding (no contact). Re-measure multiple time, trying to contact the pin on different points on the surface to confirm. Also check if your multimeter displays a sensible value if you short the multimeter probes.
If you see a resistance that is way too low (e.g. 0.2 Ω for a nominal 1.5 Ω motor), your winding is shorted. This is typically not caused by a bad contact from the probes to the pin, but re-measure a few times just to confirm.

Now measure the resistance from one of the phase A pins to one of the phase B pins. The resistance should typically be OL (overload – i.e. a resistance so high the multimeter can’t measure it), but at least 5.0 MΩ. If it is any less, this indicates that the phases are shorted to each other. Be sure that you measured the correct pins.

Measuring the phase inductance

Now, Check the inductance of each phase winding (pair A and pair B). The inductance should be a couple of mH, but never below 100uH. A lower inductance indicates an winding that is shorted internally. If possible, compare with the datasheet of your stepper motor or see What phase inductivity value should my stepper motor have? for more details on this topic.

Now check, using the resistance mode of your LCR meter, check if any phase wiring has. Typically the resistance between one phase winding and the other

If you don’t know which pins of the connector belong to which phase winding, the two pins of each pair should have an inductance that is within ±30% of each another at maximum.

Posted by Uli Köhler in 3D printing, Electronics

How to fix Marlin random driver error detected coil short circuit crash (Trinamic TMC)

Problem:

When using Trinamic drivers with the Marlin 3D printer firmware, you might occasionally experience errors like

Send: N4193 G1 X12.345 Y34.567 E122.15252
Recv: 
Recv: E driver error detected: 0x10136000
Recv: coil short circuit
Recv: 		X	Y	Z	E
Recv: Enabled		true	true	true	true
Recv: Set current	2000	2000	2000	1000
Recv: RMS current	1999	1999	1999	990
Recv: MAX current	2819	2819	2819	1396
Recv: Run current	31/31	31/31	31/31	19/31
Recv: Hold current	15/31	15/31	15/31	9/31
Recv: Global scaler	167/256	167/256	167/256	133/256
Recv: CS actual	31/31	31/31	22/31	19/31
Recv: PWM scale	196716	33095745	65576	33488924
Recv: vsense
Recv: stealthChop	true	true	true	true
Recv: msteps		128	128	128	128
Recv: interp		true	true	true	true
Recv: tstep		171	678	max	1580
Recv: PWM thresh.	24	24	411	13
Recv: [mm/s]		102	102	3	41
Recv: OT prewarn	false	false	false	false
Recv: triggered
Recv:  OTP		false	false	false	false
Recv: off time	4	4	4	4
Recv: blank time	24	24	24	24
Recv: hysteresis
Recv:  -end		2	2	2	2
Recv:  -start		1	1	1	1
Recv: Stallguard thrs	0	0	0	0
Recv: uStep count	62	32	227	497
Recv: DRVSTATUS	X	Y	Z	E
Recv: sg_result	0	0	395	0
Recv: stallguard
Recv: fsactive
Recv: stst		*	*		*
Recv: olb
Recv: ola
Recv: s2gb					*
Recv: s2ga
Recv: otpw
Recv: ot
Recv: Driver registers:
Recv: 		X	0x00:1F:40:00
Recv: 		Y	0x00:1F:40:00
Recv: 		Z	0x80:16:41:8B
Recv: 		E	0x10:13:60:00
Recv: 
Recv: 
Recv: echo:Driver error
Recv: Error:Printer halted. kill() called!
Changing monitoring state from "Printing" to "Error"

Solution:

If the error always occurs at the start of each print, be sure to check your stepper motor setup for actual shorts! Measure the inducitivity using an LCR meter to be sure, it should be around the rated value for your motor! See this post for information about typical stepper motor inductivity.

If the error occurs occasionally, it is recommended in the datasheet that you use more conservative short detection settings.

You need to make the change this in Marlin/src/module/stepper/trinamic.cpp.

Go to the section for the driver type you are using. In my example, this would be

#if HAS_DRIVER(TMC5160)
// ...
void tmc_init(/* ... */) {
   // NOTE: tmc_init() is the function we need to insert code in!
}
#endif

Now insert, just before the TERN(HYBRID_THRESHOLD, /* ... */) line (if such a line does not exist, insert at the end of the function:

// Set more conservative short detection settings
st.shortfilter(3); // 3us SHORT filter
st.s2vs_level(15); // Lowest sensitivity short to supply voltage protection
st.s2g_level(15); // Lowest sensitivity short to ground protection
st.shortdelay(1); // 1.5us instead of 0.75us short delay

These are the most conservative settings possible using this setup.

Now retry your print.

In case this still doesn’t help, you can disable short protection entirely. Note that short protection exists to prevent damage to your stepper motor, your mainboard and your cabling, so you should only disable it if there is no other choice!

In order to disable short protection entirely, insert this code block instead of the conservative setting code block from above:

// Disable short protection entirely
st.diss2g(true);
st.diss2vs(true);

Tested with Marlin version 2.0.9.1

Posted by Uli Köhler in 3D printing, Embedded

What phase inductivity value should my stepper motor have?

Stepper motor phase inductivity varies widely. Even for a given size of stepper motor, such as NEMA17 with a length of 38mm, you can buy widely varying inductivity values even from the same manufacturer:

Both of these have the same holding torque of 80NcmInductivity is not an indicator of motor power!

As a general rule, remember:

  • NEMA17 steppers tend to be in the 1mH to 50mH range.
  • NEMA23 steppers tend to be in the 250μH to 25mH range

If you are checking your motor for a shorted winding, if the inductance is below 100μH, you can be pretty sure that the stepper motor is defective.

Posted by Uli Köhler in 3D printing, Electronics

Test code for 8 Neopixel/WS2812B LEDs

This test code toggles 8 WS2812b LEDs with circulating colors and will test all three R/G/B LEDs in each LED plus their connection.. It is based on an ESP32 with a 74LV245 level shifter, but it will also work on other platforms supported by NeoPixelBus. In my configuration, the 3.3V signal output pin is on pin 13.

#include <NeoPixelBus.h>
#include <NeoPixelAnimator.h>

const uint16_t PixelCount = 8;
const uint8_t PixelPin = 13;

NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);

void DrawPixels(uint32_t offset)
{
    strip.SetPixelColor((0 + offset) % 8, RgbColor(255, 0, 0));
    strip.SetPixelColor((1 + offset) % 8, RgbColor(0, 255, 0));
    strip.SetPixelColor((2 + offset) % 8, RgbColor(0, 0, 255));
    strip.SetPixelColor((3 + offset) % 8, RgbColor(255, 255, 0));
    strip.SetPixelColor((4 + offset) % 8, RgbColor(0, 255, 255));
    strip.SetPixelColor((5 + offset) % 8, RgbColor(255, 0, 255));
    strip.SetPixelColor((6 + offset) % 8, RgbColor(255, 255, 255));
    strip.SetPixelColor((7 + offset) % 8, RgbColor(0, 0, 0));
    strip.Show();
}

void setup()
{
    strip.Begin();
    strip.Show();
}

uint32_t loopCounter = 0;
void loop()
{
  DrawPixels(loopCounter);
  loopCounter++;
  delay(250);
}

PlatformIO config:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps =
     makuna/NeoPixelBus @ ^2.6.9

 

Posted by Uli Köhler in Electronics, ESP8266/ESP32, PlatformIO

How I fixed PlatformIO Arduino

Problem:

When uploading an Atmel AVR firmware like for the Arduino Uno using PlatformIO, you see an error message like

Auto-detected: /dev/ttyACM0
Uploading .pio/build/uno/firmware.hex
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

avrdude done.  Thank you.

*** [upload] Error 1

or

Auto-detected: /dev/ttyUSB0
Uploading .pio/build/uno/firmware.hex
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

avrdude done.  Thank you.

*** [upload] Error 1

Solution

This issue occurs if:

  • Either there is no bootloader on the board or the bootloader is damaged or
  • The wrong upload speed for this bootloader is selected

Note that  you can flash a new/updated bootloader using the Arduino IDE, overwriting the old / defective bootloader. You can do that using an AVR programmer, or you can do it using another Arduino. This will make working with the board much easier in general, but it will most likely fix your programming issues. In case you flash a new bootloader, the correct upload_speed setting in platformio.ini will typically be upload_speed = 115200.

In case you want to work with the bootloader currently present on the board, you need to select the correct upload_speed setting.

Depending on the age of the bootloader on the board, it will typically have either 115200, 57600 or 19200 baud speed.

In order to fix the issue, edit platformio.ini and add

upload_speed = 115200

then restart the upload. In case this doesn’t work, try

upload_speed = 57600

and

upload_speed = 19200

Rarely, the following setting work:

upload_speed = 9600

or

upload_speed = 230400

In case none of these settings work at all, either you are facing a hardware defect or you need to flash a new bootloader onto the board. I recommend to always try to flash a bootloader first before throwing away the board.

Posted by Uli Köhler in Arduino, Electronics, Embedded, PlatformIO

How fast is analogRead() on the ESP8266?

An analogRead() call on the ESP8266 takes about 90-100 microseconds.

I tested this experimentally using a Wemos D1 Mini board, PlatformIO and this code:

uint32_t t0 = micros();
for (size_t i = 0; i < 1000; i++)
{
    analogRead(A0);
}
uint32_t t1 = micros();
Serial.print("1000 analogRead() calls took [us]: ");
Serial.println(t1-t0);

Output:

1000 analogRead() calls took [us]: 97168

Since 1000 analogRead() calls (including some overhead from the loop) took 97.168ms, one analogRead() call takes about 97.168us (+-0.02us).

Posted by Uli Köhler in Arduino, Electronics, ESP8266/ESP32, PlatformIO

What is the DALI Bus Baud Rate?

DALI operates at 1200 baud i.e. 1200 bits per second.

This means transmitting 1 bit takes 1/1200 seconds = 0.833ms

Posted by Uli Köhler in Electronics

How to enable Watchdog on ESP8266/ESP32 using PlatformIO

You can enable the watchdog on the ESP8266 or ESP32 using

ESP.wdtEnable(5000);

where 5000 is the number of milliseconds until the watchdog times out.

Call

ESP.wdgFeed();

periodically to reset the watchdog.

Posted by Uli Köhler in ESP8266/ESP32, PlatformIO

What does ESP8266 rst cause: 2 mean?

Sometimes you will see a message like

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v0007e100
~ld
7

on the ESP8266 serial line.

rst cause: 2 means that the ESP was restarted from the firmware using

ESP.restart();

Typically such a restart is intentional. Look for ESP.restart() calls in your firmware. It’s not straightforward to identify which ESP.restart() call caused the reset. I recommend to insert Serial.println() statements describing the reset cause before every call to ESP.restart(), for example:

Serial.println("Resetting due to Wifi not connecting...");
ESP.restart();

 

Posted by Uli Köhler in Electronics, ESP8266/ESP32, PlatformIO

How to fix ESP8266 PlatformIO error: WIFI_STA was not declared in this scope

Problem:

When compiling your PlatformIO firmware, you see an error message like

src/main.cpp: In function 'void setup()':
src/main.cpp:49:10: error: 'class WiFiClass' has no member named 'mode'
   49 |     WiFi.mode(WIFI_STA);
      |          ^~~~
src/main.cpp:49:15: error: 'WIFI_STA' was not declared in this scope
   49 |     WiFi.mode(WIFI_STA);
      |               ^~~~~~~~

Solution:

You included WiFi.h instead of the correct ESP8266WiFi.h. This will cause multiple issues like reboots on WiFi.begin() even if it compiles correctly.

Replace

#include <WiFi.h>

with

#include <ESP8266WiFi.h>

everywhere in your source code and does not support

WiFi.mode(WIFI_STA);

For more details, see our previous post on How to fix PlatformIO ESP8266 WiFi.h: No Such File or Directory

Posted by Uli Köhler in Electronics, ESP8266/ESP32, PlatformIO

How I fixed ESP8266 WiFi.begin() rst cause 4 on PlatformIO

I faced an issue with my PlatformIO ESP8266 firmware causing a rst cause 4 reset a few seconds after calling WiFi.begin():

ets Jan  8 2013,rst cause:4, boot mode:(3,2)

wdt reset
load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v0007e660
~ld
7

and a subsequent reboot of the firmware. The issue repeated ad infinitum.

In my case, the issue was that I had listed WiFi as lib_deps dependency in platformio.ini:

lib_deps =
    WiFi

instead of including #include <ESP8266WiFi.h> instead of #include <WiFi.h>. See How to fix PlatformIO ESP8266 WiFi.h: No Such File or Directory for more details.

Posted by Uli Köhler in Electronics, ESP8266/ESP32, PlatformIO

How to install ArduinoOTA for ESP8266/ESP32 on PlatformIO (platformio.ini and lib_deps)

As the ESP Arduino framework already includes ArduinoOTA, there is no need to explicitly install or require ArduinoOTA.

You do not need to add anything to platformio.ini or lib_deps in order to use ArduinoOTA on the ESP8266 or ESP32.

In order to use ArduinoOTA, the minimum you need to to is to include it:

#include <ArduinoOTA.h>

then call

ArduinoOTA.begin();

in your setup() function and then call

ArduinoOTA.handle();

periodically (for example, in your loop() function).

For a more complete example, see:

Posted by Uli Köhler in ESP8266/ESP32, PlatformIO