Asynchronous Programming

void send_my_data()
{

    if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  // if there are incoming bytes available
  // from the server, read them and print them:


  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

This is a function that is a part of the code below

#include <SPI.h>
#include <Ethernet2.h>
#include <avr/pgmspace.h>
#include <EEPROM.h>
#include <Uardecs_mega.h>
#include <Wire.h>

#define OPRMODE_ERR_SHT2xSENSERR       0x20000000 //0 01000 00000 0 0000 0000000000000000 //


char SHT2x_sensSts;
unsigned char SHT2x_errcount=0;


/////////////////////////////////////
//IP reset jupmer pin setting
//IP
/////////////////////////////////////
const byte U_InitPin = 3;
const byte U_InitPin_Sense=HIGH;


////////////////////////////////////
//Node basic infomation
//
///////////////////////////////////
const char U_name[] PROGMEM= "TempHumid Node";//MAX 20 chars
const char U_vender[] PROGMEM= "WARC/NARO";//MAX 20 chars
const char U_uecsid[] PROGMEM= "000000000000";//12 chars fixed
const char U_footnote[] PROGMEM= "SHT2x Sensor Node";
char U_nodename[20] = "node";   //MAX 19chars (This value enabled in safemode)
UECSOriginalAttribute U_orgAttribute;//
//////////////////////////////////
// html page1 setting
//Web
//////////////////////////////////

//Total number of HTML table rows.
//web
const int U_HtmlLine = 0;

//
struct UECSUserHtml U_html[U_HtmlLine]={
};

//////////////////////////////////
// UserCCM setting
// CCM
//////////////////////////////////

enum {
CCMID_InAirTemp,
CCMID_InAirHumid,
CCMID_InAirHD,
CCMID_cnd,
CCMID_dummy, //CCMID_dummy
};


//CCM
//
const int U_MAX_CCM = CCMID_dummy;
UECSCCM U_ccmList[U_MAX_CCM];

//CCM
const char ccmNameTemp[] PROGMEM= "Temperature";
const char ccmTypeTemp[] PROGMEM= "InAirTemp";
const char ccmUnitTemp[] PROGMEM= "C";


const char ccmNameHumid[] PROGMEM= "Humid";
const char ccmTypeHumid[] PROGMEM= "InAirHumid";
const char ccmUnitHumid[] PROGMEM= "%";

const char ccmNameHD[] PROGMEM= "HumidDiff";
const char ccmTypeHD[] PROGMEM= "InAirHD.mIC";
const char ccmUnitHD[] PROGMEM= "g m-3";

const char ccmNameCnd[] PROGMEM= "NodeCondition";
const char ccmTypeCnd[] PROGMEM= "cnd.mIC";
const char ccmUnitCnd[] PROGMEM= "";

//------------------------------------------------------
void UserInit(){
U_orgAttribute.mac[0] = 0x00;
U_orgAttribute.mac[1] = 0x00;
U_orgAttribute.mac[2] = 0x00;
U_orgAttribute.mac[3] = 0x00;
U_orgAttribute.mac[4] = 0x00;
U_orgAttribute.mac[5] = 0x01;
  
UECSsetCCM(true, CCMID_InAirTemp, ccmNameTemp, ccmTypeTemp, ccmUnitTemp, 29, 1, A_10S_0);
UECSsetCCM(true, CCMID_InAirHumid, ccmNameHumid, ccmTypeHumid, ccmUnitHumid, 29, 1, A_10S_0);
UECSsetCCM(true, CCMID_InAirHD, ccmNameHD, ccmTypeHD, ccmUnitHD, 29, 2, A_10S_0);
UECSsetCCM(true, CCMID_cnd      , ccmNameCnd , ccmTypeCnd , ccmUnitCnd , 29,3, A_10S_0);
}

//---------------------------------------------------------
void OnWebFormRecieved(){
}
//---------------------------------------------------------
void UserEverySecond(){
  U_ccmList[CCMID_cnd].value=0;
  if(SHT2x_errcount>3)
        {
          SHT2x_errcount=4;
        U_ccmList[CCMID_cnd].value|=OPRMODE_ERR_SHT2xSENSERR;
        }

//------------------------------------------------------(3)  
static char count=0;
count++;
if(count %3==1)
  {

  double t=SHT2x_GetTemperature();
  Serial.print("TEMPERATURE using t=SHT2x_GetTemperature():");
  Serial.print (t);
  double rh=SHT2x_GetHumidity();
  
  
  if(SHT2x_sensSts)
      {
        SHT2x_errcount++;
      }
  else{
      SHT2x_errcount=0;
      U_ccmList[CCMID_InAirTemp].value=(long)(t*10);
      
      Serial.print("TEMPERATURE using U_ccmList[CCMID_InAirTemp].value=(long)(t*10):");
      Serial.print (U_ccmList[CCMID_InAirTemp].value);
      
      U_ccmList[CCMID_InAirTemp2].value=(long)(t*10);
      U_ccmList[CCMID_InAirQuality].value= 0;
      U_ccmList[CCMID_InAirHumid].value=(long)(rh*10);
      }
  
  //
  double humidpress=6.1078*pow(10,(7.5*t/(t+237.3)));
  double humidvol=217*humidpress/(t+273.15);
  U_ccmList[CCMID_InAirHD].value=(100-rh)*humidvol;//

  
  
  }

//------------------------------------------------
if(SHT2x_errcount)
    {
        U_ccmList[CCMID_InAirTemp].flagStimeRfirst=false;
        U_ccmList[CCMID_InAirTemp2].flagStimeRfirst=false;
        U_ccmList[CCMID_InAirQuality].flagStimeRfirst=false;
        U_ccmList[CCMID_InAirHumid].flagStimeRfirst=false;
        U_ccmList[CCMID_InAirHD].flagStimeRfirst=false;
    }
  

/* to get attribute and type data

//  HTTPGetFormDataEDITCCMPage HTTPGetFormDataEDITCCMPageTRIAL;
//  Serial.print("U_ccmList[CCMID_InAirTemp].baseAttribute[AT_ORDE]");
//  Serial.println(U_ccmList[CCMID_InAirTemp].baseAttribute[AT_ORDE]);

//    Serial.print("U_ccmList[CCMID_InAirTemp].typeStr");
//    Serial.println(U_ccmList[CCMID_InAirTemp].typeStr);

*/
}

//---------------------------------------------------------
void UserEveryMinute(){

    send_my_data();
  
}
//---------------------------------------------------------
void UserEveryLoop(){
}

//---------------------------------
void loop(){
UECSloop();

      
     

delay(5000);
}

//---------------------------------
void setup(){

Wire.begin();
Serial.begin(9600);
UECSsetup();
delay(5000);
}

//----------------------------------------SHT2x
#define eSHT2xAddress   0x40
#define eTempHoldCmd      0xE3
#define eRHumidityHoldCmd 0xE5
#define eTempNoHoldCmd      0xF3
#define eRHumidityNoHoldCmd 0xF5

double SHT2x_GetHumidity(void)
{
  return (-6.0 + 125.0 / 65536.0 * (double)(SHT2x_readSensor(eRHumidityHoldCmd)));
}
double SHT2x_GetTemperature(void)
{
  return (-46.85 + 175.72 / 65536.0 * (double)(SHT2x_readSensor(eTempHoldCmd)));
}

uint16_t SHT2x_readSensor(uint8_t command)
{
    uint16_t result;

    Wire.beginTransmission(eSHT2xAddress);
    Wire.write(command);
    delay(5);
    SHT2x_sensSts=Wire.endTransmission();
    if(SHT2x_sensSts){return 0;}

    Wire.requestFrom(eSHT2xAddress, 3);
    if(Wire.available()==3)
    {
    result = ((Wire.read()) << 8);
    result += Wire.read();
    result &= ~0x0003;   // clear two low bits (status bits)
    return result;
    }
    else
    {
      SHT2x_sensSts=4;
      return 0;
    }
    
}

The problem is that WHEN THE FUNCTION send_my_data() IS CALLED, OTHER OPERATIONS OF THE CODE STOPS UNTIL THE FUNCTION IS EXECUTED. I DON'T WANT THIS TO HAPPEN. WHAT SHOULD I DO TO ENSURE THAT OTHER PROCESSES OF THE CODE CONTINUES AFTER THE FUNCTION send_my_data() IS CALLED? HERE send_my_data() IS JUST FOR ILLUSTRATION, THERE WILL BE OTHER THINGS IN IT TOO.

UARDECS_MEGA-master.zip (58.4 KB)

Hello,

Please DO NOT SHOUT! There is no need to SHOUT, it just annoys people here and certainly does not result in you getting a better answer.

    // do nothing forevermore:
    while (true);

What do you think happen when your code gets to this bit?

PerryBebbington:
Hello,

Please DO NOT SHOUT! There is no need to SHOUT, it just annoys people here and certainly does not result in you getting a better answer.

    // do nothing forevermore:

while (true);




What do you think happen when your code gets to this bit?

I didn't understand what you meant by "shouting"

I didn't understand what you meant by "shouting"

Typing everything IN CAPITAL LETTERS as you did in your opening post is considered to be shouting and is considered rude. Not just on this web site but on many others. Putting everything in bold is considered similarly (yes, I realise you didn't do this).

However, I note that your reply to me was in normal text, thank you :slight_smile:

    if (client.connect(server, 80)) {

i've noticed that an attempt to connect must have a ~15 sec timeout before returning and reporting failure. It doesn't look like there is any other option, an arg to return sooner on failure.

either the hostname and port are incorrect or the server is not active. Otherwise it returns quickly with a valid connection to the server

i have this code in a loop with a button press that will abort the attempt, allowing the hostname/port to be corrected

I missed in my first reading that you also have:

delay(5000);

What do you think your code does in those 5 seconds? How many other processes get done in that 5 seconds?

I suspect you have not studied the basics of writing C/C++ because if you had you would have spotted these things as they are very basic.

Please follow the examples in the IDE and on this web site and learn the basics. Of particular importance to your question are these two:

Using millis for timing
Demonstration for several things at the same time

However, those 2 alone are not enough and you should study the more basic tutorials first.

Enjoy.