Wemos D1 Mini with Multiplexer

I tried to use a 4051 analog multiplexer to increase my ADC port on a ESP8266. The setup works in Arduino IDE but I’m unable to port it to Blynk. Whenever I put 2 or more virtualWrite in the loop, the ESP8266 keep resetting and disconnecting from the network. Any help will be very much appreciated. Here’s my setup and my code

/**************************************************************
   Blynk is a platform with iOS and Android apps to control
   Arduino, Raspberry Pi and the likes over the Internet.
   You can easily build graphic interfaces for all your
   projects by simply dragging and dropping widgets.

     Downloads, docs, tutorials: http://www.blynk.cc
     Blynk community:            http://community.blynk.cc
     Social networks:            http://www.fb.com/blynkapp
                                 http://twitter.com/blynk_app

   Blynk library is licensed under MIT license
   This example code is in public domain.

 **************************************************************
   This example runs directly on ESP8266 chip.

   You need to install this for ESP8266 development:
     https://github.com/esp8266/Arduino

   Please be sure to select the right ESP8266 module
   in the Tools -> Board menu!

   Change WiFi ssid, pass, and Blynk auth token to run :)

 **************************************************************/
//#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "***********"; //insert here your token generated by Blynk

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, SSID, password);

  pinMode(5, OUTPUT); //s0
  pinMode(6, OUTPUT); //s1
  pinMode(7, OUTPUT); //s2
}

void pin1() //y0
{
  digitalWrite(5, 0); 
  digitalWrite(6, 0);
  digitalWrite(7, 0);
  Blynk.virtualWrite(V0, analogRead(A0));
}

void pin2() //y1
{
  digitalWrite(5, 1);
  digitalWrite(6, 0);
  digitalWrite(7, 0);
  Blynk.virtualWrite(V1, analogRead(A0));
}

void pin3() //y2
{
  digitalWrite(5, 0);
  digitalWrite(6, 1);
  digitalWrite(7, 0);
  Blynk.virtualWrite(V2, analogRead(A0));
}

void loop()
{
  Blynk.run();

  pin1();     //read sensor value from y0
  delay(500);
  pin2();     //read sensor value from y1
  delay(500);
  pin3();     //read sensor value from y2
  delay(500);
}

(P/S please don’t be too harsh with comments, I’m still a newbie without any electronic background)

@William_Lim_Wei_Yap that is because your loop is all wrong. ESP’s and Blynk don’t like delays so look up SimpleTimer as a replacement which is one of the four libraries you should have installed when you set up your system for Blynk.

Then ensure you just have 2 lines in loop relating to Blynk and Simple Timer so the 3 functions get called at 500ms intervals with SimpleTimer. You will be good to go then.

Thanks for your reply Costas. I’ve changed my code to this

    #define BLYNK_PRINT Serial  
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>

    // the timer object
    SimpleTimer timer;

    void setup()
    {
      Serial.begin(9600);
      Blynk.begin(auth, ssid, pass);
      
      timer.setInterval(500, readPin);
      
      pinMode (5, OUTPUT); //s0
      pinMode (6, OUTPUT); //s1
      pinMode (7, OUTPUT); //s2
    }

    void readPin()
    {
      //read from pin y0
      digitalWrite (5, 0);
      digitalWrite (6, 0);
      digitalWrite (7, 0);
      Blynk.virtualWrite(V0, analogRead(A0));

      //read from pin y1
      digitalWrite (5, 1);
      digitalWrite (6, 0);
      digitalWrite (7, 0);
      Blynk.virtualWrite(V1, analogRead(A0));

      //read from pin y2
      digitalWrite (5, 0);
      digitalWrite (6, 1);
      digitalWrite (7, 0);
      Blynk.virtualWrite(V2, analogRead(A0));
    }

    void loop()
    {
      Blynk.run();
      timer.run();
    }

and it still keep connecting and disconnecting. Is it because too many datas being written to the server at the same time?

I think the limit is 100 data items per second and you look to be well below this.

In your first sketch you had:

char auth[] = "***********"; //insert here your token generated by Blynk

which is correct but not in your second. Are you including your token in:

Blynk.begin(auth, ssid, pass);

Before the 3 virtualWrite’s I would send the data to Serial Monitor to be sure it looks ok. As per docs the data type can be string, integer, float, array or raw data.

You might have to do it this way:

  int val = analogRead(A0);    // read the input pin
  Serial.println(val);             // debug value
  Blynk.virtualWrite(V0, val);

What size is the resistor (330R? )you show connected to the multiplexer and are you aware that the AO pin on the WeMos can only accept 3.2V (not 3.3V or more) without a voltage divider?

Oops my bad, I didn’t copy the code fully. The main issue is sometimes I’m able to see some data being transferred to Blynk app, after that there will be a pop up disconnection message.

I’ve modified readPin() according to your suggestion and I also have to change the pin to pin 16, 14 and 12 (For some weird reason, they put D5, D6, D7 but I get some rubbish when I open up serial monitor, so I change my pin number according to esp12 GPIO pinout and it works)

Coding:

void readPin()
{
      //read from pin y0
      digitalWrite (16, 0);
      digitalWrite (14, 0);
      digitalWrite (12, 0);
      val = analogRead(A0);
      Serial.println(val);
      Blynk.virtualWrite(V0, analogRead(A0));

      //read from pin y1
      digitalWrite (16, 1);
      digitalWrite (14, 0);
      digitalWrite (12, 0);
      val = analogRead(A0);
      Serial.println(val);
      Blynk.virtualWrite(V1, analogRead(A0));

      //read from pin y2
      digitalWrite (4, 0);
      digitalWrite (5, 1);
      digitalWrite (0, 0);
      val = analogRead(A0);
      Serial.println(val);
      Blynk.virtualWrite(V2, analogRead(A0));
  
}

Result:

[5111] Connecting to BelongYN3F43G6
[6613] Connected to WiFi
[6614] IP: 192.168.1.120
[6614] Blynk v0.3.7 on ESP8266
[6614] Connecting to blynk-cloud.com:8442
7
8
8
[7221] Ready (ping: 3ms).
5
8
8
9
8
7
9
8
7
9
7
8
8
8
6
8
9
7
6

That’s when I didn’t start the Blynk app… Once I start the blynk app and run on two virtual pin graph, it keep resetting

8
6
6
6
6
7
4
8
6
7
9
8
9
8
6
9
8
9
[28947] Connecting to blynk-cloud.com:8442
6
8
7
8
8
7
7
8
8
8
9
9
9
8
8
6
9
8
7
7
6
8
6
7
6
6
6
6
7
7
8
7
5
5
5
7
6
6
5
4
6
5
6
6
6
5
5
5
[29627] Ready (ping: 17ms).
9
6
6
7
7
7
8
7
8
[37195] Connecting to blynk-cloud.com:8442
8
8
6
7
7
7
6
7
5
6
5
7
6
5
6
6
5
6
5
5
4
6
5
6
5
6
6
5
5
5
5
5
5
5
5
5
5
5
5
5
6
6
6
6
8
7
7
9
[37877] Ready (ping: 18ms).
5
6
5

Running 1 graph in the app is completely fine, but once I started the second graph it just gone crazy. Do you know what’s the issue?

I always use the ESP pins when coding for the WeMos.

We have 2 graphs on the same page of some of our projects and they are find but we only send data at 3 second intervals.

Try increasing the time between virtualWrites to see if that helps and if not provide your latest code.

Thanks for your suggestion. It works fine if I set the frequency of requests to 3s in the app, but not anything less than 3s :neutral_face: but that’s not sufficient for me as I’m trying to make a real time sensor :disappointed:

@William_Lim_Wei_Yap from my own obsevations - if you use pushing and polling at same time - performance of hardware decreased dramatically. In case you use only pushing - performance becomes much better. SO my recommendation would be to avoid “3s in the ap” and make timer on hardware side and don’t use reading widgets. Ot you can buy more powerful hardware of course =)

@William_Lim_Wei_Yap we use PUSH frequency for everything and your sketch can make the push at very short intervals of less than 1 second intervals.

Hi guys, thanks a lot! push frequency went quite well, but still not as good as running directly on single analog. Considering using other boards now. Thanks heaps!