Skip to content

Commit a5a25ab

Browse files
committed
Opta code completed
1 parent 603ba21 commit a5a25ab

File tree

1 file changed

+191
-0
lines changed
  • content/hardware/07.opta/opta-family/opta/tutorials/18.smart-compressor-app-note

1 file changed

+191
-0
lines changed

content/hardware/07.opta/opta-family/opta/tutorials/18.smart-compressor-app-note/content.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,197 @@ In the `setup()` function the different board peripherals are initiated includin
184184
- LEDs and relay outputs
185185
- ADC configuration
186186
- Arduino Cloud properties
187+
- Ethernet interface
188+
189+
```arduino
190+
void setup() {
191+
// put your setup code here, to run once:
192+
Serial.begin(115200);
193+
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
194+
delay(1500);
195+
196+
pinMode(RELAY1, OUTPUT);
197+
pinMode(LED_RELAY1, OUTPUT);
198+
pinMode(LEDB, OUTPUT);
199+
pinMode(LEDG, OUTPUT);
200+
201+
// Set the resolution of the ADC to 12 bits.
202+
analogReadResolution(12);
203+
204+
// Defined in thingProperties.h
205+
initProperties();
206+
207+
// Connect to Arduino IoT Cloud
208+
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
209+
210+
/*
211+
The following function allows you to obtain more information
212+
related to the state of network and IoT Cloud connection and errors
213+
the higher number the more granular information you’ll get.
214+
The default is 0 (only errors).
215+
Maximum is 4
216+
*/
217+
setDebugMessageLevel(2);
218+
ArduinoCloud.printDebugInfo();
219+
220+
// Init Ethernet connection for Modbus TCP
221+
EthernetInit();
222+
}
223+
```
224+
In the `loop()` function, the WiFi and BLE connections are verified continuosly alongside the sensor readings.
225+
226+
```arduino
227+
void loop() {
228+
229+
if (WiFi.status() == WL_CONNECTED) { // If WiFi is successfully connected
230+
digitalWrite(LEDG, HIGH); // Opta green LED ON
231+
232+
if (control_once) { // Once WiFi is ready, init Bluetooth LE once
233+
Serial.println("BLE Init");
234+
235+
if (!BLE.begin()) {
236+
Serial.println("Starting BLE failed!");
237+
while (1) {
238+
}
239+
}
240+
241+
BLE.scan();
242+
Serial.println("Scanning for peripherals.");
243+
control_once = 0;
244+
}
245+
246+
// check if a peripheral has been discovered
247+
BLEDevice peripheral = BLE.available();
248+
249+
if (peripheral) {
250+
// Check if the peripheral is called Nicla:
251+
if (peripheral.localName() != "Nicla") {
252+
return;
253+
}
254+
Serial.print("Found ");
255+
Serial.println(peripheral.localName());
256+
257+
BLE.stopScan();
258+
// Nicla Sense ME node connection handler
259+
NiclaVibrationHandler(peripheral);
260+
261+
BLE.scan();
262+
}
263+
264+
// If there is not BLE connection this will manage the sensors reading
265+
unsigned long currentMillis = millis();
266+
267+
if (currentMillis - previousMillis >= interval) {
268+
269+
previousMillis = currentMillis;
270+
271+
Serial.println("Not BLE Loop");
272+
273+
BLE.scan(); // continue scanning for new BLE connection
274+
SensorRead(); // ADC sensor reading and convertions
275+
}
276+
277+
ArduinoCloud.update(); // to maintain Cloud connectivity
278+
279+
} else { // If WiFi is not connected
280+
281+
digitalWrite(LEDG, LOW); // Opta green LED OFF
282+
control_once = 1;
283+
284+
ArduinoCloud.update(); // to maintain Cloud connectivity
285+
}
286+
}
287+
```
288+
289+
The function `SensorRead()` reads the sensor data, as they are connected to the Opta analog inputs we can easily read them by sampling their respective ADC inputs. Also, it check for the Ethernet connection to decide on sending the sensor readings through Modbus TCP.
290+
291+
To convert the Opta ADC raw readings into voltage the following method can be used:
292+
293+
```arduino
294+
Voltage = analogRead(<Input>) * (3.3 / 4095.0) / 0.30337;
295+
```
296+
***The formula from above is the result of the Opta analog input frontend circuit.***
297+
298+
```arduino
299+
void SensorRead() {
300+
301+
T_I2 = (analogRead(T_SENSOR) * (3.3 / 4095.0) / 0.30337) * 10.0 - 0.31; // RTD transmitter is limited to 0-100 C in the 0-10 V range.
302+
C_I1 = (analogRead(C_SENSOR) * (3.3 / 4095.0) / 0.30337) * 1.4235; // Current sensor range is configurable, 0-10 A was used plus a calibration factor.
303+
P_I3 = (analogRead(P_SENSOR) * (3.3 / 4095.0) / 0.30337); // Pressure sensor range is 0-10 Bar with a respective 0-10 V output.
304+
305+
// update Arduino Cloud variables
306+
temp = T_I2;
307+
power = C_I1 * GRID_V; // Adjust GRID_V with your grid voltage
308+
press = P_I3;
309+
310+
Serial.print("Temperature: ");
311+
Serial.print(T_I2, 1);
312+
Serial.println(" C");
313+
Serial.print("Current: ");
314+
Serial.print(C_I1, 2);
315+
Serial.println(" A");
316+
Serial.print("Power: ");
317+
Serial.print(power, 2);
318+
Serial.println(" W");
319+
Serial.print("Pressure: ");
320+
Serial.print(P_I3, 1);
321+
Serial.println(" Bar");
322+
323+
if (Ethernet.linkStatus() == LinkOFF) {
324+
Serial.println("Ethernet Cable Disconnected");
325+
326+
} else if (Ethernet.linkStatus() == LinkON) {
327+
Serial.println("Sending Modbus Message");
328+
ModbusSend();
329+
}
330+
}
331+
```
332+
333+
The `ModbusSend()` function attempts the connection with the Modbus TCP server, if it is successful it formats the sensor data to be sent.
334+
335+
```arduino
336+
void ModbusSend() {
337+
if (!modbusTCPClient.connected()) {
338+
339+
// client not connected, start the Modbus TCP client
340+
Serial.println("Attempting to connect to Modbus TCP server");
341+
342+
if (!modbusTCPClient.begin(server, 502)) {
343+
Serial.println("Modbus TCP Client failed to connect!");
344+
} else {
345+
Serial.println("Modbus TCP Client connected");
346+
}
347+
} else {
348+
// client connected
349+
//Parse Float data to integer
350+
int Mod_T = T_I2 * 100;
351+
int Mod_Power = power * 100;
352+
int Mod_P = P_I3 * 100;
353+
int Mod_C = C_I1 * 100;
354+
355+
// send the variables data
356+
if (modbusTCPClient.beginTransmission(HOLDING_REGISTERS, 0x00, 4)) {
357+
modbusTCPClient.write(Mod_T);
358+
modbusTCPClient.write(Mod_Power);
359+
modbusTCPClient.write(Mod_P);
360+
modbusTCPClient.write(Mod_C);
361+
}
362+
if (modbusTCPClient.endTransmission()) {
363+
Serial.println("Modbus Msg Sent Successfully");
364+
} else {
365+
Serial.println("Modbus Msg Not Sent");
366+
}
367+
}
368+
}
369+
```
370+
There are other functions in the main code listed below:
371+
372+
- `EthernetInit()`: initialize the Ethernet connection for the Modbus TCP communication.
373+
- `onPwrChange()`: callback that controls the compressor power relay from the cloud.
374+
- `NiclaVibrationHAndler()`: manages the BLE connection and gather the vibrations alert from the Nicla Sense ME.
375+
376+
***You can download the complete code from [here]().***
377+
187378
### Nicla Sense ME Code
188379

189380
### Arduino Cloud Dashboard

0 commit comments

Comments
 (0)