Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is most likely no translated for the post!




Need design and cod...
 
Share:
Notifications
Clear all

Need design and code for multiple sensors using Blynk and ESP8266

Page 1 / 2

(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

I’m working on a project that I can’t seem to get any help on that works from any Arduino forum on the internet. I think it is simple enough for someone who knows what they are doing with Arduino but too complex for me as a beginner. I’ve spent several months on this and I’m at a standstill. The project is making 4 sensors work with ESP8266 on Blynk. DHT11, MQ-135 Gas, HC-SR501 Motion and an EMF Detector (of any config). That’s it. Thank you.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1891
 

Hi Locomojo,

I have to admit that I have never used Blynk, so I'd look at that one, once the 4 sensors work (use Serial.print to temporary dump the data to the serial monitor). 

It seems that the sensors are straight forward (example DHT11, MQ-135, HC-SR501, EMF Detector).
Did you get these to work in a single sketch?

I'd recommend making individual functions for each sensor.
If you have code; please post it and let us know where you're getting stuck 😊 


ReplyQuote
(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

@hans I have code that gets the temp and gas to work but not the motion. The motion sensor is not responding in Blynk. I did not write the code I'm using. I do not have code or design for the EMF detector. I posted in other forums but the replies were very rude with more negative comments than actual help. Thank you!

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

/******************* Pin Definition ***************************/

//Relays for switching appliances
#define Relay1 D6
#define Relay2 D2
#define Relay3 D1
#define Relay4 D5

//DHT11 for reading temperature and humidity value
#define DHTPIN D7

//buzzer to use it for alert
#define buzzer D0

//Selection pins for multiplexer module to switch between different sensors and give data on a single analog pin.
#define S0 D3
#define S1 D4

//Analog pin to read the incoming analog value from different sensors.
#define analogpin A0

int proximity;
int light;
int gas;
int motion;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "yourblynkauthenticationtoken";

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourWiFiSSID";
char pass[] = "YourWiFiPassword";

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, proximity);
  Blynk.virtualWrite(V3, gas);
  Blynk.virtualWrite(V4, motion);
  Blynk.virtualWrite(V5, light);
}

void setup() {
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(buzzer, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000 L, sendSensor);
}

void loop() {
  // Now we can publish stuff!
  // Address 00
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  gas = analogRead(analogpin);
  Serial.print("Gas - ");
  Serial.println(gas);

  // Address 10
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  motion = analogRead(analogpin);

  if (motion > 512) {
    motion = 1;
  } else {
    motion = 0;
  }

  // Address 01
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  proximity = analogRead(analogpin);
  Serial.print("Proximity - ");
  Serial.println(proximity);

  if (proximity < 512) {
    proximity = 1;
    digitalWrite(buzzer, HIGH);
  } else {
    proximity = 0;
    digitalWrite(buzzer, LOW);
  }

  // Address 11
  digitalWrite(S0, HIGH);
  digitalWrite(S1, HIGH);
  int raw_light = analogRead(analogpin);
  light = map(raw_light, 1024, 0, 0, 100);
  Serial.print("Light - ");
  Serial.println(light);

  Blynk.run();
  timer.run();
}

 

This post was modified 4 weeks ago by Hans

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1891
 

Hi Locomojo,

I do not condone folks being rude, and in this forum it will not be tolerated - so no worries. 😊 

The main problem is that you do not provide much information, and reading the code (which I'm trying to do right now) does take up quite some time, to see if and what may be going wrong.

For example: I do not see the wiring used with this sketch, however, I do see in the code some reference to a multiplexer (which one?). I haven't used multiplexers, but from what I know, it is being used to use multiple devices on one pin (for example).

Second issue is that you'd probably be better off building the code one step (sensor) at a time, so you can easier debug potential issues and get a better understanding of each section. 

Having said that, some additional comments would be easier for a third party to read, for example, the section in void loop() is a little confusing to me.
For address 00  things are obvious, it is for the gas detector, so I write a comment with that like so:

  // Now we can publish stuff!
  // Address 00 - MQ 135 Gas detector
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  gas = analogRead(analogpin);
  Serial.print("Gas - ");
  Serial.println(gas);

 

This will make the code more readable for other developers/users.

I'd probably put each sensor detecting in a separate function and have SendSensor call these functions, and change/add a few of the define's.

For example something like this:

int ReadGas() {
  int gas;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);

  // Read analog pin value
  gas = analogRead(analogpin);

  // Debug output via Serial Monitor
  Serial.print("Gas - "); Serial.println(gas);

  return gas;
}

 

And an example on how to change the defines:

I changed "S0", "S1" and "analogPin" to names that are more clear and are easier to read (Multiplexer_Pin1 and 2, Multiplexer_DataPin).
When using "define", the longer names do not take up more memory in your sketch, but the code becomes much more readable, and if you have to change something, then all these "settings" can right away be done in the beginning of the code.

// Multiplexer pins
#define Multiplexer_Pin1 D3
#define Multiplexer_Pin2 D4
#define Multiplexer_DataPin A0

// Mulitplexer addresses
#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW

...

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);

  pinMode(buzzer, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

...

int ReadGas() {
  int gas;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);

  // Read analog pin value
  gas = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Gas - "); Serial.println(gas);

  return gas;
}

 

Another example:

// Multiplexer pins
#define Multiplexer_Pin1 D3
#define Multiplexer_Pin2 D4
#define Multiplexer_DataPin A0

...

// Mulitplexer addresses
#define Multiplexer_Motion1 HIGH
#define Multiplexer_Motion2 LOW

...

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);

  pinMode(buzzer, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

...

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Motion - "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}

 

With these 2 examples, I'd start writing a sketch that just outputs to the serial monitor, and leave Blynk out of the equation for now.
Once you see all these functions producing sensible data, then we can move on to the use of Blynk.

Ideally, I'd want a individual function for each of the sensors, so in the end we can do something like this in the SendSensor function:

void sendSensor()
{
  Blynk.virtualWrite(V0, ReadTemperature() );
  Blynk.virtualWrite(V1, ReadHumidity() );
  Blynk.virtualWrite(V2, ReadProximity() );
  Blynk.virtualWrite(V3, ReadGas() );
  Blynk.virtualWrite(V4, ReadMotion() );
  Blynk.virtualWrite(V5, ReadLight() );
}

 

Note: I haven't tested if placing a function in the "Blynk.virtualWrite" is allowed or not.
If not, then we can catch the values in a variable (which may be needed for temperature and humidity).

I've spend about an hour now trying to read and understand the code, so this could use some re-organizing, and is probably the reason why some response you've gotten on other forums were not that nice (personally I'd prefer folks to not respond at all, if they feel this is too much work for them, versus being rude).
Eg. I see some reference to relay, but they do not seem to be used, and I have no idea what raw_light is, or what proximity versus motion is.

Note: when using libraries, it is always a good idea to add a comment, which one / where to find it.
While trying to find the DHT library I found quite a few with the same or similar names. (not your fault - the original programmer forgot to mention this)

Since this is not your own code, which is really not uncommon amongst Arduino users, I'd really recommend starting from scratch, while keeping an eye on this sketch of course. Not having some of the sensors and the multiplexer myself, and not being a Blynk user (yet), makes it difficult to test and/or make a working sketch.

An example test sketch, which compiles but has not been tested, and where I removed anything Blynk related (which we will add later):

Note: D0, D3, D4 and D7 were not recognized for my Arduino. You may have to change them back for your ESP8266.

#include <DHT.h>

// ---- Pin Definition ----

//DHT11 for reading temperature and humidity value
#define DHTPIN 7 // my Arduino didn't like D7, so I used 7

//buzzer to use it for alert
#define Buzzer 0 // my Arduino didn't like D0, so I used 0

// ---- Multiplexer Definition ----

// Pins to control multiplexer addressing
#define Multiplexer_Pin1 3 // my Arduino didn't like D3, so I used 3
#define Multiplexer_Pin2 4 // my Arduino didn't like D4, so I used 4

//Analog pin to read the incoming analog value from different sensors.
#define Multiplexer_DataPin A0

// Predefined multiplexer addresses vs sensors
#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW

#define Multiplexer_Motion1 HIGH
#define Multiplexer_Motion2 LOW

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  // Debug console
  Serial.begin(9600);
  
  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);
  pinMode(Buzzer, OUTPUT);
}

void loop()
{
  sendSensor();
  delay(1000);
}

int ReadGas() {
  int gas;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);

  // Read analog pin value
  gas = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Gas Sensor - "); 
  Serial.println(gas);

  return gas;
}

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor - "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}

// Sends sensor data to Blynk.
// For testing/debugging we're sending this to the serial monitor instead
void sendSensor()
{
  Serial.print('GAS = ');
  Serial.println(ReadGas());

  Serial.print('TEMPERATURE = ');
  Serial.println(dht.readTemperature(true)); // temp in F

  Serial.print('HUMIDTY = ');
  Serial.println(dht.readHumidity());

  Serial.print('MOTION = ');
  Serial.println(ReadMotion());
}

 

I hope this will be a good starting point to get a clean and working sketch 😊 

This post was modified 4 weeks ago by Hans

ReplyQuote
(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

Hi Hans,

I deeply appreciate your evaluation and your time! I apologize for not providing enough info. The project came from here https://techiesms.in/iot-projects/nodemcu-projects/multiple-appliances,-multiple-sensors,-multiple-projects/      It was the closest to what I needed. I had not used Blynk before and I was unsure how to integrate it as far as coding and the setup of the Blynk gauges. The direction the creator took with project seemed unusual, which made it more difficult to problem solve. For instance, I was unfamiliar with the setting in the motion sensor code “if (proximity < 512).” I could not find anywhere what 512 represented. I assumed sensitivity but I could not find anyone using that sensor with that in their code.

I believe a proximity sensor goes off when you get close to it and a motion sensor detects any motion. I do not need the proximity because it seemed redundant.  I assume raw light means there is nothing between the sensor and the light unlike a motion sensor which has a plastic dome over the sensor, which can be removed.

You make valid points. I have been doing Arduino for about 3 years but I’m still a novice. I totally get confused when someone writes code that doesn’t make sense but I assume they know better than me because if they posted it, I assume it works and the failure is on my part.

I’ll be working on this all week, and applying your recommendations. I’m happy to continue this. I’ll follow up with you. Thank you again Hans!


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1891
 

No worries 😊 - I'm happy to help where I can.

Most Arduino projects start like that; a project from another person modified to what we need.
I started the same way, and as I did more and more projects I started more and more starting from scratch after having played with a project from another person 😊 

As for the magical "512" - The analog pin will return a value from 0 - 1024 (see the Analog Pin description on the Arduino website). 512 is an arbitrary number the other programmer selected for his/her purposes. This is where outputting the data to the Serial Monitor can be handy to see what values you're observing.

Feel free to ask if you have questions, and remember; we all had to start at some point 😉 


ReplyQuote
(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

Hello, I just wanted to post an update. I spent all last week on this and ran into issues with the voltage regulator overheating and had to switch gears as far as the power goes. This is part of a bigger project and I was concerned about the heat so I switched to usb power. After making the suggested changes, which I thought were excellent, and adding some of my own, I do have a few code errors, mostly “dht” errors. I’ve tried everything that I have knowledge of but I can’t resolve it. I was hoping you could do a check?  I hope I haven’t botched the whole thing. Thank you!

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


/********************** Pin Definition ******************************/

//Relays for switching appliances
#define Relay1            D6
#define Relay2            D2
#define Relay3            D1
#define Relay4            D5

#define DHTPIN            D2

//buzzer to use it for alert
#define buzzer            D0

#define Multiplexer_Pin1 D3
#define Multiplexer_Pin2 D4
#define Multiplexer_DataPin A0

#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW
#define Multiplexer_Motion1 HIGH
#define  Multiplexer_Motion2 LOW

int proximity;
int light;
int gas;
int motion;
int ReadMotion()
  
 #define DHTTYPE DHT11 // DHT 11
{
DHT dht(DHTPIN, DHT11);
BlynkTimer timer;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
char auth[] = "";
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
(
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, proximity);
  Blynk.virtualWrite(V3, gas);
  Blynk.virtualWrite(V4, motion);
  Blynk.virtualWrite(V5, light);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);
  pinMode(buzzer, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  sendSensor();
  delay(1000);
}

 int ReadGas() {
  int gas;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);

  // Read analog pin value
  gas = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Gas Sensor - "); 
  Serial.println(gas);

  return gas;
}
  int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor - "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}



// Sends sensor data to Blynk.
// For testing/debugging we're sending this to the serial monitor instead
void sendSensor()
{
  Serial.print('GAS = ');
  Serial.println(ReadGas());

  Serial.print('TEMPERATURE = ');
  Serial.println(dht.readTemperature(true)); // temp in F

  Serial.print('HUMIDTY = ');
  Serial.println(dht.readHumidity());

  Serial.print('MOTION = ');
  Serial.println(ReadMotion());
}

  Blynk.run();
  timer.run();
}
 

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1891
 

Normally I'd right away ask to copy and paste the error messages (which makes it easier to find what is going wrong), but when looking at you code I see that this may be why it is going wrong:

 #define DHTTYPE DHT11 // DHT 11
{
DHT dht(DHTPIN, DHT11);

 

I think you have to remove the accolade ( { ) here.

Hope this fixes it 😁 


ReplyQuote
(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

Thanx! That worked. I had to eliminate some of what you changed because there were issues. This is what I have but it is still untested with the sensors. At the moment, there is one error 

'Blynk' does not name a type

// Sends sensor data to Blynk.
// For testing/debugging we're sending this to the serial monitor instead


  
  Blynk.run();
  
  timer.run();
}
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

int gas;
int motion;

// ---- Pin Definition ----

//DHT11 for reading temperature and humidity value
#define DHTPIN D7 // my Arduino didn't like D7, so I used 7

//buzzer to use it for alert
#define Buzzer D0 // my Arduino didn't like D0, so I used 0

// ---- Multiplexer Definition ----

// Pins to control multiplexer addressing
#define Multiplexer_Pin1 D3 // my Arduino didn't like D3, so I used 3
#define Multiplexer_Pin2 D4 // my Arduino didn't like D4, so I used 4

//Analog pin to read the incoming analog value from different sensors.
#define Multiplexer_DataPin A0

// Predefined multiplexer addresses vs sensors
#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW

#define Multiplexer_Motion1 HIGH
#define Multiplexer_Motion2 LOW

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
char ssid[] = "";
char pass[] = "";
char auth[] = "";
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V3, gas);
  Blynk.virtualWrite(V4, motion);
 
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);
  pinMode(Buzzer, OUTPUT);
}

void loop()
{
  sendSensor();
  delay(1000);
}

int ReadGas() {
  int gas;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);

  // Read analog pin value
  gas = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Gas Sensor - "); 
  Serial.println(gas);

  return gas;
}

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor - "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}

// Sends sensor data to Blynk.
// For testing/debugging we're sending this to the serial monitor instead


  
  Blynk.run();
  
  timer.run();
}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1891
 

For some odd reason, I wasn't able to install the Blynk library for my Arduino IDE.

However ... I have the impression something is missing in that last part of the code. If we look at this:

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor - "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}

// Sends sensor data to Blynk.
// For testing/debugging we're sending this to the serial monitor instead


  
  Blynk.run();
  
  timer.run();
}

 

Then you'll see that this part is not part of anything ... 

// Sends sensor data to Blynk.
// For testing/debugging we're sending this to the serial monitor instead


  
  Blynk.run();
  
  timer.run();
}

 

So I'm guessing you are either missing a function header (something like "void sendData() {") or you have an accolade too many in the ReadMotion function ...

So this should either be:

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor - "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }

  // Sends sensor data to Blynk.
  // For testing/debugging we're sending this to the serial monitor instead

  Blynk.run();
  
  timer.run();
}

 

Or ... (just made up a random function name):

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor - "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}

void SendSensorData() {
  // Sends sensor data to Blynk.
  // For testing/debugging we're sending this to the serial monitor instead

  Blynk.run();
  
  timer.run();
}

 


ReplyQuote


