Adding a delay to I2C sensor run thru multiplexer

Your problem will be that there is no way to accurately test your code without the hardware that you are using - including the multiplexer and the sensors.
Did parts of this code, in particular the void tcaselect(uint8_t i) function, come from somewhere else?
If so, then seeing the source code may help me to understand how it should work in theory.

Pete.

It came from this link

Okay, I’ll take a look at that, and your code, later and see if I can put something together for you to test.

Pete.

I know nothing about this multiplexor, or your sensors and their associated libraries, but I’ve had a stab at re-writing your code.

To keep it simple, and because you have three different types of sensors that need to be handled in different ways, I’ve gone for the ‘longhand’ approach rather than parameterising the calls to the multiplexor library. This should make it easier for you to follow and debug…

#define BLYNK_PRINT Serial
#include <Wire.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>

#include <Adafruit_MCP9808.h>
#include <Adafruit_Si7021.h>
#include "HX711.h"

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

// Set password to "" for open networks.
char ssid[] = "Bxxxxxx";
char pass[] = "Fxxxxxxxxxxxxx";

int reading_frequency = 5000; // How often (in milliseconds) should we take the readings

#define calibration_factor -4800.0 

#define DOUT  2  //#define scale DT (2)
#define CLK  3   //#define scale CL (3)

HX711 scale;

#define TCAADDR       0x70

#define TEMP1_ADDR    0x18
#define TEMP2_ADDR    0x18
#define TEMP3_ADDR    0x18
#define HUM1_ADDR     0x40
#define HUM2_ADDR     0x40


// Assign unique ID to each sensor
Adafruit_MCP9808 temp1 = Adafruit_MCP9808();
Adafruit_MCP9808 temp2 = Adafruit_MCP9808();
Adafruit_MCP9808 temp3 = Adafruit_MCP9808();
Adafruit_Si7021 hum1 = Adafruit_Si7021();
Adafruit_Si7021 hum2 = Adafruit_Si7021();
//HS711 scale1 = HS711(6)

float temperature1;
float temperature2;
float temperature3;

float humidity1;
float humidity2;

float weight;

long int loadcell;
long int tare;

// Initialise the Blynk timer object
BlynkTimer timer;


