DATA NOT PLOTING VIA SERIAL AND THINGSPEAK

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);

}

thingsSpeak accepts sending data only once every 20 seconds.

Have you checked the baudrate of the serial monitor?

best regards Stefan

Thanks for response, yes the baudrate of serial monitor is correct on 9600, hence I have mentioned that once I comment out all the code related to ThingSpeak the serial monitor works, strange.

I will change my delay to 20 seconds to see if it works and give feedback thank you.

I have changed the delay, unfortunately still nothing comes out and I suspect the sensor is not even reading the temperature.

When I upload and run the code below, it works perfectly well and I can see the plot on serial,

// AutoPID - Version: Latest 
#include <PID_v1.h>
#include <SPI.h>
#include <Ethernet.h>

#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;
  
  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;

  delay(1000);
}

the problem starts when I attempt to send data to the ThingSpeak, then it does nothing .

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.