Arduino UNO R4 WiFi, Gravity I2C 4-20mA DAC, Mass Flow Controller

I’m trying make a open source system to operate a mass-flow-controller (MFC) over the web. The MFC is operated with a linear current 4-20mA, with 4mA-ValveClosed and 20mA-ValveOpen. I’m using an arduino uno r4 wifi board and an Gravity IC2 4-20mA DAC breakoutboard purchased from dfrobot (https://www.dfrobot.com/product-2625.html). I want my arduino to standby on an dedicated IP address and listen for incoming clients and accordingly control the MFC. For example, when a client sents 10.0.0.35/15, the arduino sets the Gravity I2C DAC to a setpont of 15mA which eventually cotrolles the MFC. I’ve written the code bellow, but the problem is that the arduino-DAC doesn’t hold the setpoint and keeps reseting when the connection is closed!

    #include "DFRobot_GP8302.h"
    DFRobot_GP8302 module;
    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <WiFiServer.h>
    #include "RTC.h"

    const char* ssid     = "abc123";
    const char* password = "abc";
    volatile int set;
    const int mfc_flow_pin = A2;
    WiFiServer server(80); // Create a webserver on port 80

    int mfc_flow_pin_value = 0;

    void setup(){
      Serial.begin(115200);
      RTC.begin();
      RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
      RTC.setTime(startTime);
      while(!Serial){

      }
      Serial.print("I2C to 0-25 mA analog current moudle initialization ... ");
      uint8_t status = module.begin(); // use the pin used by the I2C Wire object of the MCU hardware by default

      if(status != 0){
        Serial.print("failed. Error code: ");
        Serial.println(status);
        Serial.println("Error Code: ");
        Serial.println("t1: _scl or _sda pin is invaild.");
        Serial.println("t2: Device not found, please check if the device is connected.");
        while(1) yield();
      }
      Serial.println("done!");
      uint16_t dac = module.output(set); //control the converter module to output a current of 10mA and return the corresponding DAC value
      Serial.print("DAC value: 0x"); 
      Serial.println(dac, HEX);
      module.store(); 
      Serial.println("Save current configuration."); //the above current config will be saved and will not be lost after power off
      // Attempt to connect to WiFi
      Serial.print("Connecting to ");
      Serial.println(ssid); 
      WiFi.begin(ssid, password); 
      while (WiFi.status() != WL_CONNECTED) 
      {
        delay(500);
        Serial.print(".");
      }
      // Connected to WiFi
      Serial.println("");
      Serial.println("WiFi connected.");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
      // Start the server
      server.begin();
    }

    void setOutput(uint16_t value) {
      //Serial.print("I2C to 0-25 mA analog current moudle initialization ... ");
      uint8_t status = module.begin();
      if(status != 0){
        //Serial.print("failed. Error code: ");
        //Serial.println(status);
        //Serial.println("Error Code: ");
        //Serial.println("t1: _scl or _sda pin is invaild.");
        //Serial.println("t2: Device not found, please check if the device is connected.");
        while(1) yield();
      }
      //Serial.println("done!");

      uint16_t dac = module.output(value);
      //Serial.print("DAC value: 0x"); 
      //Serial.println(dac, HEX);
      module.store(); 
      //Serial.println("Save current configuration.");
      set = value;
      delay(100);
    }

    void loop(){
      RTCTime currentTime;
      RTC.getTime(currentTime);
      mfc_flow_pin_value = map(analogRead(mfc_flow_pin), 0, 1023, 4, 20);

      WiFiClient client = server.available();   // listen for incoming clients
      if (client) {                             // if you get a client,
        String currentLine = "";                // make a String to hold incoming data from the client
        while (client.connected()) {            // loop while the client's connected
          if (client.available()) {             // if there's bytes to read from the client,
            char c = client.read();             // read a byte, then
            if (c == 'n') {                    // if the byte is a newline character

              // if the current line is blank, you got two newline characters in a row.
              // that's the end of the client HTTP request, so send a response:
              if (currentLine.length() == 0) {
                // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
                // and a content-type so the client knows what's coming, then a blank line:
                client.println("HTTP/1.1 200 OK");
                client.println("Content-type:text/html");
                client.println();
                
                // the content of the HTTP response follows the header:
                int ts = currentTime.getUnixTime();
                client.print("Unix Time: ");
                client.println(ts);
                client.print("Flow [4-20mA] = ");
                client.println(mfc_flow_pin_value);
                client.print("Setpoint [4-20mA] = ");
                client.println(set);

                // The HTTP response ends with another blank line:
                client.println();
                // break out of the while loop:
                break;
              } else {    // if you got a newline, then clear currentLine:
                currentLine = "";
              }
            } else if (c != 'r') {  // if you got anything else but a carriage return character,
              currentLine += c;      // add it to the end of the currentLine
              // Check if the current line starts with "GET /"
              if (currentLine.startsWith("GET /")) {
                // Extract the integer from the request
                int value = currentLine.substring(5).toInt();
                // Call setOutput with the extracted value
                if (value > 20) {
                  value = 20;
                }
                if (value < 4) {
                  value = 4;
                }
                setOutput(value);
                // the content of the HTTP response follows the header:
                int ts = currentTime.getUnixTime();
                client.print("THIS IS ---> Unix Time: ");
                client.println(ts);
                mfc_flow_pin_value = map(analogRead(mfc_flow_pin), 0, 1023, 4, 20);
                delay(100);
                client.print("Flow [4-20mA] = ");
                client.println(mfc_flow_pin_value);
                client.print("Setpoint [4-20mA] = ");
                client.println(set);
              }
            }
          }
        }
        // close the connection:
        client.stop();
      }
      setOutput(set);
    }

I’ve tryed to call the setOutput(set) helper function in different positions but with no avail.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật