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