Adding a delay to I2C sensor run thru multiplexer

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.