Ethernet.begin(mac,ip) and dfrobot CO2 Sensor

I had a question for dfrobot CO2 Sensor setting.

if i setting Ethernet.begin(mac,ip); in program, CO2 Sensor can sending value to mysql server, just frist time running, CO2 value is normal, the other time running, CO2 value is "0"

I try disable "Ethernet.begin(mac,ip);" this line, CO2 value is normal, but can't write to mysql database.

how can i modify my program......

ericyu0328:
I had a question for dfrobot CO2 Sensor setting.

if i setting Ethernet.begin(mac,ip); in program, CO2 Sensor can sending value to mysql server, just frist time running, CO2 value is normal, the other time running, CO2 value is "0"

I try disable "Ethernet.begin(mac,ip);" this line, CO2 value is normal, but can't write to mysql database.

how can i modify my program......

External power supply (7~12V) is necessary to supply the microcontroller board when you using this CO2 sensor module.

Juraj:
CO2_Sensor_SKU_SEN0159-DFRobot

complement

i'm using this CO2 Sensor

ericyu0328:
how can i modify my program......

posting your code would help us do that!

post my code, thanks a lot!!
temperature & humidity & CO2 display LCD and save to mysql

#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip (192,168,0,150);
EthernetClient client;

#define DHTPIN 2
#define DHTTYPE
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000;
float t = 0;
float h = 0;
String data;
static long CO2;

void setup() {

client.stop();
Serial.begin(9600);
while (!Serial) {

}
mySerial.begin(9600);

lcd.begin(16, 2);
lcd.backlight();

dht.begin();

h = (float) dht.readHumidity();
t = (float) dht.readTemperature();

data = "";
}
void loop(){
lcd.clear();
digitalWrite(LED3, HIGH);
mySerial.write(hexdata,9);
delay(300);

currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
h = (float) dht.readHumidity();
t = (float) dht.readTemperature();
}

mySerial.write(hexdata,9);
delay(500);

for(int i=0,j=0;i<9;i++)
{
if (mySerial.available()>=0)
{
long hi,lo;
int ch=mySerial.read();

if(i==2){ hi=ch; }
if(i==3){ lo=ch; }
if(i==8) {
CO2=hi*256+lo;
Serial.print("CO2 concentration: ");
Serial.print(CO2);
Serial.println("ppm");
}
}
}

//--------LCD Display
lcd.setCursor(0, 0); //Display CO2 Title
lcd.print("CO2:");
lcd.setCursor(8, 0); //Display CO2 Value
lcd.print(CO2);
lcd.setCursor(13, 0); //Display ppm
lcd.print("ppm");
lcd.setCursor(0, 1); //Display Temperature Title
lcd.print("T:");
lcd.setCursor(2, 1); //Display Temperature Value
lcd.print(t);
lcd.setCursor(6, 1); //Display C
lcd.print("C");
lcd.setCursor(9, 1); //Display Humidity Title
lcd.print("H:");
lcd.setCursor(11, 1); //Display Humidity Value
lcd.print(h);
lcd.setCursor(15, 1); //Display %
lcd.print("%");

//--------

data = "temp1=" + String(t) + "&hum1=" + String(h) + "&co2=" + String(CO2);

Ethernet.begin(mac,ip);

if (client.connect("192.168.0.105",80))
client.println("POST /add.php HTTP/1.1");
client.println("Host: 192.168.0.105");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
//----mysql save
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED2, LOW);
}

if (client.connected()) {
client.stop();
}

delay(30000);
}

are you using an ethernet shield?

if yes, what shield?

I suspect that the shield you are using, uses SPI to communicate with your arduino.

If you are using an arduino UNO, the SPI pins are on 10, 11, 12 and 13.

If that is the case, then should change the pins your are using for your softwareserial.

that is in your code: "SoftwareSerial mySerial(10, 11); // RX, TX //change the pins here (in software and hardware of course!)"

hope that helps! :slight_smile:

thanks for your suggestions!!

I'm using "Arduino Ethernet Shield 2 W5500 + arduino uno R3", and Physical connection by ethernet cable to raspberry server.

I try to change 10&11pin to 12&13, but the CO2 value display is "0"

Ethernet shield uses pins 10, 11, 12 and 13. And 4 if you use a SD card. It is not a secret, it is documented.

SoftwareSerial mySerial(10, 11); <--- this line is Manufacturer default code

I'm using :

this CO2 Sensor

ericyu0328:
SoftwareSerial mySerial(10, 11); <--- this line is Manufacturer default code

I'm using :

Infrared_CO2_Sensor_0-50000ppm_SKU__SEN0220-DFRobot

this CO2 Sensor

the manufacturer of the sensor doesn't expect in his example that you will use it with some SPI devices. choose a pair of free pins for the SoftwareSerial

ericyu0328:
I try to change 10&11pin to 12&13, but the CO2 value display is "0"

did you really just do that! what was the point of me telling you:

sherzaad:
If you are using an arduino UNO, the SPI pins are on 10, 11, 12 and 13.

If I'm not mistaken you should have pins 8 and 9 available. USE THOSE.

If they are not available you can always use the Analog pins as digital IO.

and it does not matter what pins the manufacturer used in his example code. they are on the arduino which you have FULL CONTROL on ie you can change the assignment to whatever YOU want to set them to!

Clearly you should look more into how to use the board than just patching codes together...

and btw, it seems to me that you may face the same problem with your DHT sensor.
"#define DHTPIN 2", your also using pin 2 for your LCD!

and what is this?!
"#define DHTTYPE"

should be one of those if I'm not mistaken
#define DHTTYPE DHT11 // DHT 11
OR
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
OR
#define DHTTYPE DHT21 // DHT 21 (AM2301)