How to convert an ArrayBuffer that is sent by serial to Arduino to an Int

Hi everyone,

I’m recently started with programming and I have a question about reading values from the serial that are sent by a chrome extension that I made. I’m using an Arduino Uno. Basically, I’m sending a value to my board when cookies are detected on a website. The idea is that my LED should start burning for 5 seconds after the chrome extension detected cookies. But somehow my Arduino doesn’t read the serial as I want. Can someone help me with how I should sending the value 3 on the right way from my chrome extension to the Arduino Board? Thanks in Advance!

First of all the code of my chrome extension that is sending data to the Arduino serial. The function sendLEDValue is sending that data to the serial.

const form = document.getElementById("control-row");
const go = document.getElementById("go");
const input = document.getElementById("input");
const message = document.getElementById("message");

// The async IIFE is necessary because Chrome <89 does not support top level await.
(async function initPopupWindow() {
  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

  if (tab?.url) {
    try {
      let url = new URL(tab.url);
      input.value = url.hostname;
    } catch {}
  }

  input.focus();
 
      const ports = await navigator.serial.getPorts();
 if (ports.length <= 0) {
      chrome.tabs.create({url:chrome.runtime.getURL('permissions.html')})
}
})();

form.addEventListener("submit", handleFormSubmit);

async function handleFormSubmit(event) {
  event.preventDefault();

  clearMessage();

  let url = stringToUrl(input.value);
  if (!url) {
    setMessage("Invalid URL");
    return;
  }

  let message = await searchDomainCookies(url.hostname);
  setMessage(message);
}

function stringToUrl(input) {
  // Start with treating the provided value as a URL
  try {
    return new URL(input);
  } catch {}
  // If that fails, try assuming the provided input is an HTTP host
  try {
    return new URL("http://" + input);
  } catch {}
  // If that fails...
  return null;
}

async function searchDomainCookies(domain) {
  
    const cookies = await chrome.cookies.getAll({ domain });

    if (cookies.length === 0) {
      console.log("No cookies found")
      sendLEDValue(2);
      console.log("Zero signal send");
      //readLEDValue();    
      return "No cookies found";
    }

    if (cookies.length > 0) {
      console.log("Cookies found");
      sendLEDValue(3);
      console.log("LED signal send");
      //readLEDValue();         
      return "Cookies found";
    }   
      
  }
      
async function sendLEDValue (value) {
      const ports = await navigator.serial.getPorts(); //let op dat je toegang tot 1 apparaat moet hebben
      await ports[0].open({ baudRate: 9600 });
      const writer = ports[0].writable.getWriter();
    
      var buffer = new ArrayBuffer(1);
      var uint8View = new Uint8Array(buffer);
      uint8View[0] = parseInt(value, 10)  
    
      //const data = new Uint8Array(buffer); // hello var buffer = new ArrayBuffer(1);
      await writer.write(buffer);
      console.log(buffer)
      writer.releaseLock;
      
      }



function setMessage(str) {
  message.textContent = str;
  message.hidden = false;
}

function clearMessage() {
  message.hidden = true;
  message.textContent = "";
}

This is the code of my Arduino:

int led = 7;
int lightOnLength = 0;

unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 5000;           // interval at which to blink (milliseconds)


void setup() {
  Serial.begin(9600);  
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);

  pinMode(buttonPin, INPUT);
}


void loop() {
  
  if (Serial.available() > 0) {
    lightOnLength = Serial.read();
    Serial.print("light On Length Available");
      }


  //LED for available cookies
  if (lightOnLength == 3){
  Serial.print("Cookies found");
  Serial.print(lightOnLength);   
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(5000);               // wait for a second
  }
  else if (lightOnLength == 2){
  digitalWrite(led, LOW);
  unsigned long currentMillis = millis();
    
  if (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;

  Serial.print("No cookies found");
  Serial.print(lightOnLength);    
  }
  }            
  else {
    digitalWrite(led, LOW);
    unsigned long currentMillis = millis();
    
    if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
      
    Serial.print(lightOnLength);
    }
  }
    
}

I guess you should ask that question in a Chrome forum as the problem is on the browser side I guess.

You don't describe what you actually get on the Arduino side but I guess you already tried using the Serial Monitor of the IDE. Does that work? I can imagine that the browser is not allowed to access the serial port or that several serial ports are available and it sends the data to the wrong port. But that are just two guesses, many more are imaginable. But all are problems on the browser side.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.