Electronics > Beginners
Arduino remote serial communication via ESP module
(1/2) > >>
Muffins:
Hi Guys,

I've written a program to communicate with an Arduino via serial. I've successfully turned an LED connected to the (serial connected) Arduino on and off from my desktop.

What I want to do is control the LED remotely over wifi using a similar serial communication program from my desktop. I want to do it this way because I'm learning to code my own programs and the serial communication code is at my level, at least enough to play with. If I can remotely control hardware with programs I write I can impress the guys at work and get more training.

What I have done so far is the following:
- Flashed (installed?) ESP-Link onto an ESP12F.
- Controlled the LED via the Console in the ESP-link browser page.
- Used Putty to start a Telnet connection to the ESP module and view the serial print of the Arduino.

I was expecting to be able to type in my same commands that worked in the ESP browser console into the Putty terminal to control the LED but I haven't gotten this to work. I don't know if it's used for viewing only or not.

I am left now trying to find out how to create a virtual serial port or something so that my desktop program can interface serially with the remote Arduino.

Any ideas about what I could try?
JustMeHere:
There's more than one serial port on the ESP8266
Psi:
ya need to post your code. My guess is yes, the example your using is send only to telnet ip/port.
Muffins:
Okay, this is what the Arduino sketch looks like:

#define BAUD 9600
//led
#define led 2
//macro for on/off
#define on (digitalWrite(led, HIGH))
#define off (digitalWrite(led, LOW))


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


 
}

void loop(){
  String input;
  //If any input is detected in arduino
  if(Serial.available() > 0){
    //read the whole string until '\n' delimiter is read
    input = Serial.readStringUntil('\n');
    //If input == "ON" then turn on the led
    //and send a reply
    if (input.equals("ON")){
      digitalWrite(led, LOW);
      Serial.println("Led is ON");
    }
    //If input == "OFF" then turn off the led
    //and send a reply
    else if (input.equals("OFF")){
      digitalWrite(led, HIGH);
      Serial.println("Led is OFF");
    }
  }
}


And this is the code from my program.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SerialPort.h"

using namespace std;

//String for getting the output from arduino
char output[10];

/*Portname must contain these backslashes, and remember to
replace the following com port*/
char *port_name = "\\\\.\\COM5";

//String for incoming data
char incomingData[20];

int main()
{
  SerialPort arduino(port_name);
  if (arduino.isConnected()) cout << "Connection Established" << endl;
  else cout << "ERROR, check port name";

  while (arduino.isConnected()){
    cout << "Input command: \n";
    std::string input_string;
    //Getting input
    getline(cin, input_string);
    //Creating a c string
    char *c_string = new char[input_string.size() + 1];
    //copying the std::string to c string
    std::copy(input_string.begin(), input_string.end(), c_string);
    //Adding the delimiter
    c_string[input_string.size()] = '\n';
    //Writing string to arduino
    arduino.writeSerialPort(c_string, 20);
    //Getting reply from arduino
    arduino.readSerialPort(output, 20);
    //printing the output
    puts(output);
    //freeing c_string memory
    delete[] c_string;
  }
}

Which is using this library: https://blog.manash.me/serial-communication-with-an-arduino-using-c-on-windows-d08710186498

The above code works for wired serial. The code on the Arduino works when sending commands via the ESP-link browser console.
C:

If you have been using the browser interface you could use
a program called

"cURL"
https://en.wikipedia.org/wiki/CURL


Navigation
Message Index
Next page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod