Avatar for maman

maman

Member since Aug 2018 • Last active Oct 2018
  • 1 conversations
  • 4 comments

Most recent activity

  • in Projects
    Avatar for maman

    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
    > 
    
  • in Projects
    Avatar for maman

    Hello @ClearMemory041063, @Wilberforce

    I am using pid-controller and mqtt modules for our project, now.
    We use Wemos D1 Mini.
    I am trying the example simulation of the github @Wilberforce, it is working, normally.
    However, if we combine the pid-controller and mqtt modules, when we
    try to connect to mqtt broker, there is an error.

    Execution Interrupted during event processing.
    New interpreter error: LOW_MEMORY,MEMORY
    

    Is there any issue for compatibility of pid-controller and mqtt module ?

    Regards,
    Maman

  • in ESP8266
    Avatar for maman

    @hungryforcodes, @allObjects, thanks for the reply. I use ESP-07 from Ai. Now, I am developing lamp controller that can be remotely controlled by MQTT protocol. However, I want to use the previously mechanical switch to control the lamp, locally, in case the network failed. I connected directly the mechanical switch to pin D5 of esp8266 tied-up with 10K resistor to VCC. It seems that the problem is the mechanical switch. I have tested the code below :

    print("Starting watch set test\n");
    var testPin = D13;
    pinMode(testPin, "input");
    var watchId = setWatch(function() {
      print("The callback function was called for a pin I/O change\n");
    }, testPin, { repeat: true} );
    print("A new watch was set up with id=" + watchId + "\n");
    

    And the result is there are many callback function was called, and the ESp8266 was crashed.

    then I put a small capacitor on the switch terminal, and add debounce at 100, then it is run well.
    Thanks for your attention.

  • in ESP8266
    Avatar for maman

    I use D5 and D4 of ESP8266 to detect pin status using setWatch.

    setWatch(function updateDIN1() {
    // ...
    }, D5, {repeat:true, edge:'both', debounce:100});

    If I connect D5 to normally GND, then the ESP8266 will detect many interrupt, and
    ESP8266 can not continue to run the other script program.
    However, If I connect D5 to VCC via pull-up resistor, then when I change the D5 to GND, then
    I will get many interrupt, and ESP8266 to busy accepting interrupt from the setWatch..

    Is there anyone have this kind of problem ?

    Regards,
    Maman

Actions