(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

Everything looks good until it's uploaded and that is when the trouble starts. That last sketch produced nothing that worked properly. If the gauges worked at all they were wonky. I'm so frustrated because I spent over a month on this thing at least 5 hours a day and have nothing to show for it and I have to get this done for a bigger project. I've done everything. I did this with and wo the multiplexer, switched between digital and analogue pins, tested with and wo the serial monitor for results, used leds to show activity off the board for each sensor, tested each sensor, etc. I really didn't think this be that much of a problem. I have more sketches than Leonardo DaVinci! I'm open to any suggestions. 


ReplyQuote
(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

One of the biggest problems with Arduino is that many of the sketches online of finished projects don't work and it makes it that much harder to learn. I've seen code that looks like a mess and it works and that all gets confusing to both write and diagnosis my own projects.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1891
 

The biggest challenge with sketches done by others is indeed "reading" the code.
Everybody thinks in a slightly different way, and everybody is different when it comes to experience - which results in sometimes hard to read code.
That's why, when trying to read code by others, the first thing I do is cleanup the code with Codebeautify (online service).
This makes it that for example indentation is corrected, which is a first step in making code more readable.

Next thing is experience, which takes time, testing, reading, and coding. Something you can't learn over night unfortunately.

Coming back to the last code: I do not know where 

  Blynk.run();
  
  timer.run();
}

 

should be placed. It is outside of all functions, which makes it problematic.

After doing some Googling, I found that Blynk.run() needs to be called frequently in the void loop().

I just did an attempt to clean up the code, but then realized, the WiFi connection isn't initialized either, so Blynk wouldn't work anyway (no network connection).
I assume you've collected bits and pieces from other projects and are missing a few vital parts.
Here an example for of Blynk on how to connect. Maybe this is a good base.

Having said that, first thing I'd do is reading the sensors and output the data to the serial port. Once we know this works OK, then add connecting to WiFi. If that works OK as well, then move on and add Blynk.

I do not have a multiplexer laying around and did an initial attempt for the first step, you'll have to test and see if this work by reading the serial output).
The only thing this does is reading the sensors and outputting the results to the serial monitor.

#include <DHT.h>

int gas;
int motion;

// ---- Pin Definition ----

//DHT11 for reading temperature and humidity value
#define DHTPIN D7 // my Arduino didn't like D7, so I used 7

//buzzer to use it for alert
#define Buzzer D0 // my Arduino didn't like D0, so I used 0

// ---- Multiplexer Definition ----

// Pins to control multiplexer addressing
#define Multiplexer_Pin1 D3 // my Arduino didn't like D3, so I used 3
#define Multiplexer_Pin2 D4 // my Arduino didn't like D4, so I used 4

//Analog pin to read the incoming analog value from different sensors.
#define Multiplexer_DataPin A0

// Predefined multiplexer addresses vs sensors
#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW

#define Multiplexer_Motion1 HIGH
#define Multiplexer_Motion2 LOW

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 

DHT dht(DHTPIN, DHTTYPE);

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  Serial.pintln("----- Start Sending data -----");
  
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) 
  {
    Serial.println("Failed to read from DHT sensor!");
  }
  else
  {
    Serial.print("t: ");
    Serial.println(t);
  
    Serial.print("h: ");
    Serial.println(h);
  }
  
  Serial.print("Gas: ");
  Serial.println(gas);

  Serial.print("Motion: ");
  Serial.println(motion);
  
  Serial.pintln("----- Done Sending data -----");
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  
  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);
  pinMode(Buzzer, OUTPUT);
}

void loop()
{
  ReadGas();
  ReadMotion();
  
  sendSensor();
  delay(1000);
}

int ReadGas() {
  int gas;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);

  // Read analog pin value
  gas = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Gas Sensor: "); 
  Serial.println(gas);

  return gas;
}

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor: "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}

 

This may need a little fine tuning, but I suspect it may run well. Again: do not have a multiplexer, so I cannot test this.

Once this works, we'll add the basics of settings up a WiFi connection with the ESP - which is relatively easy and should look something like this:

#include <DHT.h>
#include <ESP8266WiFi.h>

int gas;
int motion;

// ---- Pin Definition ----

//DHT11 for reading temperature and humidity value
#define DHTPIN D7 // my Arduino didn't like D7, so I used 7

//buzzer to use it for alert
#define Buzzer D0 // my Arduino didn't like D0, so I used 0

// ---- Multiplexer Definition ----

