PID Control Object (with questions)

Posted on
Page
of 2
Prev
/ 2
  • Hello @Wilberforce,

    Yes, I am using esp8266 (wemos d1 mini).
    I have put the program on "save on send" option.
    The script should be small in size as follows

    I2C1.setup({scl:D5,sda:D4, bitrate:100000});
    var PID = require('pid-controller');
    var bme = require("BME280").connect(I2C1);
    var WIFI_NAME = "xxx";
    var WIFI_OPTIONS = { password : "xxx" };
    var MQTT_HOST = "192.168.0.156";
    var PATH = "/mydevice/";
    var LED = D13;
    var RELAY = D12;
    var BTN = D0;
    var mqtt;
    var wifi;
    var hum = 20,
        humSetpoint = 55;
    
    var Kp = 100,
        Ki = 0,
        Kd = 0;
    
    var ctr;
    var timeframe = 30000;
    var dacMin = 100;
    var dacMax = 3000;
    
    wifi = require("Wifi");
    var nodeID = wifi.getIP().mac;
    nodeID = nodeID.replace(/:/g, "");
    nodeID = nodeID.toUpperCase();
    
    function updatePid() {
      hum = bme.getData().humidity;
      hum = hum.toFixed(1);
      hum = E.clip(hum, 10, 100);
      ctr.setInput(hum);
      if (ctr.compute()) {
        var dataDac = ctr.getOutput();
        dataDac = E.clip(dataDac, dacMin, dacMax);
        I2C1.writeTo(0x60, [dataDac >> 8, dataDac]);
        setTimeout(function() {
          var adc = analogRead().toFixed(3);
          var bufText = "{\"H\":"+hum+",\"DAC\":"+dataDac+",\"AD­C\":"+adc+"}";
          console.log(bufText);
          mqtt.publish(PATH+"status",bufText);
        }, 3000);
      }
    }
    
    function setState(v) {
      RELAY.write(v);
      LED.write(!v);
      mqtt.publish(PATH+"status", v?1:0);
    }
    
    function mqttMessage(pub) {
      console.log("MQTT=> ",pub.topic,pub.message);
    
      if (pub.topic == PATH+"set") {
        setState(pub.message!=0);
      }
      if (pub.topic == PATH+"eval") {
        try {
          mqtt.publish(PATH+"response", eval(pub.message));
        } catch(e) {
          mqtt.publish(PATH+"exception", e.toString());
        }
      }
    }
    
    function mqttConnect() {
      mqtt = require("MQTT").connect({
        host: MQTT_HOST,
        client_id : nodeID,
      });
      mqtt.on('connected', function() {
        console.log("MQTT connected");
        setTimeout(function() {      
          mqtt.subscribe(PATH+"#");
        }, 1000);
      });
      mqtt.on('publish', mqttMessage);
    }
    
    
    function onInit() {
      ctr = new PID(hum, humSetpoint, Kp, Ki, Kd, 'direct');
      ctr.setSampleTime(timeframe);
      ctr.setOutputLimits(dacMin, dacMax);
      ctr.setMode('auto');
      digitalWrite(D16,0);
      console.log("Connecting WiFi");
      setInterval(updatePid, timeframe);
      setInterval(function() {
        if (!mqtt) return;
        if (!mqtt.connected) {
          console.log("MQTT disconnected... reconnecting.");
          mqtt.connect();
        }
        digitalWrite(D16, 1);
        setTimeout( function d16() {
          digitalWrite(D16,0);
        }, 300);
      }, 60*1000);
    
      wifi = require("Wifi");
      wifi.on('connected',function() {
        console.log("Connected to WiFi");  
      });
      wifi.on('disconnected',function() {
        console.log("Disconnected from WiFi");  
      });
      wifi.setHostname("MYDEVICE");
      wifi.stopAP();
      wifi.connect(WIFI_NAME, WIFI_OPTIONS,
                   function(ap){ 
        console.log("Successful connect.");
      });
      // wait, and connect MQTT
      setTimeout(function() {
        console.log("MQTT connecting");
        mqttConnect();
      }, 10000);
    }
    

    and bellow is the error message

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     1v99 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:1024/1024, manuf 0xc8 chip 0x4016
    >
    =undefined
    Running onInit()...
    Connecting WiFi
    Successful connect.
    Connected to WiFi
    MQTT connecting
    ERROR: Error processing Serial data handler - removing it.
    Execution Interrupted during event processing.
    New interpreter error: CALLBACK,LOW_MEMORY,MEMORY
    > 
    
  • You could try calling reset(true) on the left-hand side before upload just in case any data was left in memory from previous saves.

    The issue is not your code itself, but that you're trying to cram MQTT, BME680 and PID into an ESP8266.

    You could use something like an Espruino WiFi that has more than enough space for this, however if you're after some ideas to save space you could ask on the ESP8266 section of the forum rather than hijacking this thread. I believe there is a smaller MQTT module available.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

PID Control Object (with questions)

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions