thingspeak inclusion causing undesirable problems | Arduino | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
thingspeak inclusion causing undesirable problems
Avatar
disha
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
1
April 20, 2016 - 10:41 am
sp_Permalink sp_Print sp_EditHistory

I got a very basic program just reading sensor values
displayed on thingspeak with live updation w/o even requirement of
refreshing the page. Now I wanted to implement it in some other project
wherein I want to keep the source voltage at around a fixed decided
value in my case it's 14.85V-15V and I am continuously monitoring this
value and keeping same by either increasing or decreasing PWM given to
the MOSFET and then, also want to display these results of source
voltage on thingspeak.

Here is my program:

 

#include "TimerOne.h"
#include
int source_voltage = A0;
long int value_temp;
float analog(int);
float value_temp1;
int i=0,j=0;
int freq=10;
#define DEBUG FALSE //comment out to remove debug msgs

//*-- Hardware Serial
#define _baudrate 9600

//*-- Software Serial
//
#define _rxpin 0
#define _txpin 1
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX

//*-- IoT Information
#define SSID "disha"
#define PASS "fiftyfive55"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149

// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&field2=[data 2]...;
String GET = "GET /update?key=PT41QR66HL1NH8LU";

void setup() {
pinMode(freq,OUTPUT);
pinMode(source_voltage,INPUT);
Timer1.initialize(50); // initialize timer1, and set a 1/2 second period
Timer1.pwm(10, 0);
Serial.begin( _baudrate );
debug.begin( _baudrate );

sendDebug("AT");
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK
Data ready to sent!");
connectWiFi();
}

}

void loop() {
char buf[16];

while(analog(source_voltage)<15) //this loop maintains source voltage at 15V
{
i--;
if(i<0)
i=1;
for(j=0;j<=10;j++)
Timer1.setPwmDuty(10,i);
value_temp1=analog(source_voltage); //source voltage calculated and stored as float value
String temp=dtostrf(value_temp1,4,1,buf); //converts float to string as thingpeak accepts only string
updateTS(temp); //this calculated value is sent for updation on channel

delay(3000);
}
i++;
if(i>1023)
i=1022;
for(j=0;j<=10;j++)
Timer1.setPwmDuty(10,i);
value_temp1=analog(pulse);
String temp=dtostrf(value_temp1,4,1,buf);
updateTS(temp);
delay(3000); //wait as thingspeak updates every 3 seconds
}
float analog(int x) //subroutine to calculate source voltage
{
int i=0;
int out;
out =analogRead(x);
float out1=(out/1023.0)*4.9;
float out2=((out1)*(21.58+98.24))/(21.58);
return(out2);
}
//----- update the Thingspeak string with 3 values
void updateTS( String T)
{
// ESP8266 Client
String cmd = "AT+CIPSTART=\"TCP\",\"";// Setup TCP connection
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if( Serial.find( "Error" ) )
{
debug.print( "RECEIVED: Error
Exit1" );
return;
}

 cmd = GET + "&field1=" + T +"
";
Serial.print( "AT+CIPSEND=" );
Serial.println( cmd.length() );
if(Serial.find( ">" ) )
{
debug.print(">");
debug.print(cmd);
Serial.print(cmd);
}
else
{
sendDebug( "AT+CIPCLOSE" );//close TCP connection
}
if( Serial.find("OK") )
{
debug.println( "RECEIVED: OK" );
}
else
{
debug.println( "RECEIVED: Error
Exit2" );
}
}

void sendDebug(String cmd)
{
debug.print("SEND: ");
debug.println(cmd);
Serial.println(cmd);
}

boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");//WiFi STA mode - if '3' it is both client and AP
delay(2000);
//Connect to Router with AT+CWJAP="SSID","Password";
// Check if connected with AT+CWJAP?
String cmd="AT+CWJAP=\""; // Join accespoint
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
if(Serial.find("OK"))
{
debug.println("RECEIVED: OK");
return true;
}
else
{
debug.println("RECEIVED: Error");
return false;
}

cmd = "AT+CIPMUX=0";// Set Single connection
sendDebug( cmd );
if( Serial.find( "Error") )
{
debug.print( "RECEIVED: Error" );
return false;
}
}

Now in void loop you can see that there is a loop which always maintains the source voltage fixed at around 14.85 or 15V and the same is updated using function :::::----> updateTS(temp);

Here some contradictory results I have got that is:

1)When I just wanted to check source voltage whether remained at desire value w/o bothering for utilizing thingspeak evreything worked fine.

2)But in void loop when I included that function

updateTS(temp); The source voltage sat at open circuit voltage and the same values got displayed and when i commented that function and very next line i.e delay(3000); everything again worked. Those two code lines are very important for updating on thingspeak what do you'll think? Why is this happening? How can I solve this?

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
2
April 20, 2016 - 11:10 pm
sp_Permalink sp_Print

Note that Thingspeak updates can be no more often than 15 seconds so you need to choose a different service if you want 5 second updates.

Adding the call to updateTS() and the delay() adds at least 5 seconds to every iteration of the inner loop so by the time you do this 10 times almost a minute has passed before you can change the voltage at all.

Why do you call setPwmDuty() 10 times with the same value?  Seems pointless.. the value you set remains active on the pin until you change it.

I would try something like

v=analog(source_voltage)

if (v<14.85) {

  if (i<1022) i++;

}

if (v>15.0) {

  if (i>0) i--;

}

Timer1.setPwmDuty(10,i)

updateTS() // etc

 

So at least you can respond every 5 seconds or so.

 

If the load is likely to change more rapidly than this it will make it very hard to control it and make regular updates to Thingspeak

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: jacktheripper125, pudderz
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

bbruno22

pudderz

Utkya

Poornak26

LindasHed

reddysiva

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee