lost in loops!

Hi
I thought i had completed my software but i have discovered some “issues” lol

what I would like is for the temperature and humidity to be checked every 15 minutes ish
the webpage to be refreshed when ever it is asked
and when I click on the fan on link the settings are overridden so the fan stays on for 6hours.

it used to check every 15min ish, but then i installed wifi connectivity and now it only refreshes when the webpage is requested so I have broken one loop, but it may have to be like this to work the webpage, who knows :)))

Im really not sure how to fix it, also to add to the problems the program file is getting too big, but this doesnt really bother me as long as it runs (yes I am ware it is a horribly written program but my skills lay elsewhere lol

can anyone fix?

thank you

main.txt (6.95 KB)

SoftwareSerial Serial1(9, 8); // RX, TX

Do you REALLY have a Serial1 connected to those pins? If not, give the instance a far better name that reflects what IS connected.

The page you server up could have META tags in it, to make the client re-request the page periodically.

lol, some may call it a sht1 i call it Serial1 potato potato lol

no refresh in the meta because nothing is connected to the wifi for more than a second so any meta refresh wouldn't work.

malcomp:
lol, some may call it a sht1 i call it Serial1 potato potato lol

no refresh in the meta because nothing is connected to the wifi for more than a second so any meta refresh wouldn't work.

You connect your PC, running a browser, to the WiFi network for less than a second, and you expect it to show real time data from something else connected to the network. That hardly makes sense.

The browser makes a GET request, which might be processed by the server in less than a second. But the page served up CAN contain META tags, telling the browser/client to make another request, causing another connection...

i understand but your barking up the wrong tree.

what i am after is for the program to run the relays in its own loop.
then the wifi to just display the data of what is happening,
and as a bonus feature for it to swith the relays on for a long period.

every time i have tried to mess with the loop it breaks the webpage.

meta tags are not the solution to this. if you don't know how to achieve this then that is ok, you dont have to know everything :slight_smile:

i understand but your barking up the wrong tree.

You put the cat up the wrong tree.

Lets look at your code. You use I2C to read from two devices here:

 unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  // delay(300);

 // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

//second write and read
  
{
  unsigned int data2[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
 //  delay(300);

  // Start I2C Transmission
  Wire.beginTransmission(Addr2);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 6 bytes of data
  Wire.requestFrom(Addr2, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data2[0] = Wire.read();
    data2[1] = Wire.read();
    data2[2] = Wire.read();
    data2[3] = Wire.read();
    data2[4] = Wire.read();
    data2[5] = Wire.read();
 
   }

This code is in:

void printWifiStatus()
{

which is called here:

    Serial.println("Client disconnected");
     printWifiStatus(); 
  }
}

First, printWiFiStatus() does NOT print the WiFi status, so that's a stupid name for the function.
Second, that function is only called AFTER a client has connected, and been dealt with.

You need a FAR better name for that function, AND you need to call it on every pass through loop() (or at some schedule, using millis()) and server up the last data collected, if the client has a need to know any of the data that that function collects.

I have no idea what the function printWifiStatus(); does.

if i remember rightly if it remove it everything stops working, but i could be wrong.

maybe it is an old loop with void? void printWifiStatus()

the vibe i think im getting is that my data is trapped in a loop of printwifistatus?

if it is this is by accident

so maybe called it PrintCollectedData?

then call it later ? again.

im starting to feel that my 3 objectives are so far from my grasp i may never get it to work

I have no idea what the function printWifiStatus(); does.

Why not? It's your code. You NEED to know what it does, so that you can call it at the right time/place.

if i remember rightly if it remove it everything stops working, but i could be wrong.

I suppose that that would depend on your definition of "everything".

the vibe i think im getting is that my data is trapped in a loop of printwifistatus?

Perhaps if you studied your code, and tried to decide what the function should be called, based on what it actually does, you'd understand that there is no loop for your data to get trapped in.

so maybe called it PrintCollectedData?

That's better, but it does more than just print collected data.

Whenever one gets stuck like this, I suggest that they move the printing of the data into another function. Collect the data in one function. Print it in another. Then, it becomes easier to see what CollectData() should do, and where it should be called.

Do you need to call CollectData() on every pass through loop()? Every 5 minutes? Every time a client connects? Before or after you've handled the client request and closed the connection? Only YOU can answer these questions.

I have asked before on here but I have not been able to understand or sort my problem

my program worked fine until I added wifi function. now the program gets stuck in a loop at the start of the webpage client. I dont understand the loop that is there, but my program just sticks at it and does not go onto the original program where it compares temperatures and moves the relays.
It does work fine if I connect and view the webpage, but as soon as I stop requesting the webpage the program stops working the relays.

anyone have any practical ways to get it to stop?

thank you

newcode.txt (7.13 KB)

Post your code in the forum, not as an attachment people have to download. USE CODE TAGS as described here: Read this before posting a programming question ... - Programming Questions - Arduino Forum.
You have 34 posts. You should know this by now.

Threads merged.

I was hoping for a more constructive reply this time by asking in a different way.

I always seem to get the yoda type answer.......

program is faulty is it not? not understand code do you not.

look into the millis and face your inner demons and you will find the answer you seek.

Im kind of looking for the inbetweeners version

yeah mate the get rid of the break part, that will sort it.

So, Post Your Code

gfvalvo:
Post your code in the forum, not as an attachment people have to download. USE CODE TAGS as described here: Read this before posting a programming question ... - Programming Questions - Arduino Forum.
You have 34 posts. You should know this by now.

Yes your right, i do know this by now. I even know a bit more that the file is too big to fit in code tags. but thank you for giving no constructive input at all.

why is everyone like this. I have never known a forum be so miserable.
I have had a few amazing answers, and for them I am truly grateful. but this isnt one of them.

Ta-Dah

#include <Wire.h>
#include "WiFiEsp.h"
#include "SoftwareSerial.h"
SoftwareSerial Serial1(9, 8); // RX, TX

char ssid[] = "H";
char pass[] = "N";
int status = WL_IDLE_STATUS;
int ledStatus = LOW;
int cTemp;
int cTemp2;
int humidity;
int humidity2;
int fTemp;
int fTemp2;

WiFiEspServer server(80);
RingBuffer buf(8);

#define Addr 0x44
#define Addr2 0x45
#define RelayControl1 4
#define RelayControl2 5
#define RelayControl3 6
#define RelayControl4 7

void setup()
{
  Wire.begin();
  Serial.begin(9600);     //SH13
  Serial1.begin(9600);    // wifi
  // wifi START
  pinMode(LED_BUILTIN, OUTPUT);  // digital pin LED as an output.
  WiFi.init(&Serial1);
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }
  while (status != WL_CONNECTED) {
    Serial.print("Connecting to: ");
    Serial.println(ssid);
    // Connect WPA/WPA2
    status = WiFi.begin(ssid, pass);
  }
  Serial.println("Connected :)");
  // Start port 80
  server.begin();

  pinMode(RelayControl1, OUTPUT);
  pinMode(RelayControl2, OUTPUT);
  pinMode(RelayControl3, OUTPUT);
  pinMode(RelayControl4, OUTPUT);
  digitalWrite(RelayControl1, HIGH);
  digitalWrite(RelayControl2, HIGH);
  digitalWrite(RelayControl3, HIGH);
  digitalWrite(RelayControl4, HIGH);
}

void loop()

{
  WiFiEspClient client = server.available();  // listen clients
  if (client) {                               //  client?
    Serial.println("User");             // print
    buf.init();                               // circular buffer
    while (client.connected()) {              // loop user connected
      if (client.available()) {               // if bytes to read from the client,
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to ring buffer
        // 2 newline is the end of the HTTP request,  send a response
        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }
        // Check "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn Fan ON");
          ledStatus = HIGH;

          digitalWrite(RelayControl3, LOW);
          // digitalWrite(LED_BUILTIN, HIGH);   // turn the fan
        }
        else if (buf.endsWith("GET /L")) {
          Serial.println("Turn Fan OFF");
          ledStatus = LOW;
          digitalWrite(RelayControl3, HIGH);   // turn the fan
        }
      }
    }

    // close the connection
    client.stop();
    Serial.println("Client disconnected");
    printDataCollected();
  }
}

