Arduino remote serial communication via ESP module - Page 1
Forum section: electronics / beginners

author: Muffins
author_rank: Regular Contributor
timestamp: May 19, 2018, 08:20:59 pm
content: 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-Linkonto an ESP12F.- Controlled the LED via the Console in the ESP-link browser page.- UsedPuttyto 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?

author: JustMeHere
author_rank: Frequent Contributor
timestamp: May 19, 2018, 10:29:32 pm
content: There's more than one serial port on the ESP8266

author: Psi
author_rank: Super Contributor
timestamp: May 20, 2018, 12:01:44 am
content: ya need to post your code. My guess is yes, the example your using is send only to telnet ip/port.

author: Muffins
author_rank: Regular Contributor
timestamp: May 20, 2018, 07:27:38 am
content: 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 arduinoif(Serial.available() > 0){//read the whole string until '\n' delimiter is readinput = Serial.readStringUntil('\n');//If input == "ON" then turn on the led//and send a replyif (input.equals("ON")){digitalWrite(led, LOW);Serial.println("Led is ON");}//If input == "OFF" then turn off the led//and send a replyelse 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 arduinochar output;/*Portname must contain these backslashes, and remember toreplace the following com port*/char *port_name = "\\\\.\\COM5";//String for incoming datachar incomingData;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 inputgetline(cin, input_string);//Creating a c stringchar *c_string = new char[input_string.size() + 1];//copying the std::string to c stringstd::copy(input_string.begin(), input_string.end(), c_string);//Adding the delimiterc_string[input_string.size()] = '\n';//Writing string to arduinoarduino.writeSerialPort(c_string, 20);//Getting reply from arduinoarduino.readSerialPort(output, 20);//printing the outputputs(output);//freeing c_string memorydelete[] c_string;}}Which is using this library:https://blog.manash.me/serial-communication-with-an-arduino-using-c-on-windows-d08710186498The above code works for wired serial. The code on the Arduino works when sending commands via the ESP-link browser console.

author: C
author_rank: Super Contributor
timestamp: May 20, 2018, 02:53:40 pm
content: If you have been using the browser interface you could usea program called"cURL"https://en.wikipedia.org/wiki/CURL

author: kripton2035
author_rank: Super Contributor
timestamp: May 20, 2018, 03:07:45 pm
content: why don't you use only the esp8266 to do your stuff remotely ? you don't need an arduino you already have an esp8266 !

author: Muffins
author_rank: Regular Contributor
timestamp: May 21, 2018, 07:43:38 pm
content: 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 thatI 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 themHere. 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++ fromhere, a blog post for it ishere. The example code works quite well.3) DownloadTibbo 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.

author: C
author_rank: Super Contributor
timestamp: May 21, 2018, 08:33:59 pm
content: 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 & StabilityAndreas Spiess has many videos about the ESP8266The 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

author: JustMeHere
author_rank: Frequent Contributor
timestamp: June 01, 2018, 10:11:45 pm
content: 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.