0
votes

Goal: Send two integer values from Arduino Nano to internet via ESP8266 using Arduino IDE

I am new to embedded programing and currently working on a project which sends some integer value from Arduino analog pins to an online database (IP address, port) via esp8266.

At this moment I know how to individually send data from ESP8266 to an IP keeping ESP in client mode. But I don't know how to transfer data generated at Arduno Nano to ESP8266.

#include <ESP8266WiFi.h>
#include<Wire.h>

const char *ssid = "SSID";
const char *password = "asdfghjkl";

const char* host = "192.222.43.1";
int portNum = 986;

WiFiClient client;
WiFiServer server(portNum);

void setup() {
  Serial.begin(115200);
  Wire.begin();
  delay(10);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("WIFI OK");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("Connected to Wifi");
}

String message="";

void loop() {  
message = "12,13"; // Message to be sent to ESP8266

  if(!client.connected())
      {
        client.connect(host,portNum);
      }
      if(message.length()>0)
      {
        Serial.println(message);
        client.println(message);

        message="";
    }

I can understand I would have to connect the TX-RX pin of Arduino - ESP to pass the data. But for some reason I am not able to make it work.

I would really appreciate if someone can help me understand the process with a simple example.

Thanks.

PS: The reason I had to use Arduino is because the sensor I am using need 2 analog Pins and ESP just have 1.

2
Lighting control via Internet ESP8266 &Arduino & Firebase Part 1: setup Arduino: Video Part 2 Connect Esp8266 to Firebase Part 3: Build IOS app control on/OffToan Nguyen Dinh

2 Answers

1
votes

You connect Arduino Tx to Esp Rx

You connect ESP Tx to your serial device to your PC (so you can read the messages from the ESP in your Terminal window)

On the ESP you use the Wire library you have loaded.

You use the Serial object to listen for incoming data on the ESP's Rx pin.

void loop()
{
     while (Serial.available()) 
     {
         Do something;
     }
}

This works exactly the same as Arduino to Arduino serial comms and there is a nice tutorial here: Arduino to Arduino comms

WARNING: ESPs use 3.3V and Arduinos use 5V on the Tx and Rx pins. You must not allow 5v to reach the pins of the ESP or it may burn out.

This tutorial shows a safe wiring diagram. safe wiring diagram

-1
votes

1) Try this sample: simple sample that looks good

2) You have a logic problem in your loop function a) Your message will be send out as fast as possible because after you leave the loop function you will enter the function again b) You don't wait for incoming data

I hope the sample helps: I didn't try it because I directly used AT comands instead.