Author Topic: Arduino remote serial communication via ESP module  (Read 2232 times)

0 Members and 1 Guest are viewing this topic.

Offline MuffinsTopic starter

  • Contributor
  • Posts: 45
  • Country: za
Arduino remote serial communication via ESP module
« on: May 19, 2018, 08:20:59 pm »
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?
 

Offline JustMeHere

  • Frequent Contributor
  • **
  • Posts: 744
  • Country: us
Re: Arduino remote serial communication via ESP module
« Reply #1 on: May 19, 2018, 10:29:32 pm »
There's more than one serial port on the ESP8266
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 9950
  • Country: nz
Re: Arduino remote serial communication via ESP module
« Reply #2 on: May 20, 2018, 12:01:44 am »
ya need to post your code. My guess is yes, the example your using is send only to telnet ip/port.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline MuffinsTopic starter

  • Contributor
  • Posts: 45
  • Country: za
Re: Arduino remote serial communication via ESP module
« Reply #3 on: May 20, 2018, 07:27:38 am »
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.
 

Offline C

  • Super Contributor
  • ***
  • Posts: 1346
  • Country: us
Re: Arduino remote serial communication via ESP module
« Reply #4 on: May 20, 2018, 02:53:40 pm »

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

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


 

Online kripton2035

  • Super Contributor
  • ***
  • Posts: 2587
  • Country: fr
    • kripton2035 schematics repository
Re: Arduino remote serial communication via ESP module
« Reply #5 on: May 20, 2018, 03:07:45 pm »
why don't you use only the esp8266 to do your stuff remotely ? you don't need an arduino you already have an esp8266 !
 
The following users thanked this post: BergRD

Offline MuffinsTopic starter

  • Contributor
  • Posts: 45
  • Country: za
Re: Arduino remote serial communication via ESP module
« Reply #6 on: May 21, 2018, 07:43:38 pm »
In my mind the Arduino is a placeholder for a larger system. I want to learn to better use the ESP module, it's just going to take some time.

I'll get Curl going and play with it, thanks for that  :-+

I have managed to succeed at what I was trying to do. I can now communicate serially over wifi from a C++ program to the Arduino.

Here's how to do it.

1) Install ESP-Link onto the ESP module. If you are running windows you can use the Flash Download Tool from the official site; here.
You'll then need the binary files to flash onto the ESP, you can find them Here. Note, don't download the Assests, scroll down and click the link next to DOWNLOAD for the binaries.
Once you have flashed the ESP you can power it up, connect to it's open wifi and open the interface in your browser through the address 192.168.4.1. You can then go to the console page and send commands serially to the Arduino, make sure the right baud rate is selected. The debug features cause errors with the serial commands.

2) Download a serial communication library for C++ from here, a blog post for it is here. The example code works quite well.

3) Download Tibbo Device Server Toolkit, it has a VSP manager that you can use to setup a virtual serial port that connects via Telnet into the ESP. It allows you to interact with the Arduino as if it was connected to your pc via usb.

That's it. As you can see I don't have a deep understanding of whats going on but this seems to work.

 

Offline C

  • Super Contributor
  • ***
  • Posts: 1346
  • Country: us
Re: Arduino remote serial communication via ESP module
« Reply #7 on: May 21, 2018, 08:33:59 pm »

You need to keep in mind that the second you start using WIFI you are opening your network to external hacks.

With this fact in mind you should not use Telnet which has no encryption.  Every thing can be heard and monitored over WIFI.

To have a more secure console you should be using SSH not Telnet.

You should also note that esp8266 has one CPU which has to be shared to do work.
You can program the esp8266 with the Arduino IDE.
Care has to be taken in the Arduino program so as to not cause problems with the WIFI code.
In simple terms the Arduino program must be simple short steps that allow the WIFI code run when needed.
Look at

#33 Internet of Things with ESP8266 #5: Watchdogs, Timers & Stability


Andreas Spiess has many videos about the ESP8266

The ESP32 fixes this problem with dual CPU's.

You need to think of all the benefits and problems each change makes.

USB has much better security then networking.
The second you network you add all the connections to the network as possible security problems.

A physical connection is still better then a radio connection like WIFI.

Need to remember that better antenna's & radios increases the range of possible hacks.

C


 
 

Offline JustMeHere

  • Frequent Contributor
  • **
  • Posts: 744
  • Country: us
Re: Arduino remote serial communication via ESP module
« Reply #8 on: June 01, 2018, 10:11:45 pm »
Check out this project:
https://arduino-esp8266.readthedocs.io/en/latest/

It will be fairly easy to set up a web server on the ESP8266....easier than messing with sending the commands via serial.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf