ESP8266 TO ARDUINO UNO

Good day community.
I'm trying to send sensor data to IBM WATSON IOT using ESP8266-E01 wifi module and Arduino Uno. The code seems to work well except for the fact that the ultrasonic sensor reading is always ZERO. I'm assuming the ESP8266 isn't able to read the digital inputs. Any assitance will be highly appreciated
Attached is my code

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

const char* ssid = "Smile4g";
const char* password = "****************";

#define ORG "*******"
#define DEVICE_TYPE "Sensor"
#define DEVICE_ID "benedict"
#define TOKEN "****************"

char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

const char eventTopic[] = "iot-2/evt/status/fmt/json";
const char cmdTopic[] = "iot-2/cmd/led/fmt/json";

const int trigPin = 3;
const int echoPin = 2;
long duration;
int distance;

WiFiClient wifiClient;
void callback(char* topic, byte* payload, unsigned int payloadLength) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < payloadLength; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

PubSubClient client(server, 1883, callback, wifiClient);

int publishInterval = 5000; // 5 seconds//Send adc every 5sc
long lastPublishMillis;

void setup() {
  Serial.begin(115200); Serial.println();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  wifiConnect();
  mqttConnect();
}

void loop() {
  if (millis() - lastPublishMillis > publishInterval) {
    publishData();
    lastPublishMillis = millis();
  }

  if (!client.loop()) {
    mqttConnect();
  }
}

void wifiConnect() {
  Serial.print("Connecting to "); Serial.print(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());

}

void mqttConnect() {
  while (!!!client.connected()) {
    Serial.print("Reconnecting MQTT client to "); Serial.println(server);
    if (!!!client.connect(clientId, authMethod, token)) {
      Serial.print(".");
      delay(500);
    }
    if (client.subscribe(cmdTopic)) {
      Serial.println("subscribe to responses OK");
    } else {
      Serial.println("subscribe to responses FAILED");
    }
    Serial.println();
  }
}

void publishData() {
  
   
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(1000);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance= duration*0.034/2;
    Serial.print("Distance: ");
    Serial.println(distance);
        
     String payload = "{\"d\":{\"adc\":";
  payload += String(distance);
  payload += "}}";

  Serial.print("Sending payload: "); Serial.println(payload);

  if (client.publish(eventTopic, (char*) payload.c_str())) {
    Serial.println("Publish OK");
  } else {
    Serial.println("Publish FAILED");
  }
}

Mqtt_to_IBM_Watson.ino (2.6 KB)

The first two lines in "publishData()" should be:

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

in order to clear the pin. You should also consider to change "*0.034/2" to "*0.017" since this will remove an unecessary division.

EDIT: You have 1000 us delay, should that not have been 10?

thanks for your response. Values are still ZERO. When i use the Ultrasonic sensor on the Arduino board without the ESP8266, it works fine.

how did you connect the sensor to esp-01?

Arduino Uno ESP8266
3.3v 3.3v, EN,RESET
RX RX
TX TX
GND GND, GPIO1

Try hardcoding a value to send, just to make sure data is getting from the Arduino to the ESP8266 -> to the cloud.

I'm concerned because I don't see how the 8266 is connected, but it normally talks over serial. But you're also using the Uno's only serial port for debugging, and I see no sign of a softwareSerial instance.

Put another way - I'm not convinced ANYTHING is making it to the cloud, so it's just showing you the default starting value of 0.

Edit: Your recent post about the wiring confirms it.

Where is your level shifter? You are liable to damage the esp8266 putting a 5v output onto it! At least put a resistor (1k is fine) in series with the Arduino's TX line...

In serial, you have to cross RX and TX - one side's RX goes to other side's TX (they stand for Receive and Transmit - you want what one side transmits to be received by the other. You can actually damage parts by connecting TX to TX, because you're tying two outputs that will frequently have different values together).

Your esp sketch assumes sensor is on pins 2 and 3 of the esp. Pin 3 is TX on esp8266.

Wait, I'm confused, is that the sketch on the Arduino Uno, with the ESP8266 running the default FW, or is that sketch running on the ESP8266 (in which case the uno is sitting there doing nothing?)

I assumed the former.

DrAzzy:
Wait, I'm confused, is that the sketch on the Arduino Uno, with the ESP8266 running the default FW, or is that sketch running on the ESP8266 (in which case the uno is sitting there doing nothing?)

not for nothing. I am sure, the sensor is connected to Uno :slight_smile:

DrAzzy:
Try hardcoding a value to send, just to make sure data is getting from the Arduino to the ESP8266 -> to the cloud.

I'm concerned because I don't see how the 8266 is connected, but it normally talks over serial. But you're also using the Uno's only serial port for debugging, and I see no sign of a softwareSerial instance.

Put another way - I'm not convinced ANYTHING is making it to the cloud, so it's just showing you the default starting value of 0.

Edit: Your recent post about the wiring confirms it.

Where is your level shifter? You are liable to damage the esp8266 putting a 5v output onto it! At least put a resistor (1k is fine) in series with the Arduino's TX line...

In serial, you have to cross RX and TX - one side's RX goes to other side's TX (they stand for Receive and Transmit - you want what one side transmits to be received by the other. You can actually damage parts by connecting TX to TX, because you're tying two outputs that will frequently have different values together).

DrAzzy:
Try hardcoding a value to send, just to make sure data is getting from the Arduino to the ESP8266 -> to the cloud.

I'm concerned because I don't see how the 8266 is connected, but it normally talks over serial. But you're also using the Uno's only serial port for debugging, and I see no sign of a softwareSerial instance.

Put another way - I'm not convinced ANYTHING is making it to the cloud, so it's just showing you the default starting value of 0.

Edit: Your recent post about the wiring confirms it.

Where is your level shifter? You are liable to damage the esp8266 putting a 5v output onto it! At least put a resistor (1k is fine) in series with the Arduino's TX line...

In serial, you have to cross RX and TX - one side's RX goes to other side's TX (they stand for Receive and Transmit - you want what one side transmits to be received by the other. You can actually damage parts by connecting TX to TX, because you're tying two outputs that will frequently have different values together).

I did hardcoding of 0s and 1s using the A0 of the Arduino, and it was uploaded to the cloud. I'm using a breadboard power supply for the 3.3v of the ESP8266. I tried connecting RX to TX and vice versa, i got gibberish in my serial monitor

Juraj:
not for nothing. I am sure, the sensor is connected to Uno :slight_smile:

yeah the sensor and the esp8266 are connected to the UNO but the code is uploaded to the ESP8266 board(not the UNO) via the Arduino IDE

so you think esp should read uno pins with digitalRead? No, digitalRead in esp reads pins of the esp8266.

Juraj:
so you think esp should read uno pins with digitalRead? No, digitalRead in esp reads pins of the esp8266.

how do i correct it?

What are you even doing with the arduino Uno in this configuration then?

It sounds like you're just running code on the ESP8266. So pick an unused pin on the esp8266, connect the sensor to that, adjust code, and remove the uno.... Assuming pulseIn works on esp8266, at least.

DrAzzy:
What are you even doing with the arduino Uno in this configuration then?

It sounds like you're just running code on the ESP8266. So pick an unused pin on the esp8266, connect the sensor to that, adjust code, and remove the uno....

esp-01 has only RX, TX, 0 and 2.

Juraj:
esp-01 has only RX, TX, 0 and 2.

yeah

Okay. Then use TX and RX and don't turn on the serial?

OH! I see why you've got it wired that way, you're using the arduino uno just as a serial adapter?

Or use an ESP8266 in a module that isn't so lousy? WeMos D1 Mini clones are like $3 a pop on ebay! All pins broken out, builtin serial adapter, better PCB antenna and a shield.

DrAzzy:
Okay. Then use TX and RX and don't turn on the serial?

OH! I see why you've got it wired that way, you're using the arduino uno just as a serial adapter?

Or use an ESP8266 in a module that isn't so lousy? WeMos D1 Mini clones are like $3 a pop on ebay! All pins broken out, builtin serial adapter, better PCB antenna and a shield.

Alright will check out the WeMos. In the meantime isn't they anything i could to with the code?

DrAzzy:
Okay. Then use TX and RX and don't turn on the serial?

OH! I see why you've got it wired that way, you're using the arduino uno just as a serial adapter?

Or use an ESP8266 in a module that isn't so lousy? WeMos D1 Mini clones are like $3 a pop on ebay! All pins broken out, builtin serial adapter, better PCB antenna and a shield.

Got the Wemos D1 board. Works fine. Thanks for your input