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?