-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Description
Hardware:
Board: ESP32 Dev Module wroom
IDE name: Arduino IDE?
Flash Frequency: 40Mh
PSRAM enabled: no
Upload Speed: 115200
Computer OS: Windows 10
Description:
Hi I have Bme280 ,Bh170 and rtc connected to my I2c on esp32 .
I also have 4 channel 5v coil relay connected using uln2003.
The code works perfect . When i turn on/off the relay at high frequency or occasionally the i2c sensors start sending garbage values BH170 will send zero and BMe 280 humidity will go to 100% . Analog sensors will keep on sending data and relays will keep on responding and occasionally while flipping the relays on/off the I2c might recover and start sending proper data.
Do not understand why it is happening . Trying to put mosfet irfz44n to reset the i2c.
Sketch:
#include <MQ135.h>
/*
@ harshit --> AIR-GROW
*/
#include <FS.h>
#include <PubSubClient.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#include <SPIFFS.h>
#endif
#include <DNSServer.h>
#if defined(ESP8266)
#include <ESP8266WebServer.h>
#else
#include <WebServer.h>
#endif
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <BH1750.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFiClient.h>
#include <ESPmDNS.h>
#include <Update.h>
//#include <SparkFunCCS811.h>
#include <ArduinoOTA.h>
#include <EEPROM.h>
BH1750 lightMeter;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
WebServer server(81);//81 port handles OTA
char mqtt_server[] = "159.89.168.123";
//char mqtt_server[40] = "demo.thingsboard.io";
char mqtt_port[6] = "1883";
char token[33] = "harshit1";
const char* host = "airgrow"; //for OTA airgrow.local:81
/////////////////////////////////////////////////////////////////
/// The load resistance on the board
#define RLOAD 10.0
/// Calibration resistance at atmospheric CO2 level
#define RZERO 76.63
/// Parameters for calculating ppm of CO2 from sensor resistance
#define PARA 116.6020682
#define PARB 2.769034857
/// Parameters to model temperature and humidity dependence
#define CORA 0.00035
#define CORB 0.02718
#define CORC 1.39538
#define CORD 0.0018
/// Atmospheric CO2 level for calibration purposes
#define ATMOCO2 397.13
/*
@HARSHIT -> Relay pin
*/
#define GPIO18 18
#define GPIO19 19
#define GPIO23 23
#define GPIO05 05
// We assume that all GPIOs are LOW
boolean gpioState[] = {false, false, false, false};
// Timer execution flag
bool set_action = false;
// Timer handler
hw_timer_t * timer = NULL;
unsigned long noWifi = 0;
unsigned long noServer = 0;
int i2cError = 0;
int gpioPins[] = { 18, 19, 23, 5 };
/*
@HARSHIT -->touch pins
*/
int threshold = 40;
bool touch0detected = false;
bool touch3detected = false;
bool touch4detected = false;
bool touch5detected = false;
void gotTouch0() {
touch0detected = true;
}
void gotTouch3() {
touch3detected = true;
}
void gotTouch4() {
touch4detected = true;
}
void gotTouch5() {
touch5detected = true;
}
int i2cPower = 34;
//////////////////////////////////////////////////////////////
/*
@HARSHIT -->proximity sensor
/
int sensor = 16;
int relayInput = 39;
int val = 0; //to store sensor status
int state = LOW; //by default no motion detected
////////////////////////////////////////////////////////
/
@HARSHIT -->water temperature
/
#define ONE_WIRE_BUS 17
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempC = 0.0;
/////////////////////////////////////////////////////////
/
@HARSHIT -->ultraSonic
*/
const int trigPin = 27;
const int echoPin = 26;
long duration;
int distance;
bool setRelay = false;
int relay1Address = 4;
int relay2Address = 5;
int relay3Address = 6;
/////////////////////////////////////////////////////////
/*
@HARSHIT -->BME280
/
#define I2C_SDA 21
#define I2C_SCL 22
#define SEALEVELPRESSURE_HPA (1013.25)
#define BME280_ADD 0x76
#define co2Zero 55
Adafruit_BME280 bme; // I2C
/////////////////////////////////////////////////////////
/
@HARSHIT -->CCS0811
*/
#define CCS811_ADDR 0x5A //Default I2C Address
//CCS811 myCCS811(CCS811_ADDR);
MQ135 mq135_sensor = MQ135(35);
/////////////////////////////////////////////////////////
unsigned long lastSend;
String temperature = "10", humidity = "10", altitude = "10", pressure = "10", light = "10", co = "10", ppm = "10", touch0, touch3, touch4, touch5, pir;
//flag for saving data
bool shouldSaveConfig = false;
/*
@HARSHIT
/
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
/
@HARSHIT OTA-UI
*/
const char* loginIndex = "
"""
""
"
Air-Grow Login Page" " "
const char* serverIndex = "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>" progress: 0% ""<script>" "$('form').submit(function(e){" "e.preventDefault();" "var form = $('#upload_form')[0];" "var data = new FormData(form);" " $.ajax({" "url: '/update'," "type: 'POST'," "data: data," "contentType: false," "processData:false," "xhr: function() {" "var xhr = new window.XMLHttpRequest();" "xhr.upload.addEventListener('progress', function(evt) {" "if (evt.lengthComputable) {" "var per = evt.loaded / evt.total;" "$('#prg').html('progress: ' + Math.round(per*100) + '%');" "}" "}, false);" "return xhr;" "}," "success:function(d, s) {" "console.log('success!')" "}," "error: function (a, b, c) {" "}" "});" "});" "</script>"; ////////////////////////////////////////////////////////// digitalWrite(GPIO18, HIGH); if (!EEPROM.begin(64)) bool r1State = (int)EEPROM.read(relay1Address) == 1 ? true : false; /* /* ////////////////////////////////////////////////////////// // locate devices on the bus // report parasite power requirements /////////////////////////////////////////////////////////// //read configuration from FS json if (SPIFFS.begin()) {
} else { Serial.println(token); WiFiManager wifiManager; wifiManager.setSaveConfigCallback(saveConfigCallback); //add all your parameters here wifiManager.setMinimumSignalQuality(); //if you get here you have connected to the WiFi //read updated parameters //save the custom parameters to FS
} Serial.println("local ip"); ArduinoOTA.setHostname("myesp32"); // No authentication by default /* /use mdns for host name resolution/ //////////////////////////////////////////////////////////////////////////////// client.connect("ID", token , NULL);
} /* /* void reconnect() { } /* Serial.println("On message"); char json[length + 1]; Serial.print("Topic: "); // Decode JSON request if (!data.success()) // Check request method if (methodName.equals("getGpioStatus")) { // Start the timer /* /* void set_gpio_status(int pin, boolean enabled, int flag) {
} else if (pin == GPIO19) { } void loop() { client.loop(); //////////////////////////////////////////////////////////////////////////////////
// uint16_t lux = lightMeter.readLightLevel(); /* Serial.print("Pressure = "); Serial.print(bme.readPressure() / 100.0F); Serial.print("Approx. Altitude = "); Serial.print("Humidity = "); if (t < 0.0 ) { Serial.println(); } void printInfoSerial() //void printDriverError( CCS811Core::status errorCode ) //////////////////////////////////////////////////////////////////////////////////////////////// // Send payload void payload_data() char attributes1[payload1.length() + 1]; void readCO2() { // display.clearDisplay(); //clear display @ beginning of each loop for (int x = 0; x < 10; x++) { //samplpe co2 10x over 2 seconds for (int x = 0; x < 10; x++) { //add samples together } } /* |