I try to make a very basic app that reads an analog voltage to be displayed in WebUI (and a LCD, if connected). Everything seems to wrk well exept that the value don't show in the WebUI.
UNO-Q 2MB, windows
/*
Sketch: Analog input to WebUI
(LCD connected via voltage level translation 3,3V to 5V)
*/
// include the library code:
#include <LiquidCrystal.h>
#include <Arduino_RouterBridge.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 7, en = 6, d4 = 1, d5 = 5, d6 = 2, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int rw = 10;
int ext_input_A = 9; // isolated input rom shield 5V
int D2_LED = 12; // isolated output to shield 5V: D2-LED
int D3_LED = 11; // isolated output to shield 5V: D3_LED
boolean LCD_on_ext_A = false; // default as LOW when LCD shield is not connected
const float V_REF = 3.3; // 3,3V for UNO-Q inputs
const float R_BITS = 10.0; // number of bits ADC-resolution
const float ADC_STEPS = (1 << int(R_BITS)) - 1; // Number of steps (2^R_BITS - 1)
int sensorValueDigital = 0; // binary/digital value
float quotient = 0.0; // floating point number with integer and decimal part
int intPart = 0; // the integer part of conversion from binary to decimal value from ADC: A0
float decimal_part = 0.0;
int desimal_as_int = 0;
void setup() {
// put your setup code here, to run once:
Bridge.begin();
Monitor.begin();
pinMode(rw, OUTPUT);
digitalWrite(rw, LOW);
pinMode(ext_input_A, INPUT);
LCD_on_ext_A = digitalRead(ext_input_A);
if (LCD_on_ext_A == true) { // LCD shield connected?
// ---------------LCD 40x2 setup --------------------
lcd.begin(40, 2);
delay(3000); // wait for LCD to start up
lcd.setCursor(0, 0);
delay(50);
lcd.print("UNO-Q: Analog voltage from A0, ");
delay(30);
lcd.print(int(R_BITS));
delay(30);
lcd.print(" bits: ");
delay(50);
//Monitor.println("LCD is connected. ");
}
pinMode(D2_LED, OUTPUT); // (leds can be used for extra info)
digitalWrite(D2_LED, LOW); // shield-5V: D2-LED off
pinMode(D3_LED, OUTPUT);
digitalWrite(D3_LED, LOW); // shield-5V: D3-LED off
}
void loop() {
// main code run repeatedly:
delay(5000);
digitalWrite(D2_LED, HIGH); // shield-5V: D2-LED ON
sensorValueDigital = analogRead(A0);
// asynchronous notification
Bridge.notify("sensor_data", sensorValueDigital); // send binary value to main.py
Monitor.print("ADC bin value: ");
Monitor.println(sensorValueDigital); // check the value in Serial Monitor
if (LCD_on_ext_A == true) {
printOut_LCD(); // convert and print to LCD
}
digitalWrite(D2_LED, LOW); // shield-5V: D2-LED off
delay(2000);
}
// convert the binary value to two integer numbers separated by a decimal point
// and print to LCD
void printOut_LCD() {
quotient = (sensorValueDigital / ADC_STEPS) * V_REF; // floating point number with integer and decimal part
intPart = (sensorValueDigital / ADC_STEPS) * V_REF; // the integer part of the conversion
decimal_part = quotient - intPart; // the decimal part of the conversion
desimal_as_int = decimal_part * 1000; // move the decimal point; keeping three digits, and convert to integer
lcd.setCursor(0, 1);
delay(50);
lcd.print(" "); // delete previous content in row
delay(30);
lcd.setCursor(0, 1);
delay(50);
lcd.print(intPart); // integer part: (x,---)
delay(30);
lcd.print(",");
delay(30);
if (desimal_as_int < 10) {
lcd.print("00");
delay(30);
lcd.print(desimal_as_int); // thousandths: (-,00x)
delay(30);
} else if (desimal_as_int < 100) {
lcd.print("0");
delay(30);
lcd.print(desimal_as_int); // hundredths and thousandths: (-,0xx)
delay(30);
} else {
lcd.print(desimal_as_int); // tenths, hundredths and thousandths: (-,xxx)
delay(30);
}
lcd.print(" Volt");
delay(3);
}
python:
import time
from arduino.app_utils import App, Bridge
from arduino.app_bricks.web_ui import WebUI
# initialize WebUI
web_ui = WebUI()
print("Hello world!")
def sensor_data_callback(value):
# Prosess the data if needed
print(f"Received from sketch: ", {value})
# Send the data to the web UI using the same key/event name
web_ui.send_message('sensor_data_update', value)
# Register the callback function with the Bridge
Bridge.provide("sensor_data", sensor_data_callback)
def loop():
time.sleep(1)
App.run(user_loop=loop)
# See: https://docs.arduino.cc/software/app-lab/tutorials/getting-started/#app-run
app.js:
let errorContainer;
/*
* Socket initialization. We need it to communicate with the server
* <script src="app.js"></script>
*/
const socket = io(`http://${window.location.host}`); // Initialize socket.io connection
// Start the application
document.addEventListener('DOMContentLoaded', () => {
errorContainer = document.getElementById('error-container');
initSocketIO();
});
function initSocketIO() {
socket.on('connect', () => {
if (errorContainer) {
errorContainer.style.display = 'none';
errorContainer.textContent = '';
}
});
socket.on('sensor_data_update', (value) => {
updateValue(value);
});
socket.on('disconnect', () => {
if (errorContainer) {
errorContainer.textContent = 'Connection to the board lost. Please check the connection.';
errorContainer.style.display = 'block';
}
});
};
function updateValue(bin_value) {
console.log("Received binary value: ", bin_value);
document.getElementById("my-text-box").textContent = bin_value;
//valueTxtBox.textContent = status;
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Analog input to web UI</title>
</head>
<body>
<h1>Binary code value: <span id="my-text-box">n/a</span></h1>
<script src="libs/socket.io.min.js"></script>
<script src="app.js"></script>
</body>
</html>