// Pins to control multiplexer addressing
#define Multiplexer_Pin1 D3 // my Arduino didn't like D3, so I used 3
#define Multiplexer_Pin2 D4 // my Arduino didn't like D4, so I used 4

//Analog pin to read the incoming analog value from different sensors.
#define Multiplexer_DataPin A0

// Predefined multiplexer addresses vs sensors
#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW

#define Multiplexer_Motion1 HIGH
#define Multiplexer_Motion2 LOW

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 

// ----- WiFi details ----
const char* ssid = "YourNetworkName";
const char* pass = "YourPassword";


DHT dht(DHTPIN, DHTTYPE);

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  Serial.pintln("----- Start Sending data -----");
  
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) 
  {
    Serial.println("Failed to read from DHT sensor!");
  }
  else
  {
    Serial.print("t: ");
    Serial.println(t);
  
    Serial.print("h: ");
    Serial.println(h);
  }
  
  Serial.print("Gas: ");
  Serial.println(gas);

  Serial.print("Motion: ");
  Serial.println(motion);
  
  Serial.pintln("----- Done Sending data -----");
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  
  connectWiFi();
  
  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);
  pinMode(Buzzer, OUTPUT);
}

void loop()
{
  // Reconnect WiFi if needed
  if (WiFi.status() != WL_CONNECTED) {
    connectWiFi();
    return;
  }

  ReadGas();
  ReadMotion();
  
  sendSensor();
  delay(1000);
}

int ReadGas() {
  int gas;

  // Switch mulitplexer to address 00 for the MQ 135 Gas Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);

  // Read analog pin value
  gas = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Gas Sensor: "); 
  Serial.println(gas);

  return gas;
}

int ReadMotion() {
  int motion;

  // Switch mulitplexer to address 00 for the Motion Detector
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);

  // Read analog pin value
  motion = analogRead(Multiplexer_DataPin);

  // Debug output via Serial Monitor
  Serial.print("Read Motion sensor: "); 
  Serial.println(motion);

  if(motion>512) {
    return 1;
  } else {
    return 0;
  }
}

// This function tries to connect to your WiFi network
void connectWiFi()
{
  Serial.print("Connecting to ");
  Serial.println(ssid);

  if (pass && strlen(pass)) {
    WiFi.begin((char*)ssid, (char*)pass);
  } else {
    WiFi.begin((char*)ssid);
  }

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println('\n');
  Serial.println("WiFi Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
}

 

As you can see, I did take some of the parts of the Blynk example I mentioned before.
Once you see "WiFi Connection established!" and it lists an IP address, then we're good to go for the next step: adding Blynk.

It is important to test each aspect before trying to add everything in a single sketch.
Let me know if these 2 steps work - then in a next reply I'll try to add Blynk.

As for microcontrollers; the ESP's are very powerful, especially compared to the Arduino models, so I'd stick with those.

It will take some effort to get this to work - just hang in there and do not give up just yet 😊  - I know it can be frustrating!


ReplyQuote
(@locomojo)
Active Member
Joined: 4 weeks ago
Posts: 14
Topic starter  

Thank you. I will be going over all this today. I work on this everyday 😫 I do have a Wifi connection with Blynk, I just took out the credential info when I posted my code. The Blynk example is interesting and I will have to study it. It's different than other examples I've seen.

I sort of understand how the multiplexer works but this is my first time using it and so troubleshooting on that end is difficult. I did notice on one sketch when I ran it the gas sensor triggered the motion sensor and that seemed like maybe the issue could be on the multiplexer end, not necessarily the unit but maybe the communication in the code.

The serial monitor results have been consistent with the Blynk results as far as activity or no activity. Each sensor has been tested with it's own sketch so I know they all work and show results. Putting it all together is the problem but I'll keep trying.

 

 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1891
 

I understand how frustrating it can be. But as we move forward, I'm sure we will get this to work.
Since we have to deal with a few aspects (sensors, wifi, blynk), I always tend to break up the "problem" in each part, making sure each part actually works.

Unfortunately, I do not have the sensors and multiplexer, but I'd be happy to help where I can 😊 

If sensors and wifi work, then we can add Blynk 😊 


ReplyQuote


Page 1 / 2

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: