Robotzero One Sending Data Esp8266 To Esp8266
Robotzero One Sending Data Esp8266 To Esp8266
ESP8266
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
A simple guide to sending data from one ESP8266 to another over Wi-Fi using an ad-hoc, device to device
network, without using a wi router.
The ESP8266WebServer library allows you run an ESP8266 as a basic webserver and access point. This can process data received from a remote
sensor over Wi-Fi without connecting the devices to a network or router.
For this tutorial I’m using two NodeMCU boards from eBay but you can do this with any ESP8266 based board. To simulate the output from a sensor
I’m using a trim pot potentiometer like these from eBay. I’m also using two small OLED screens from AliExpress so you can see the data easily but
you don’t need these if you want to see the results in the serial monitor.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
As you can see in the video below, when the potentiometer pot is adjusted the value shown on the transmitting module OLED changes to re ect the
change in voltage to pin A0. On the receiver, the value on the OLED is updated as the data is received from the transmitter.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
If you don’t have the Arduino IDE set up for the ESP8266 range yet you can nd a tutorial here – https://robotzero.one/heltec-wi -kit-8/ under Setting
Up the Arduino IDE for the ESP8266 Range.
The wiring is identical for both the transmitter and receiver except the transmitting device has the potentiometer connected to the power
and analogRead(A0) pins. You might need to connect the A0 pin via a resistor on some boards as they only read up to 1v on this pin.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
ESP8266 to ESP8266 Wi-Fi Wiring Diagram
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
ESP8266 to ESP8266 Wi-Fi Breadboard Layout
If it’s not installed already you will need to install the U8g2 display library (for the OLED) It can be installed using the Arduino IDE library manager –
open Sketch > Include Library > Manage Libraries and search for and then install U8g2.
Here’s the sketch for the transmitter. There’s a more verbose version if you want to see the output in the serial monitor or need to debug here.
#include <ESP8266WiFi.h>
#include <U8g2lib.h>
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
const char *password = "pingu4prez";
const int analogInPin = 0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the potentiometer
int outputValue = 0; // value sent to server
void setup() {
Serial.begin(115200);
delay(10);
u8g2.begin();
u8g2.setFont(u8g2_font_logisoso62_tn);
u8g2.setFontMode(0); // enable transparent mode, which is faster
}
void loop() {
// read the analog in value:
sensorValue = analogRead(A0);
// map to range. The pot goes from about 3 to 1023. This makes the sent value be between 0 and 999 to fit on the OLED
outputValue = map(sensorValue, 3, 1023, 0, 999);
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
char intToPrint[5];
itoa(outputValue, intToPrint, 10); //integer to string conversion for OLED library
u8g2.firstPage();
u8g2.drawUTF8(0, 64, intToPrint);
u8g2.nextPage();
// We now create a URI for the request. Something like /data/?sensor_reading=123
String url = "/data/";
url += "?sensor_reading=";
url += intToPrint;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
delay(500);
}
On the server (receiver) the sketch looks like this. Again, if you want a version with serial outputs to see more details you can download that here.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <U8g2lib.h>
ESP8266WebServer server(80);
void handleSentVar() {
if (server.hasArg("sensor_reading")) { // this is the variable sent from the client
int readingInt = server.arg("sensor_reading").toInt();
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
char readingToPrint[5];
itoa(readingInt, readingToPrint, 10); //integer to string conversion for OLED library
u8g2.firstPage();
u8g2.drawUTF8(0, 64, readingToPrint);
u8g2.nextPage();
server.send(200, "text/html", "Data received");
}
}
void setup() {
delay(1000);
u8g2.begin();
u8g2.setFont(u8g2_font_logisoso62_tn);
u8g2.setFontMode(0); // enable transparent mode, which is faster
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then
run the handleSentVar function
server.begin();
}
void loop() {
server.handleClient();
}
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Hopefully this tutorial helps you start o in the right direction with using Wi-Fi on these devices. I’ve seen other tutorials that made things a lot more
complicated than they need to be.
Buy Me A Coffee
If you liked this tutorial and want me to create more then please say thanks by buying me a co ee here...
(Paypal) €2
Or send me Bitcoin/Litecoin/Etherium 🙂
Previous Next
ESP8266 Built-in OLED – Heltec WiFi Kit 8 ESP8266 Fan Speed Control with DS18B20 Temperature Sensor
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
You may also like...
ESP8266 and BME280 Temp, Pressure and Humidity Sensor over SPI
dave
December 1, 2018 at 10:07 pm
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
receiving
REPLY
WordBot
December 3, 2018 at 4:25 pm
You’ll need to change the query string sent so the receiving ESP will know which one sent the data. Something like:
Basement ESP: ?sensor_reading_basement=
Attic ESP: ?sensor_reading_attic=
On the receiver you’ll need a smaller font to t everything on the screen and change the handleSentVar to listen to the two new variables above. This
system doesn’t use your WiFi, it runs on its own network.
REPLY
Mauro
December 12, 2018 at 10:36 am
Hi, is it possible to send sensor value from an esp8266 to an esp32 with the same technic ?
best regards
REPLY
WordBot
December 12, 2018 at 1:47 pm
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Yep. The data is sent as a GET request like this: /data/?sensor_reading=123 so any web server can parse the information and use it. I’m not sure
exactly which libraries you need but the ESP32 has equivalents to the ESP8266WiFi and ESP8266WebServer libraries.
REPLY
Alexander
January 1, 2019 at 9:03 pm
Hello! Could you help me a little bit? I just want to send an int value from one esp board to other.
I’ve connetcted a pontentiometer on the rst esp(Wemos D1 mini), and gets a value using map() function. I want to send it to the second board for
changing blink interval of LED.
I tried to change your sketch, but got no luck =(.
REPLY
WordBot
January 2, 2019 at 1:01 pm
Hi there.
First check the simple blink sketch works:
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output
}
void loop() {
digitalWrite(BUILTIN_LED, HIGH); // turn on LED with voltage HIGH
delay(1000); // wait one second
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
digitalWrite(BUILTIN_LED, LOW); // turn o LED with voltage LOW
delay(1000); // wait one second
}
If that’s OK then something like the following might work. I don’t have the project set up at the moment.
ESP8266WebServer server(80);
void handleSentVar() {
if (server.hasArg(“sensor_reading”)) {
varDelay = server.arg(“sensor_reading”).toInt();
server.send(200, “text/html”, “Data received”);
}
}
void setup() {
delay(1000);
pinMode(BUILTIN_LED, OUTPUT);
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
server.on(“/data/”, HTTP_GET, handleSentVar);
server.begin();
}
void loop() {
server.handleClient();
REPLY
Alexander
January 3, 2019 at 12:43 pm
REPLY
kave
February 6, 2019 at 10:18 am
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
hi,
It`s very useful project
I need help for my project,
I want to send data via serial form ESP-12 to ESP-01 , That way one RC522 RFID connect to ESP-12 and read tag card and it should be send tag to the
ESP-01 with high speed like your project.
Thanks so much if you can help me 🙂
REPLY
WordBot
February 7, 2019 at 10:27 am
Hi,
Do you have the code for the reading the tag? In that code you just need to have it send the data in the same way the transmitter code above sends
data to the other ESP.
REPLY
Ron
February 12, 2019 at 6:06 am
Hi,
Thanks for the example. It saves me a lot of work/time nding out the wheel as I’m a basic electronics engineer and not a TCP/IP/HTML specialist. I’m
lazy and have replaced the potmeter for a random generator and the output is written to a serial monitor. All seems to work ne however in my
example I have contineously timeouts on client.connect() and client.available(). Any suggestions?
REPLY
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
WordBot
February 12, 2019 at 10:54 am
Hi,
At a guess I would say that there are too many connection attempts for the webserver in the ESP8266. Maybe try a delay() command in the loop to
slow down the connection attempts? Or if you are feeling brave you could try another webserver: https://github.com/me-no-
dev/ESPAsyncWebServer
REPLY
Tudor
March 6, 2019 at 7:07 pm
HI,
I try to do a project in which I have 2 transmitters and one receiver. Each transmitter has a switch and if I press the switch (from a transmitter or the
other transmitter) the LED on the receiver should go on. Could you please help me? Thank you.
REPLY
WordBot
March 6, 2019 at 7:46 pm
It shouldn’t be too hard. Both transmitters would have the same code that detects the button press and sends this in the URL “?sensor_reading=on”;
to the listening receiver. On the receiver you would set the LED high in the handleSentVar() function.
REPLY
Tudor
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
March 6, 2019 at 8:24 pm
Thank you. I am a beginner. Could you please help me with the code? I do not understand exactly which lines should I keep and which not.
REPLY
WordBot
March 7, 2019 at 10:29 am
If you are new to this you should break it down into small parts so you can understand what is happening so you can work with it later. The
rst thing you need is add a switch to the transmitter. The simplest way (not necessarily the best) is to pull the pin connected to the onboard
LED to ground with a your switch (or just connect D4 to GND with a piece of wire).
Then use the code below to see the change in the serial monitor:
const int button = 2; // D4/GPIO2 is connected to the internal LED (which is lit when the pin is LOW).
int pinState= 0;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT);
}
void loop() {
pinState = digitalRead(button);
if (pinState == LOW) {
Serial.println(“LED Turned ON”);
delay(1000);
}
else {
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Serial.println(“LED Turned OFF”);
delay(1000);
}
}
REPLY
WordBot
March 7, 2019 at 10:34 am
Then you need to edit the code in the tutorialfor the transmitter so it sends the button state rather than the sensor reading something like this:
REPLY
Mitchell
March 9, 2019 at 7:19 pm
Hi, cool project with great info! I want to output the potentiometer reading via pwm instead of seeing the value on screen. Could you guide me on the
coding portion?
REPLY
WordBot
March 9, 2019 at 7:57 pm
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Do you have the code for the PWM part on the receiver? It shouldn’t be too hard to change the code in the tutorial to update the PWM code rather
than the screen.
REPLY
Mitchell
March 9, 2019 at 8:32 pm
No I have very basic understanding of programming. I usually troll tutorials such as this looking for code similar to what I need and then try to
stitch the bits together 🙁
REPLY
Mitchell
March 9, 2019 at 8:34 pm
I have your original code with the oled portions omitted on both mcu
REPLY
WordBot
March 10, 2019 at 9:10 am
I don’t really have time to look at this but if you ask on the forum here – https://www.esp8266.com/ someone will probably help. Tell them
how far you’ve got and what you want to achieve.
REPLY
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
hassan11
March 9, 2019 at 7:46 pm
can you help me i need code to send data between two esp32 , data from sensors in one esp32 to onther
REPLY
WordBot
March 10, 2019 at 9:17 am
The code should be mostly the same. You’ll need to use di erent libraries for the wi and wi server and possibly change the pins for the oleds if
you are using them.
REPLY
Arduino93
March 10, 2019 at 8:50 pm
i used this code to transmit the data from esp32 and they connected together but sensorValue still constant at “4095” in both esp32 , what should i do ?
REPLY
WordBot
March 11, 2019 at 9:37 am
Do you have code similar to this on the transmitter so you can see the output from the sensor:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
// read the analog in value:
sensorValue = analogRead(A0);
// map to range. The pot goes from about 3 to 1023. This makes the sent value be between 0 and 999 to t on the OLED
outputValue = map(sensorValue, 3, 1023, 0, 999);
REPLY
Safei
March 12, 2019 at 10:22 pm
REPLY
WordBot
March 13, 2019 at 6:29 pm
So you want to take 5 sensor values and send them together? You can just make 5 variables to hold each one and send it like this.
url += “?sensor_reading1=”;
url += intToPrint1;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
url += “?sensor_reading2=”;
url += intToPrint2;
etc…
Or maybe a tidier way is to put them all into one string, send that and process it on the other side. One way of sending strings:
https://randomnerdtutorials.com/decoding-and-encoding-json-with-arduino-or-esp8266/
REPLY
Safei
March 15, 2019 at 10:02 am
I already do this but it didn’t receive values in the other esp and it display “Client Timeout !” in serial monitor of transmitter esp …
REPLY
WordBot
March 15, 2019 at 5:27 pm
Client timeout is not connecting to your WiFi. Try some of the ESP8266 Wi examples to get this working. It’s always best to start simple and
then add things bit by bit to check as you go. If you are new to this.. it’s much better to do that than try and edit a more complicated script to t
your case.
REPLY
Die y
April 3, 2019 at 4:25 pm
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In your example the oled on the receiver is updating very quickly. I implemented the example code on two of my nodeMCU’s and I get a much slower
response time. Did you use a di erent code for the video? Thanks for the tutorial by the way.
REPLY
WordBot
April 3, 2019 at 4:38 pm
It’s the same script. In the tutorial there’s a version with more output on the serial port which might help diagnose. Someone in the comments said
theirs worked better with this line:
WiFi.mode(WIFI_AP);
added above this line:
WiFi.softAP(ssid, password);
in the receiver code but this isn’t in the examples code I developed this from.
REPLY
drdra
May 10, 2019 at 3:33 pm
Hey WordBot, thanks for your patience, can you tell me how can I send to multiple receivers?
REPLY
WordBot
May 11, 2019 at 8:29 am
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Looking quickly at this. If you take the part // We now create a URI for the request. Something like /data/?sensor_reading=123 and turn it into a function.
Then take this part // Use WiFiClient class to create TCP connections and do something like
Once you have that working the same as the existing example you can then make it a loop that loops through the IP addresses you have for the
clients.
REPLY
Jeevan
May 14, 2019 at 1:51 pm
Hi
the project you have explained in a crispy and simple way thanks for the new thing which I learned from you.
But when I am trying to compile your code I am getting an error saying
from C:\Users\KERNEL\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0/tools/sdk/lwip2/include/lwip/init.h:40,
from C:\Users\KERNEL\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266/IPAddress.h:27,
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
from C:\Users\KERNEL\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\libraries\ESP8266WiFi\src/ESP8266WiFi.h:31,
from C:\Users\KERNEL\Documents\Arduino\NodeMCU_Transmitter\NodeMCU_Transmitter.ino:1:
I am not getting this error what actually its saying. Please help me ,thanks in advance
REPLY
WordBot
May 14, 2019 at 1:55 pm
Your installation for the ESP8266 package is bad. How did you set up the esp8266 library? There’s a tutorial on this page for that:
https://robotzero.one/heltec-wi -kit-8/
REPLY
Deepanshu Kumar
June 7, 2019 at 9:43 am
Hey,
I’m trying to transfer values from one arduino UNO to other arduinoUNO using ESP8266. I ‘m in the process of doing the same but I’m not able to
transfer data neither I’m able to connect at the same HTTP address and port. For keeping program simple, initially, I’m trying to transfer a speci c value
of ‘temp’ to other ESP8266.
REPLY
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
WordBot
June 7, 2019 at 12:38 pm
REPLY
Deepanshu Kumar
June 9, 2019 at 4:06 pm
Yes, I tried. The default IP which is being allocated to the client is “192.168.4.2”.
REPLY
WordBot
June 9, 2019 at 6:49 pm
Did you see the server example with more verbose output – https://robotzero.one/wp-content/uploads/2018/04/ESP8266-wi -receiver.ino
maybe this will help. Maybe paste your current client and server code at https://pastebin.com/ and I’ll try to take a look.
REPLY
Deepanshu Kumar
June 10, 2019 at 7:12 pm
Hey, thanks. I’m getting something better than earlier now. The client is printing the values. Server program still needs some editing.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
REPLY
Deepanshu Kumar
June 11, 2019 at 2:34 pm
I want to transfer GPS values from system 1 to system 2 using Wi-Fi module ESP8266 along with Arduino UNO. I’ve written the main codes
for server and client end. I’m not able to print anything on LCD if I’m using ESP8266 and selected ‘generic ESP8266’ boards. What could be
the issue?
REPLY
WordBot
June 12, 2019 at 8:35 am
Which LCD are you using? Try some of the examples for that LCD rst.
REPLY
Sarah
July 3, 2019 at 3:49 pm
Hi, I’m trying to set up 3 nodemcu esp8266 to be both client and server. Basically what it would do is, if ESP01 sense a motion it will turn on the led then
notify the other two esp that there is someone so that they could also turn on their led. At the same time, the ESP01 will also be ready for any incoming
noti cation that the other two esp might send. Do you have any idea to make this work?
REPLY
WordBot
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
July 3, 2019 at 6:37 pm
I’m pretty sure you can have client and server running on the same ESP8266. Think of them as a triangle. Each one sends to the other two it’s
connected to via their IP addresses. You could include in the message a code to say which device was triggered and the receiving device could ash
the LED in a pattern to show the device number.
REPLY
Sarah
July 3, 2019 at 10:51 pm
REPLY
WordBot
July 4, 2019 at 8:56 am
I don’t have an example but you can look in the ESP8266 example in the IDE and hack something together. Another (possibly better) way to do this is
to use a mesh network: https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFiMesh
REPLY
Syira
July 5, 2019 at 5:52 am
Hi,
how to make one esp8266 ask another esp8266 to light up it’s led at a certain brightness.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
REPLY
WordBot
July 5, 2019 at 10:27 am
Hi, In the Arduino IDE: File>Examples>Basics>Fade has a very basic setup for controlling the brightness of an LED. You can use similar code on the
receiver from the tutorial above to control brightness. Search ESP8266 PWM for other tutorials.
REPLY
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name * Email *
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Website
Post Comment
This site uses Akismet to reduce spam. Learn how your comment data is processed.
ESP8266 and BME280 Temp, Pressure and Humidity Sensor over SPI (20,901)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
New Tutorial Announcements
Sign up
Search
Search …
Buy Me A Coffee
If you liked a tutorial and want to say 'Thanks!' you can buy me a co ee here...
€2
(Paypal)
Categories
3D Printing
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Arduino IDE
Arduino Uno
Bluetooth
Electronics
ESP32
ESP8266
Face Recognition
JavaScript
K210
LoRa
MicroPython
Raspberry Pi
RISC-V
Solar
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD