Good day,
Can you assist with the following code, my results are not plotting via serial port and not posting to the thingSpeak channel. However when I comment out all the lines related to ThingSpeak , I am able to get the data via serial port.
I would appreciate if anyone would be able to pick up from my code what would be the problem.
Thank you all as you take your time to assist.
#include <PID_v1.h>
#include <SPI.h>
#include <Ethernet.h>
#include "ThingSpeak.h"
byte mac[] = { 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xCC}; //ETHERNET SHIELD MAC ADRESS
EthernetClient client;
unsigned long myChannelNumber = xxxxxxx; //ThingSpeak Channel ID
const char * myWriteAPIKey = "xxxxxxxxxxxxx"; //PRIVATE WRITE KEY to ThingSpeak
#define PIN_INPUT 0
#define PIN_OUTPUT 3
int data;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp = 5, Ki = 1, Kd = 0.5;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, REVERSE);
void setup()
{
pinMode(PIN_OUTPUT, OUTPUT);
pinMode(PIN_INPUT, INPUT);
//initialize the variables we're linked to
Input = analogRead(PIN_INPUT);
Setpoint = 30;
Ethernet.begin(mac);
ThingSpeak.begin(client);
Serial.begin(9600);
delay(1000);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = (5.0 * analogRead(PIN_INPUT) * 100.0) / 1024.0; //Converting to °C
myPID.Compute(); //applying PID tuning
analogWrite(PIN_OUTPUT, Output);
Serial.print("Temperature: ");
Serial.println(Input);
data = Input;
ThingSpeak.writeField(myChannelNumber, 1, data, myWriteAPIKey);
ThingSpeak.setField(1, data);
delay(1000);
}