Using esp8266(esp12-e) as a wifi for arduino

Hi. Im new to this arduino thing and originally my project was just using the esp8266(Esp12-e) as a controller and be done with it but unfortunately my esp8266 does not do what it is intended.
My project was supposed to be using an ultrasonic sensor to watch water levels and send the data to thingspeak (and/or Node red). However, the esp8266 just displays either constant 0 or random numbers. I knew that the HC-SRO4 needs 5v and because of that Im using the arduino uno as a power source while the information is sent to the nodemcu. Needless to say i changed the main controller to the arduino uno and the ultrasonic sensor data is being read correctly.


const int trigPin = 7;
const int echoPin = 7;

void setup() {

  Serial.begin(9600);
}

void loop() {


  long duration, inches, cm;


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

  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) {

  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {

  return microseconds / 29 / 2;
}

This is my code for the offline version (yes a modified version of the example pin) and i dont understand how to connect from arduino to thingspeak (and/or Node-red) using the esp8266.
The code for the esp8266 as the main controller and wifi is this

#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
const int trigPin = D3;
const int echoPin = D4;
unsigned long ch_no = channel;
const char * write_api = "api";
const char * auth = "author";
const char * ssid = "wifi";
const char * pass = "Pass";
const char * server = "api.thingspeak.com";
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000;
WiFiClient  client;
long duration1;
int distance1;
void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
  ThingSpeak.begin(client);
  startMillis = millis();  //initial start time
}
void loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration1 = pulseIn(echoPin, HIGH);
  distance1 = duration1 * 0.034 / 2;
  Serial.println(distance1);

  currentMillis = millis();
  if (currentMillis - startMillis >= period)
  {
    ThingSpeak.setField(1, distance1);
    ThingSpeak.writeFields(ch_no, write_api);
    startMillis = currentMillis;
  }
}

Any advice is highly appreciated

I think it should run fine on 3.3V though. It's rated for 2.8 to 5.5V.

So you probably had a coding or connection error.
I'd fix that instead of going the roundabout way of connecting to microcontrollers together while one suffices.

Go back to the situation with just ESP8266 and fix that. It can work, no doubt. It's just a matter of fixing whatever you did wrong there.

This works ??

anyway i am with @rsmls on the matter. The whole thing should work on the ESP as well and it is way less complicated to do it on a single processor.

In general, never use a micro conytoller as a power source.3

The datasheet i found specifies 5v though, but i am fairly sure that 3.3v logic levels will suffice, but the echo-pin will also return 5v logic levels which will need to be divided.

fairly sure you can tap into the 5v supply coming from the USB port without to much issue, and
without complex level shifters a simple voltage divider should be able to drop the logic level from the echo pin down, though you should use a different GPIO pin. D3 & D4 are gpio 0 & 2, neither of which can be connected to GND (as voltage dividers usually are) at boot to select the proper boot mode.
edit : here it states 3.3v to 5v operation voltage so it really should be easy enough.

Yeah, although apparently, there have been versions for which the Vcc situation was less straightforward: HC-SR04 | David Pilling
I'd expect anything being sold at this point would be 3.3V proof - but who knows what's still lingering in desk drawers.

mb mb when I just copy pasted the trigpin to echopin and i forgot to change it into 8.
But yeah it works i did this to check whether my stuff works

yeah... my country usually have old stocks and I was confused on how people in youtube managed to make it work with the esp even directly connecting to one another

I... had a hunch that I may need to divide the voltage cus the esp can only safely handle 3.3v but didn't follow through. I am going to try doing so.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.