Home » vue » Networking with Arduino

Networking with Arduino

Posted by: admin November 18, 2021 Leave a comment

Questions:

I am trying to communicate between my local environment and a friend’s Arduino board connected to the internet by an ESP8266 wifi chip and port forwarding.

If I execute a command, the command is received by the ESP and passed to the Arduino, where it is executed based on a corresponding function.

What I am looking to do have the ESP return the reading to my machine
(‘the web server’) in Javascript.

I was hoping to use axios for promise-based HTTP/S requests. The action I send from axios is being registered on the board and the action is being completed and returning html to the webpage when I visit the IP/url in the browser, but how can I get a successful or error response from Arduino in the axios response i.e. JavaScript?

function_on() {
  var ArduinoVar = "http://" + this.ip + ":80";
  axios.get(ArduinoVar, {
    // cm1 is a function on Arduino, processed after 2 seconds
    params: {
      "cm1": 2000
    }
  })

  // The error was being handled here. I have since implemented CORS to manage pre-flight requests and now the error is not being hit anymore.
  .then(response => {
    console.log('response');
    console.log(response);
  })
  .catch(error => {
    console.log('error');
    console.error(error);
  });
  {Conection: close};

The Arduino code is set up to client.println “On 1” or “Off 0” to the browser when the function is hit. If I visit my friends IP, this works fine.

How can I register this with a response in Javascript?

Suggestions please?

Thanks.

Answers: