122Views6Replies

Author Options:

Data updating on thingspeak (cloude) only once Answered

 I am trying to update Thingspeak using Edison. It updates thingspeak but only at once.
If I have to update again I have to run the code again.
#include <WiFi.h>
#include <SPI.h>

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";  //TS


String writeAPIKey = "**************";    //TS
const int updateThingSpeakInterval = 20 * 1000;      // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)


char ssid[] = "*********";     //  your network SSID (name)
char pass[] = "*********";  // your network password




int status = WL_IDLE_STATUS;     // the Wifi radio's status


// initialize the library instance:
WiFiClient client;
String stringVal = "";
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
int a;
float tempC;
int B=3975;
float resistance;


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
   while (!Serial) {
 
  }


//  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 WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:  
    status = WiFi.begin(ssid, pass);


    // wait 10 seconds for connection:
    delay(10000);
  
  }
 
  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");
  // printCurrentNet();
  // printWifiData();


}


  void loop() {

a = analogRead(0);
resistance=(float)(1023-a)*10000/a;
tempC=1/(log(resistance/10000)/B+1/298.15)-273.15;

// converts temp  to string
    
stringVal += String(int(tempC))+ "."+String(getDecimal(tempC));

//char buf[16];
//String strTemp = floatToString(16 , cel , 5);
//Serial.println(strTemp);

// Print Update Response to Serial Monitor
  while (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }


  // Disconnect from ThingSpeak
  if (!client.connected() && lastConnected )
  {
    Serial.println("...disconnected");
    Serial.println();
  
    client.stop();
    }

  // Update ThingSpeak
  if(client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
  {
    updateThingSpeak("field1="+stringVal);
     Serial.print( stringVal);
   
     Serial.println("C");
     delay(1000);
     Serial.println();
  
  
  }

  lastConnected = client.connected();

}
//function to extract decimal part of float
long getDecimal(float val)
{
int intPart = int(val);
long decPart = 100*(val-intPart); //I am multiplying by 100 assuming that the foat values will have a maximum of 3 decimal places
                                   //Change to match the number of decimal places you need
if(decPart>0)return(decPart);           //return the decimal part of float number if it is available
else if(decPart<0)return((-1)*decPart); //if negative, multiply by -1
else if(decPart=0)return(00);           //return 0 if decimal part of float number is not available
}


void updateThingSpeak(String  stringVal)
{
  if (client.connect(thingSpeakAddress, 80))
  {       
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print( stringVal.length());
    client.print("\n\n");


    client.print( stringVal);
  
    lastConnectionTime = millis();
  
    if (client.connected())
    {
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
    
      failedCounter = 0;
    }
    else
    {
      failedCounter++;

      Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")"); 
      Serial.println();
    }
  
  }
  else
  {
    failedCounter++;
  
    Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")"); 
    Serial.println();
  
    lastConnectionTime = millis();
  }
}
after running this code I get this(image attached)

Discussions

0
None
iceng

2 years ago

I don't have time to block diagram your code but you should... Then modify the part

------------------------------------------------------------------------------------

// Disconnect from ThingSpeak

if (!client.connected() && lastConnected )
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}

-------------------------------------------------------------------------------------------------

and see if it remains connected.

=========================================================

Another approach I have mentioned. Use the block diagram and some new code that waits for a data change then at change log on send, log off and wait for another change...

0
None
iceng

2 years ago

So your code has a disconnect from ThingSpeak why?

I would make code that would update every time your data changed.

If every one was logged on sending null characters. ThingzSpeak would over load.

0
None
ANINDYAB5iceng

Answer 2 years ago

So what should I do now? Have you given me that code or given me a suggestion that on which part of my coding should be changed?

0
None
iceng

2 years ago

And your chief complaint question is ?

0
None
ANINDYAB5iceng

Answer 2 years ago

How shall I send the data continuously in Thingspeak