void tcaselect(uint8_t i)
{
  if (i > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}


void initialise_temp_sensors()
{
  // This function is called from void setup and initialises your temperature and weight sensors
  
  tcaselect(7);
  if (!temp1.begin(0x18))
  {
    Serial.println("Couldn't find Temp1! Check your connections and verify the address is correct.");
    while (1);
  }
  Serial.println("Found MCP9808, Sensor 1!");
  temp1.setResolution(0); // sets the resolution mode of reading, the modes are defined in the table bellow:


  tcaselect(6);
  if (!temp2.begin(0x18))
  {
    Serial.println("Couldn't find Temp2! Check your connections and verify the address is correct.");
    while (1);
  }
  Serial.println("Found MCP9808, Sensor 2!");
  temp2.setResolution(0); 


  tcaselect(5);
  if (!temp3.begin(0x18))
  {
    Serial.println("Couldn't find Temp3! Check your connections and verify the address is correct.");
    while (1);
  }
  Serial.println("Found MCP9808, Sensor 2!");
  temp3.setResolution(0);


  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
}


void take_readings()
{
  // This function is called by a timer and takes reading from each sensor nad prints them, as well as sending them to Blynk
  
  // Take temp1 readings...
  tcaselect(7);
  temperature1=temp1.readTempF();
  Serial.print("temperature1 = ");
  Serial.println(temperature1);
  Serial.println();   
  Blynk.virtualWrite(V7,temperature1); 

  Blynk.run();

  // Take temp2 readings...
  tcaselect(6);  
  temperature2=temp2.readTempF();
  Serial.print("temperature2 = ");
  Serial.println(temperature2);
  Serial.println();   
  Blynk.virtualWrite(V6,temperature2); 

  Blynk.run();

  // Take temp3 readings...
  tcaselect(5);
  temperature3=temp3.readTempF();
  Serial.print("temperature3 = ");
  Serial.println(temperature3);
  Serial.println();   
  Blynk.virtualWrite(V5,temperature3);

  Blynk.run();

  // Take hum1 readings...
  tcaselect(4);
  humidity1=hum1.readHumidity();
  Serial.print("humidity1 = ");
  Serial.println(humidity1);
  Serial.println();   
  Blynk.virtualWrite(V4,humidity1);

  Blynk.run();

  // Take hum2 readings...
  tcaselect(3);
  humidity2=hum2.readHumidity();
  Serial.print("humidity2 = ");
  Serial.println(humidity2);
  Serial.println();  
  Blynk.virtualWrite(V3,humidity2);

  // Take weight readings...
  weight = scale.get_units();
  Serial.print("Scale reading: ");
  Serial.print(weight, 1); //scale.get_units() returns a float
  Serial.println(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  Blynk.virtualWrite(V2, weight);
}


// standard Arduino setup()
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  initialise_temp_sensors();
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(reading_frequency, take_readings);
}


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

Change this line near the top of the sketch…

int reading_frequency = 5000; // How often (in milliseconds) should we take the readings

if you want to read the sensors at a different frequency. 5000ms = 5 seconds

Pete.

Pete,

Thank you so much for this redo, I had a chance today to upload it which went great. Unfortunately it still goes offline within 30 seconds of connection. It still feels like way too much data is getting it kicked out. I think I’m going to strip out a few sensors to see if that helps. At least I’ll know its a data overload at Blynk that is getting to booted.

Jim

Five virtualWrites every 5 second is fine.

I suspect that the issue lies with the WiFiNINA library, as it seems you’re not the only one with this issue…

If modifying the library as suggested in that post isn’t something you’re comfortable with then you could try this library instead…

Or maybe go for a more mainstream board such as a NodeMCU or Wemos D1 Mini.

Pete.

1 quick question, the sketch you did is not connecting to wifi. not even getting info on the serial screen. The login code is identical to my last sketch I did and that sketch is connecting just fine. I’m testing the new sketch without the sensors connected, with no data coming from the sensors will that stall the timer and therefore stop the wifi and serial print?

Jim

Do you have the correct baud rate (9600) selected in your serial monitor?

I have no idea I’m afraid, as I said before, I’m not familiar with any of the hardware you’re using.

Pete.

Just an FYI, I’m going to post the sketch you sent see if someone can ID what is keeping the serial print from working and/or why the sketch is not trying to connect to wifi. I sure can’t see what it is.

Jim

Hello,

I have posted the sketch below to see if someone can spot what I can’t. when uploaded the sketch does not try to connect to wifi, (login data was confirmed to be right as well as network was up.)

So I’m not sure if the serial write command is the issue or if the wifi related code is the issue. Can anyone see anything?

Jim

#define BLYNK_PRINT Serial
#include <Wire.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>

#include <Adafruit_MCP9808.h>
#include <Adafruit_Si7021.h>
#include "HX711.h"

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

// Set password to "" for open networks.
char ssid[] = "Skynet van 213";
char pass[] = "quickbanana462";

// How often (in milliseconds) should we take the readings
int reading_frequency = 5000;

#define calibration_factor -4800.0

#define DOUT  2  //#define scale DT (2)
#define CLK  3   //#define scale CL (3)

HX711 scale;

#define TCAADDR       0x70

#define TEMP1_ADDR    0x18
#define TEMP2_ADDR    0x18
#define TEMP3_ADDR    0x18
#define HUM1_ADDR     0x40
#define HUM2_ADDR     0x40


// Assign unique ID to each sensor
Adafruit_MCP9808 temp1 = Adafruit_MCP9808();
Adafruit_MCP9808 temp2 = Adafruit_MCP9808();
Adafruit_MCP9808 temp3 = Adafruit_MCP9808();
Adafruit_Si7021 hum1 = Adafruit_Si7021();
Adafruit_Si7021 hum2 = Adafruit_Si7021();
//HS711 scale1 = HS711(6)

float temperature1;
float temperature2;
float temperature3;

float humidity1;
float humidity2;

float weight;

long int loadcell;
long int tare;

// Initialise the Blynk timer object
BlynkTimer timer;


void tcaselect(uint8_t i)
{
  if (i > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}


void initialise_temp_sensors()
{
  // This function is called from void setup and initialises your temperature and weight sensors

  tcaselect(7);
  if (!temp1.begin(0x18))
  {
    Serial.println("Couldn't find Temp1! Check your connections and verify the address is correct.");
    while (1);
  }
  Serial.println("Found MCP9808, Sensor 1!");
  temp1.setResolution(0); // sets the resolution mode of reading, the modes are defined in the table bellow:


  tcaselect(6);
  if (!temp2.begin(0x18))
  {
    Serial.println("Couldn't find Temp2! Check your connections and verify the address is correct.");
    while (1);
  }
  Serial.println("Found MCP9808, Sensor 2!");
  temp2.setResolution(0);


  tcaselect(5);
  if (!temp3.begin(0x18))
  {
    Serial.println("Couldn't find Temp3! Check your connections and verify the address is correct.");
    while (1);
  }
  Serial.println("Found MCP9808, Sensor 2!");
  temp3.setResolution(0);


  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
}





void take_readings()
{
  // This function is called by a timer and takes reading from each sensor nad prints them, as well as sending them to Blynk

  // Take temp1 readings...
  tcaselect(7);
  temperature1 = temp1.readTempF();
  Serial.print("temperature1 = ");
  Serial.println(temperature1);
  Serial.println();
  Blynk.virtualWrite(V7, temperature1);

  Blynk.run();

  // Take temp2 readings...
  tcaselect(6);
  temperature2 = temp2.readTempF();
  Serial.print("temperature2 = ");
  Serial.println(temperature2);
  Serial.println();
  Blynk.virtualWrite(V6, temperature2);

  Blynk.run();

  // Take temp3 readings...
  tcaselect(5);
  temperature3 = temp3.readTempF();
  Serial.print("temperature3 = ");
  Serial.println(temperature3);
  Serial.println();
  Blynk.virtualWrite(V5, temperature3);

  Blynk.run();

  // Take hum1 readings...
  tcaselect(4);
  humidity1 = hum1.readHumidity();
  Serial.print("humidity1 = ");
  Serial.println(humidity1);
  Serial.println();
  Blynk.virtualWrite(V4, humidity1);

  Blynk.run();

  // Take hum2 readings...
  tcaselect(3);
  humidity2 = hum2.readHumidity();
  Serial.print("humidity2 = ");
  Serial.println(humidity2);
  Serial.println();
  Blynk.virtualWrite(V3, humidity2);

  // Take weight readings...
  weight = scale.get_units();
  Serial.print("Scale reading: ");
  Serial.print(weight, 1); //scale.get_units() returns a float
  Serial.println(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  Blynk.virtualWrite(V2, weight);
}


// standard Arduino setup()
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  initialise_temp_sensors();
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(reading_frequency, take_readings);
}


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

Is there any specific reason for choosing GPIO03 (SD9) ?
Can you change the pin and see it if that solves your problem ?

Choose GPIO4 and 5 as they are safe to use.

@emtjim I’ve merged this back with the earlier topic, as it gives more context to the origins of the code.

What does your serial monitor say?

Pete.

Pete,

Regarding the serial monitor, On the original sketch it starts with the login info and the Blynk Logo then finds the sensors Ect. on the second version the monitor is always blank

Jim

I made the change, the scale data was bit dodgy that may have been the issue. However it did not help with the Serial Monitor remaining blank. Still can’t tell if the monitor is not being printed to or if the unit is not trying to wifi connect

Jim

Can you tell what version of Blynk lib you are using ? If it is not updated please update.
And set baud rate to 115200 or 57600. And copy paste the serial monitor output.

Try changing your void setup so that Serial.begin and Blynk.begin are the first two lines.
Is your serial monitor set to 9600 ?

Pete.

That was it Pete, now to see if it more stable with the timer.

1 Like

V 0.6.1on MKR1010

Pete,

That helped s ton, I have wifi stability so far. only issue is that the only sensor to send is #1 the others show as NaN on the serial monitor. I think the sketch istrying to read the next sensor too quickly after #1, I have put in a delay between each sensor reading. Tomorrow ill see what that brings.

jim

1 Like

Would be more efficient use of your processor to make separate voids for each sensor and the put delays between them in the setup. In other words staggering the readings. This would do essentially the same thing as you speak of but would have your processor live at all times.