OLED , DS18b20 (temperature sensor) and the interrupt all together with esp8266

Thread Starter

Tarek1266

Joined Oct 18, 2019
55
Hi everyone,

Hope you are fine. I am currently working on a project the uses esp8266 as a MCU (I am using Nodemcu module). the nodemcu is connected to OLED and DS18B20 temperature sensor. the code works fine while I am working with only those device. When i use an interruption routine in the code the hole code does not work and the OLED does not display anything.


CODE:
#define ICACHE_RAM_ATTR

// OLED LIBRARIES
//#include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
#include<SH1106.h>
#include "images.h"

// TEMPERTURE SENSOR LIBRARIES
#include <OneWire.h>
#include <DallasTemperature.h>

// PID LIBRARY
#include <PID_v1.h>

// GPIO where the DS18B20 is connected to
const int oneWireBus = 12;

// OLED PINS ASSIGNMENTS
SH1106 display(0x3c, SDA, SCL);   // ADDRESS, SDA, SCL
  


//Define Variables for PID controller
double Setpoint, Input, Output;
  //Specify the links and initial tuning parameters
  double Kp=510.6, Ki=1.164, Kd=4998;
  PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

// DEFINING LATCHING ANGLE AND INTERUPTER
int detectado =0;
int valor=0;
uint8_t INTERUPT_PIN = 14;
int TRIAC = 13;
float temperatureC = 0.0;
// INTERUPT FUNCTION DECLARATION
void ICACHE_RAM_ATTR IntCallback();



// VARIABLES TO DISCRITIZE THE TEMPERTURE READING
int ct=0;
int lt=0;
bool r=0;

// VARIABLES TO DISCRITIZE THE PID CONTROLLER
int pid_ct=0;
int pid_lt=0;

void setup() {
  // Start the Serial Monitor
  Serial.begin(9600);
  // Start the DS18B20 sensor
  sensors.begin();

  // OLED PART
  display.init();
  display.setI2cAutoInit(true);

  // SETTING INTRUPT PIN
  attachInterrupt(digitalPinToInterrupt(INTERUPT_PIN), IntCallback,  FALLING);
   //Define 13 as output for the TRIAC pulse
  pinMode(TRIAC,OUTPUT);       

 // SETPOINT DECLARATION
 Setpoint = 30 ;

}

void loop() {
  // LIMINTING THE LATCHING ANGLE
  myPID.SetMode(AUTOMATIC);
  myPID.SetOutputLimits(100,8800);


 
  // READING TEMPERTURE (DISCRITIZED)
  ct=millis();
  if(ct-lt>=2000){
      lt=ct; 
    if(r){sensors.requestTemperatures();}
    else{temperatureC = sensors.getTempCByIndex(0);}
      r=!r;
  }


  // PID ACTION DISCRITIZED
    // LINEAR CONTROLLER 
    pid_ct = millis();
    if(pid_ct-pid_lt>=500){
        pid_lt=pid_ct;
        Input = temperatureC;
        myPID.Compute();
        valor= 8900-Output;
        }
    // NON LINEAR CONTROLLER 
    if(Setpoint-temperatureC>2.5){valor=100;}
    else if (temperatureC - Setpoint > 0.5 ) {valor = 8900;}
    
 
  // CONTROL ACTION
      if (detectado)
    {     
      delayMicroseconds(valor);
      digitalWrite(TRIAC,HIGH);
      delayMicroseconds(100);
      digitalWrite(TRIAC,LOW);
      detectado=0;
      }
 

//   DISPLAYING READING
  display.invertDisplay();
  display.flipScreenVertically();
  display.drawString(0, 0, "Setpoint : " + String(Setpoint)+ " ºC" );
  display.drawString(0, 50, "Temperture : " + String(temperatureC)+ " ºC" );
  display.display();
  display.clear();


  // SHOWING THE TEMPERTURE AND LATCHING ANGLE IN THE SERIAL FOR PROTOTYPING
  Serial.print(temperatureC);
  Serial.print("\t");
  Serial.println(valor);

 
}


//INTERRUPTION FUNCTION
void IntCallback()
{
    detectado=1;                                     
}

This code works fine with arduino. However, this does not work for some reason in esp8266.
Note : after some debugging the code was noticed to stop before the interrupt statement in the void setup ().
 
Top