no reading data happens after a series of successful readings

Hi everyone,

I am now using a UNO board and a WIFI shield to read data from two pressure sensor and give commands to two electrically controlled valves. I think I have successfully installed everything, and the code should be right.

And I did get successfully connected to the board from internet, read data online and send out command to open and close the valves. However, this morning the reading disappeared. I have no idea of what happened. Maybe the code has some bugs, or the hardware is broken down. It is bizzaro 'cuz even if there is no input into the Arduino, there should still be some random readings right?

So I am asking if anyone can help me with the code below to see if there is something wrong with it. Otherwise the cause may be in field. But I really want to know ahead before going in field.

So many thanks for your help!

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "xxxx"; //  network SSID (name) 
char pass[] = "xxxx";    // network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

char server[] = "xxxxxxxxxxxxxxxxxxxxxx";
int port = 80;

WiFiClient client;
 
unsigned long lastConnectionTime = 0;				// last time you connected to the server, in milliseconds
boolean lastConnected = false;						// state of the connection last time through the main loop
const unsigned long postingInterval = 60000;		// delay between updates, in milliseconds

boolean incomingData = false;


//----------------VALVE CONTROL - BEGIN----------------//
int so1_1_1 = 8;
int sol_1_2 = 9;
int sol_1_Enable = 6;

int so1_2_1 = 3;
int sol_2_2 = 5;
int sol_2_Enable = 2;
//----------------VALVE CONTROL - END----------------//

 
void setup() {
    
   //Valve control pins
   pinMode(so1_1_1, OUTPUT);
   pinMode(sol_1_2, OUTPUT);
   pinMode(sol_1_Enable, OUTPUT);
  
   pinMode(so1_2_1, OUTPUT);
   pinMode(sol_2_2, OUTPUT);
   pinMode(sol_2_Enable, OUTPUT);   
   
  
   //Initialize serial and wait for port to open:
   Serial.begin(9600); 
   while (!Serial) {
     ; // wait for serial port to connect. Needed for Leonardo only
   }   

    // check for the presence of the shield:
   if (WiFi.status() == WL_NO_SHIELD) {
     Serial.println("WiFi shield not present"); 
     // don't continue:
     while(true);
   } 
   
   // attempt to connect to Wifi network:
   while (status != WL_CONNECTED) { 
     Serial.print("Attempting to connect to SSID: ");
     Serial.println(ssid);
     // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
     status = WiFi.begin(ssid, pass);
   
     // wait 10 seconds for connection:
     delay(10000);
   } 
   Serial.println("Connected to wifi");
   printWifiStatus();   
}

void loop() { 
	Serial.println("========================");
	Serial.println("--- START ---");

	// if there's no net connection, but there was one last time
	// through the loop, then stop the client:
	if (!client.connected() && lastConnected) {
		Serial.println();
		Serial.println("--- disconnecting_client_1.");
		client.stop();
	}
	//--------------------------------------------------------------
	// if you're not connected, and ten seconds have passed since
	// your last connection, then connect again and send data:
	if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
		// if there's a successful connection:
		sendDataOverWiFi();
	}
	//--------------------------------------------------------------
	// store the state of the connection for next time through
	// the loop:
	lastConnected = client.connected();
		
	//--------------------------------------------------------------
	// wait 60 seconds
	Serial.println("--- wait 60 seconds ---");
	delay(60000);
	Serial.println("--- wait done");
	Serial.println("--- END ---");

        //get flow reading
        //captureFlowMeterReading();
}

void sol_OFF(int solx1, int solx2, int solxEn)
{
  digitalWrite(solxEn, HIGH); 
  digitalWrite(solx2, LOW);
  digitalWrite(solx1, HIGH);  
  delay(100);
  digitalWrite(solxEn, LOW); 
}

void sol_ON(int solx1, int solx2, int solxEn)
{
  digitalWrite(solxEn, HIGH); 
  digitalWrite(solx1, LOW);
  digitalWrite(solx2, HIGH);  
  delay(100);
  digitalWrite(solxEn, LOW); 
}

String lastWebResponse = "";

// this method makes a HTTP connection to the server:
void sendDataOverWiFi() {
  
    int analogChannel1 = 0;  
    int analogChannel2 = 1; 
  
    Serial.print("\nStarting connection to server ");
     Serial.println(server);

	//Serial.println("--- connecting to server ---");
	if (client.connect(server, 80)) {
		Serial.println();
		Serial.println("--- sending data");

                 int sensorReading1 = analogRead(analogChannel1);                 
                 char PostData1[] = "0000000";                 
                 String pdataString1 = itoa(sensorReading1, PostData1, 10);

                 int sensorReading2 = analogRead(analogChannel2);                 
                 char PostData12[] = "0000000";                 
                 String pdataString2 = itoa(sensorReading2, PostData12, 10);
                 
                 String dataString = pdataString1 + "," + pdataString2;        
                 
                 Serial.println(dataString);
                 //Serial.println(pdataString1);
                 
                 // Make a HTTP request:     
                 client.println("POST /api/device/7579DB95-9A6E-44C4-883A-1FB8ACAB60F9 HTTP/1.1");
                 client.print("Host: ");
                 client.println(server);
                 client.println("User-Agent: Arduino/1.0");
                 client.println("Connection: close");
                 client.print("Content-Length: ");
		 //// calculate the length of the sensor reading in bytes:
		 //// 8 bytes for "sensor1," + 8 bytes for "sensor2," + number of digits of the data:
		 //int thisLength = 16 + getLength(sensorReading) + getLength(fakeSecondChannel);
		 //client.println(thisLength);
                 client.println(dataString.length());
                 client.println();
                 client.println(dataString);
                 client.println(); 
                 
		// wait 10 seconds:
		delay(1000);
		// note the time that the connection was made:
		lastConnectionTime = millis();
		Serial.println("--- sending data done");
	}
	else {
		// if you couldn't make a connection:
		Serial.println("--- connection failed");
		Serial.println("--- disconnecting_client_2.");
	}

	//-------------------------------------------------------------
	// give the server time to respond
	delay(1000);

	//--------------------------------------------------------------
	// if there's incoming data from the net connection.
	// send it out the serial port.  
	while (client.available() && status == WL_CONNECTED) {
		if (incomingData == false)
		{
			Serial.println();
			Serial.println("--- Incoming data Start ---");
			incomingData = true;
		}

                char c = client.read();
		Serial.write(c);

		lastWebResponse += c;

	}
        client.flush();
        client.stop();
   
	// Debugging --------------------------------------------------
	if (incomingData == true)
	{
		Serial.println("--- Incoming data End");
                
                if(lastWebResponse.indexOf("vcomm_1_On") >=0) {
                   sol_ON(so1_1_1, sol_1_2, sol_1_Enable);      
	           Serial.println("--- vcomm_1_On ---"); 
                } 
                
                if(lastWebResponse.indexOf("vcomm_1_Off") >=0) {
                   sol_OFF(so1_1_1, sol_1_2, sol_1_Enable);     
	           Serial.println("--- vcomm_1_Off ---");  
                } 
                
                if(lastWebResponse.indexOf("vcomm_2_On") >=0) {
                   sol_ON(so1_2_1, sol_2_2, sol_2_Enable);     
	           Serial.println("--- vcomm_2_On ---");                     
                } 
                
                if(lastWebResponse.indexOf("vcomm_2_Off") >=0) {
                   sol_OFF(so1_2_1, sol_2_2, sol_2_Enable);     
	           Serial.println("--- vcomm_2_Off ---");             
                } 


                lastWebResponse = "";


		incomingData = false; 
	}
	if (status == WL_CONNECTED){
		Serial.println("--- WiFi connected");
	}
	else{
		Serial.println("--- WiFi not connected");
	}
}

// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue) {
	// there's at least one byte:
	int digits = 1;
	// continually divide the value by ten, 
	// adding one to the digit count for each
	// time you divide, until you're at 0:
	int dividend = someValue /10;
	while (dividend > 0) {
		dividend = dividend /10;
		digits++;
	}
	// return the number of digits:
	return digits;
}


void printWifiStatus() {
   // print the SSID of the network you're attached to:
   Serial.print("SSID: ");
   Serial.println(WiFi.SSID());

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

   // print the received signal strength:
   long rssi = WiFi.RSSI();
   Serial.print("signal strength (RSSI):");
   Serial.print(rssi);
   Serial.println(" dBm");
}

I guess you're running out of memory. There are two problems in the sketch: You're not using the F() macro for constant strings in the print() and println() methods and you're using the String class which clutters up the available memory and should not be used on microcontroller systems as the Arduino with only 2kB of RAM.

So first replace all calls of type

client.print("Any constant string here");

to

client.print(F("Any constant string here"));

If that doesn't help, replace all occurrences of String with conceptional identical functions using C-strings (char *) as there are sprintf() and the like.

pylon:
I guess you're running out of memory. There are two problems in the sketch: You're not using the F() macro for constant strings in the print() and println() methods and you're using the String class which clutters up the available memory and should not be used on microcontroller systems as the Arduino with only 2kB of RAM.

So first replace all calls of type

client.print("Any constant string here");

to

client.print(F("Any constant string here"));

If that doesn't help, replace all occurrences of String with conceptional identical functions using C-strings (char *) as there are sprintf() and the like.

Thank you so much.
I will try your methods and see how it works!