void sendHttpResponse(WiFiEspClient client)

{

  // HTTP headers
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  client.print("The Fan is ");
  client.print(ledStatus);
  client.println("
");

  client.print("<span id=\"T1\">");
  client.println("Lounge_Temp");
  client.println(cTemp);
  client.println("Loft_Temp");
  client.println(cTemp2);
  client.println("Lounge_Humidity");
  client.println(humidity);
  client.println("Loft_Humidity");
  client.print(humidity2);
  client.print("end");
  client.print("</span>");
  client.println("
");
  client.println("Click <a href=\"/H\">here</a> turn the Fan on
");
  client.println("Click <a href=\"/L\">here</a> turn the Fan off
");
  //  HTTP  ends
  client.println();
}

void printDataCollected()
{

  unsigned int data[6];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send 16-bit command byte
  Wire.write(0x2C);
  Wire.write(0x06);
  // Stop I2C transmission
  Wire.endTransmission();
  // delay(300);

  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);

  // Read 6 bytes of data
  // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
  if (Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }

  //second write and read

  {
    unsigned int data2[6];

    // Start I2C Transmission
    Wire.beginTransmission(Addr2);
    // Send 16-bit command byte
    Wire.write(0x2C);
    Wire.write(0x06);
    // Stop I2C transmission
    Wire.endTransmission();
    //  delay(300);

    // Start I2C Transmission
    Wire.beginTransmission(Addr2);
    // Stop I2C Transmission
    Wire.endTransmission();

    // Request 6 bytes of data
    Wire.requestFrom(Addr2, 6);

    // Read 6 bytes of data
    // temp msb, temp lsb, temp crc, hum msb, hum lsb, hum crc
    if (Wire.available() == 6)
    {
      data2[0] = Wire.read();
      data2[1] = Wire.read();
      data2[2] = Wire.read();
      data2[3] = Wire.read();
      data2[4] = Wire.read();
      data2[5] = Wire.read();

    }

    // Convert the data
    int temp = (data[0] * 256) + data[1];
    cTemp = -45.0 + (175.0 * temp / 65535.0);
    fTemp = (cTemp * 1.8) + 32.0;
    humidity = (100.0 * ((data[3] * 256.0) + data[4])) / 65535.0;

    int temp2 = (data2[0] * 256) + data2[1];
    cTemp2 = -45.0 + (175.0 * temp2 / 65535.0);
    fTemp2 = (cTemp2 * 1.8) + 32.0;
    humidity2 = (100.0 * ((data2[3] * 256.0) + data2[4])) / 65535.0;

    Serial.print("Temp     :");
    Serial.print(cTemp);
    Serial.print(" c     ");
    Serial.print("Temp2      :");
    Serial.print(cTemp2);
    Serial.println(" c  (loft)");
    Serial.print("humidity :");
    Serial.print(humidity);
    Serial.print(" %     ");
    Serial.print("humidity2  :");
    Serial.print(humidity2);
    Serial.println(" %  (loft)");

    if (((cTemp - 2) < cTemp2 ) && (humidity > humidity2 ))
    {
      Serial.println("**** RELAYS ON Temp Higher (withing 2 deg) in loft humidity in loft lower ****");
      digitalWrite(RelayControl1, LOW);
      digitalWrite(RelayControl2, LOW);
      digitalWrite(RelayControl3, LOW);
      digitalWrite(RelayControl4, LOW);
    }

    if (((cTemp - 2) < cTemp2 ) && (humidity < humidity2 ))
    {
      Serial.println("Relays off temp ok but humid in loft ");
      digitalWrite(RelayControl1, HIGH);
      digitalWrite(RelayControl2, HIGH);
      digitalWrite(RelayControl3, HIGH);
      digitalWrite(RelayControl4, HIGH);
    }


    if (((cTemp - 2) > cTemp2 ) && (humidity < humidity2 ))
    {
      Serial.println("Relays Off too cold and too humid in loft");
      digitalWrite(RelayControl1, HIGH);
      digitalWrite(RelayControl2, HIGH);
      digitalWrite(RelayControl3, HIGH);
      digitalWrite(RelayControl4, HIGH);
    }


    if (((cTemp - 2) > cTemp2 ) && (humidity > humidity2 ))
    {
      Serial.println("Relays off too cold in loft humidity ok  Change");
      digitalWrite(RelayControl1, HIGH);
      digitalWrite(RelayControl2, HIGH);
      digitalWrite(RelayControl3, HIGH);
      digitalWrite(RelayControl4, HIGH);
    }

    Serial.println(" ");
    // print your WiFi shield's IP address
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);
    //wifi end

  }
}

Well you are so smart but you still haven’t answered question.

I will go an kill myself now because I cant work a forum so well as you. you are clearly a better human being in every way, surely more hansom and better and being smug.

now if you could just manage actually helping then maybe you wouldn’t look such a prat.

also I thiough this was an interesting read

https://www.reddit.com/r/arduino/comments/7ua76x/why_are_the_arduinocc_forums_so_terrible/

oh no thats wrong isnt it. let me try this

https://www.reddit.com/r/arduino/comments/7ua76x/why_are_the_arduinocc_forums_so_terrible/

malcomp:
now if you could just manage actually helping then maybe you wouldn't look such a prat.

Childish name calling. Directed at someone who is trying to help you. See you in a few days...

Anyway, downloaded the text file representation of the sketch, opened it, and pasted into IDE. I’m wondering if this test inside the ‘while’ loop is ever successful:

        if (buf.endsWith("\r\n\r\n")) {
          sendHttpResponse(client);
          break;
        }

If the condition is never satisfied, the loop will never break. A Serial.println inside the ‘if() {…}’ would be helpful. Also, is the page ever served back to the client?

Compare this ‘if’ structure to the way end-of-HTTP-request is detected by the WebServer example in the WiFiESP library.