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 😊