-->
Page 1 of 1

Calling pinMode causes reset / hex dump - help needed.

PostPosted: Sun May 15, 2016 10:14 am
by bgowland
I'm using a Witty / GizWits module and just started with Arduino. I'm trying to read temp/humidity with a DHT22 sensor. I've got working circuitry on a breadboard which is fine with NodeMCU/Lua code and I'm trying to convert over to using Arduino.

Using the following code, when I try to set a GPIO to be input to read the DHT, it resets / reboots the ESP module (I'm using Serial Monitor to watch it) and I also get a hex dump...

Code: Select allvoid getDhtSensorData() {
  Serial.println("\ngetDhtSensorData() entered...");
  pinMode(DHT_DATA_PIN, INPUT);
  /*
  Other code removed for clarity - the above line causes the reboot
  */
  Serial.println("Exiting getDhtSensorData()...");
}

void getSensorData() {
  Serial.println("getSensorData() entered...");
  getDhtSensorData();
//  getBmpSensorData();
  Serial.println("Exiting getSensorData()...");
}

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(5000);
  Serial.begin(115200);
  Serial.println("\n\nStarting loop()...");
  Serial.println("Calling getSensorData()...");
  getSensorData();
}



The first time I upload / run the code I get the following and the code loop just stops...

Code: Select all0x0f
csum 0x0f
~ld


Starting loop()...
Calling getSensorData()...
getSensorData() entered...

getDhtSensorData() entered...
Exiting getDhtSensorData()...
Exiting getSensorData()...

 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset


If I then press the reset button on the GizWits daughter board it goes into a proper repeating loop but each time I get the same as above as well as a hex dump.

If I remove the line...
Code: Select all  pinMode(DHT_DATA_PIN, INPUT);
...altogether then I get a proper loop without errors or hex dump.

I've even disconnected the DHT circuitry completely but just using pinMode seems to be causing problems and I don't understand why.

Cheers,
Brian

Re: Calling pinMode causes reset / hex dump - help needed.

PostPosted: Sun May 15, 2016 10:38 am
by martinayotte
I doubt that pinMode() is the cause.
You don't show the code in the place of "Other code removed for clarity - the above line causes the reboot".
To prove that, simply comment this whole "hidden" code and leave the pinMode() uncommented, I'm pretty sure that it won't crash, so, something there is the source of the problem.
Also you should provide a link to the library you use, maybe it is not ported for ESP, there maybe a ported version elsewhere.

Re: Calling pinMode causes reset / hex dump - help needed.

PostPosted: Mon May 16, 2016 12:40 pm
by IOT@urremote.com
There is a clue in
bgowland wrote:
ets Jan 8 2013,rst cause:4, boot mode:(1,6)

wdt reset

rst cause:4 and wdt reset means watch dog timer reset. So somewhere your code is taking too long before feeding the watch dog timer. Oddly you get "Exiting getSensorData()..." printed and the next instruction run is delay(5000) which should allow the required watch dog timer feed.
bgowland wrote:If I remove the line...
Code: Select all  pinMode(DHT_DATA_PIN, INPUT);
...altogether then I get a proper loop without errors or hex dump.

Somehow the watchdog timer is getting a feed.
bgowland wrote:I've even disconnected the DHT circuitry completely but just using pinMode seems to be causing problems and I don't understand why.

You don't say which pin is defined as DHT_DATA_PIN, INPUT. Boot mode:(1,6) is why it stops. The first number should be 3 for the device to reboot into your code which from the following table requires GPIO0 High and GPIO2 High.

MTDO GPIO0 GPIO2 Mode Description
L L H UART Download code from UART
L H H Flash Boot from SPI Flash
See https://github.com/esp8266/esp8266-wiki ... boot-modes

When your code is looping and crashing you will probably be seeing Boot mode:(3,6) rather than Boot mode:(1,6).

Given the oddity mentioned above I doubt this is the full explanation but hopefully it provides further avenues for investigation.

Re: Calling pinMode causes reset / hex dump - help needed.

PostPosted: Mon Jan 03, 2022 2:23 pm
by nohspamjose
I'm having the same problem (NodeMCU ESP8266). I successfully run an SHT30 humi/temp sensor from D1 D2 (+Vcc & Gnd) but I'm trying to add a button (D7 + Gnd + 30kΩ between D7&3.3V) and a red LED (D5) & green LED (D6) with common ground The following code works until I uncomment one or more of the pinMode statements at the start of void setup() {


Code: Select all#include "Wire.h" //I2C library
#include "SHT31.h" //I2C sensor library
#include <ESP8266WiFi.h>
#include <Ticker.h> // non-blocking delay library
#include <AsyncMqttClient.h>

#define WIFI_SSID "********"
#define WIFI_PASSWORD "***********"

// Raspberri Pi Mosquitto MQTT Broker
#define MQTT_HOST IPAddress(192, 168, ***, ***)
#define MQTT_PORT 1883
String hostname = "BathroomHumidTemp";

// Temperature & humidity MQTT Topics
#define MQTT_PUB_TEMP "esp/dht/b/temp"
#define MQTT_PUB_HUM "esp/dht/b/humid"

// I2C & DHT
uint32_t start;
uint32_t stop;
// Initialize DHT sensor
SHT31 sht;

// pin assignments:
const int BUTTON_PIN = 7;  // the number of the pushbutton pin
const int RED_LED_PIN =  5;   // the number of the RED LED pin
const int GREEN_LED_PIN =  6;   // the number of the GREEN LED pin

int buttonState = 0;   // variable for reading the pushbutton status

// Variables to hold sensor readings
float temp;
float hum;

AsyncMqttClient mqttClient;
Ticker mqttReconnectTimer;

WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
Ticker wifiReconnectTimer;

unsigned long previousMillis = 0;   // Stores last time temperature was published
const long interval = 10000;        // Interval at which to publish sensor readings

void connectToWifi() {
  Serial.println("Connecting to Wi-Fi...");
  WiFi.setHostname(hostname.c_str());
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void onWifiConnect(const WiFiEventStationModeGotIP& event) {
  Serial.println("Connected to Wi-Fi.");
  connectToMqtt();
}

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
  Serial.println("Disconnected from Wi-Fi.");
  mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
  wifiReconnectTimer.once(2, connectToWifi);
}

void connectToMqtt() {
  Serial.println("Connecting to MQTT...");
  mqttClient.connect();
}

void onMqttConnect(bool sessionPresent) {
  Serial.println("Connected to MQTT.");
  Serial.print("Session present: ");
  Serial.println(sessionPresent);
}

void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  Serial.println("Disconnected from MQTT.");

  if (WiFi.isConnected()) {
    mqttReconnectTimer.once(2, connectToMqtt);
  }
}

void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
  Serial.println("Subscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
  Serial.print("  qos: ");
  Serial.println(qos);
}

void onMqttUnsubscribe(uint16_t packetId) {
  Serial.println("Unsubscribe acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

void onMqttPublish(uint16_t packetId) {
  Serial.print("Publish acknowledged.");
  Serial.print("  packetId: ");
  Serial.println(packetId);
}

void setup() {
//  // initialize the LED pins as an output:
//  pinMode(RED_LED_PIN, OUTPUT);
//  pinMode(GREEN_LED_PIN, OUTPUT);
//  // initialize the pushbutton pin as an pull-up input:
//  // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
//  pinMode(BUTTON_PIN, INPUT_PULLUP);
 
  Serial.begin(115200);
  Serial.println();
  Wire.begin();
  sht.begin(0x44);    //Sensor I2C Address
  Wire.setClock(100000);
  uint16_t stat = sht.readStatus();
  Serial.print(stat, HEX);
  Serial.println();
 
  wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
  wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);

  mqttClient.onConnect(onMqttConnect);
  mqttClient.onDisconnect(onMqttDisconnect);
  //mqttClient.onSubscribe(onMqttSubscribe);
  //mqttClient.onUnsubscribe(onMqttUnsubscribe);
  mqttClient.onPublish(onMqttPublish);
  mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  // If your broker requires authentication (username and password), set them below
  //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD");
 
  connectToWifi();
}

void loop() {
//  // read the state of the pushbutton value:
//  buttonState = digitalRead(BUTTON_PIN);
//  // control LED according to the state of button
//  if(buttonState == HIGH)         // If button is pressing
//    digitalWrite(RED_LED_PIN, HIGH); // turn on LED
//  else                           // otherwise, button is not pressing
//    digitalWrite(RED_LED_PIN, LOW);  // turn off LED
   
  unsigned long currentMillis = millis();
  // Every X number of seconds (interval = 10 seconds)
  // it publishes a new MQTT message
  if (currentMillis - previousMillis >= interval) {
    // Save the last time a new reading was published
    previousMillis = currentMillis;

    sht.read();
    // New SHT sensor readings
    hum = sht.getHumidity();
    // Read temperature as Celsius (the default)
    temp = sht.getTemperature();
   
    // Publish an MQTT message on topic esp/dht/b/temp
    uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());                           
    Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1);
    Serial.printf("Message: %.2f \n", temp);

    // Publish an MQTT message on topic esp/dht/b/humid
    uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());                           
    Serial.printf("Publishing on topic %s at QoS 1, packetId %i: ", MQTT_PUB_HUM, packetIdPub2);
    Serial.printf("Message: %.2f \n", hum);
  }
}