Go Down

Topic: Problem updating Thinkspeak channel. HTTP error code 302 (Read 355 times) previous topic - next topic

mb1234

I connected BMP280 to arduino uno with Ethernet Shield. What I am trying to do is, to send info about temperaure from arduino in my room to my Thinkspeak channel. So I used example code for arduino thinkspeak library and I rearrange it a little, but when I test it, I get a message in serial monitor that says: "Problem updating channel. HTTP error code -302". I really can't figure it out what the problem is.
What should I do?

Thanks.

Franz

pylon

HTTP response code 302 is a redirect. You've chosen an URL that isn't available anymore. As you neither posted your code nor a link to the used library we cannot help you further.

mb1234

So that is the code:
Code: [Select]

#include "ThingSpeak.h"
#include <SPI.h>
#include <Ethernet.h>
#include "secrets.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


Adafruit_BMP280 bme;
byte mac[] = SECRET_MAC;

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

EthernetClient client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);

void setup() {
  Ethernet.init(10);  // Most Arduino Ethernet hardware
  Serial.begin(115200);  //Initialize serial
   
  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
 
  ThingSpeak.begin(client);  // Initialize ThingSpeak
   if (!bme.begin()) { 
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }
 
}

void loop() {
  // set the fields with the values
  ThingSpeak.setField(1, bme.readTemperature());
  ThingSpeak.setField(2, bme.readPressure());
  ThingSpeak.setField(3, number3);
  ThingSpeak.setField(4, number4);

 
  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
 
  // change the values
  number1++;
  if(number1 > 99){
    number1 = 0;
  }
  number2 = random(0,100);
  number3 = random(0,100);
  number4 = random(0,100);
 
  delay(20000); // Wait 20 seconds to update the channel again
}

and here is secrets.h file:
Code: [Select]

#define SECRET_MAC {0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // I used my shield's MAC instead of zeroes
#define SECRET_CH_ID 000000 // and here I used my channel id instead of 0000000
#define SECRET_WRITE_APIKEY "XYZ"   // here I replaced XYZ with my channel write API Key

tasmod

I did mine slightly different. 302 is an unknown error.

The values are ints. You have initialised them with a value from the example but haven't actually set them to your value.

So say , 

int temperature = bme.readTemperature();

and

ThingSpeak.setField(1, temperature);

mb1234

Ok, I think I am onto something. When I declare temperature and pressure by myself, it works perfectly, but when temperature should be read from sensor it gives me an error. Here are both exemples of code.

When I do that, it works:
Code: [Select]

 float t =  22.14;
 float p = 1234.23;
 
 ThingSpeak.setField(1, t);
 ThingSpeak.setField(2, p)


But in that case I get an error (HTTP error code 302):
Code: [Select]

  float t =  bme.readTemperature();
  float p = bme.readPressure();

  ThingSpeak.setField(1, t);
  ThingSpeak.setField(2, p);

tasmod

Ok floats as you want decimal point.

If you are using software SPI I don't see an SPI pin definition, as in....

Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

or check your pins if using hardware SPI  (Don't know your board)

In loop you need this first to get your values

 bme.readSensor();


mb1234

I connected BMP280 to arduino as I2C, that's why I didn't declare SPI pins.
I tried to put bme.readSensor(); in my loop but it says " 'class Adafruit_BMP280' has no member named 'readSensor' "

tasmod

Hmm. I must have used an earlier library.  It was some time ago.

What do you get if you print to serial? Set baud rate at 9600 in setup.

Serial.begin(9600);

then in loop


Code: [Select]
Serial.print(bme.readTemperature());
Serial.println(" *C");
delay(1000);


open serial monitor to see temp

mb1234

I couldn't upload the image but I think it works fine. I got something like this:

22.64 *C
22.63 *C
22.63 *C
22.64 *C
22.63 *C
22.64 *C
22.64 *C
22.64 *C
22.64 *C
22.64 *C
22.64 *C
22.63 *C
22.64 *C
22.63 *C
22.64 *C

tasmod

Ok, so the read of the sensor is ok.

Can you post the code you are using at moment.  It seems that it is not uploading the data.

mb1234

It's not much different from the one I've posted before:

Code: [Select]

#include "ThingSpeak.h"
#include <SPI.h>
#include <Ethernet.h>
#include "secrets.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


Adafruit_BMP280 bme;
byte mac[] = SECRET_MAC;

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

EthernetClient client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

// Initialize our values
int number1 = 0;
int number2 = random(0,100);
int number3 = random(0,100);
int number4 = random(0,100);

void setup() {
  Ethernet.init(10);  // Most Arduino Ethernet hardware
  Serial.begin(115200);  //Initialize serial
   
  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
 
  ThingSpeak.begin(client);  // Initialize ThingSpeak
   if (!bme.begin()) { 
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }
 
}

void loop() {

  int temperature = bme.readTemperature();
  int pressure = bme.readPressure();


  ThingSpeak.setField(1,temperature);
  ThingSpeak.setField(2, pressure);
 

 
  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
 
  // change the values
  number1++;
  if(number1 > 99){
    number1 = 0;
  }
  number2 = random(0,100);
  number3 = random(0,100);
  number4 = random(0,100);
 
  delay(20000); // Wait 20 seconds to update the channel again
}

tasmod

Ok, let's just try this code.  Run the serial monitor and reset the Arduino to see all serial print messages including connection messages.

Code: [Select]


#include <ThingSpeak.h>
#include <SPI.h>
#include <Ethernet.h>
#include <secrets.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>


Adafruit_BMP280 bme;
byte mac[] = SECRET_MAC;

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

EthernetClient client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;


void setup() {
  Ethernet.init(10);  // Most Arduino Ethernet hardware
  Serial.begin(115200);  //Initialize serial

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);

  ThingSpeak.begin(client);  // Initialize ThingSpeak
  if (!bme.begin()) {
    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
    while (1);
  }

}

void loop() {

  int temperature = bme.readTemperature();
  int pressure = bme.readPressure();
 
  Serial.print(temperature);
  Serial.println(" *C");

  ThingSpeak.setField(1, temperature);
  ThingSpeak.setField(2, pressure);



  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if (x == 200) {
    Serial.println("Channel update successful.");
  }
  else {
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }

  delay(5000); // Wait 5 seconds to update the channel again
}


mb1234

That's what I get:


Initialize Ethernet with DHCP:
  DHCP assigned IP 192.168.1.82
21.35 *C
Problem updating channel. HTTP error code -302

tasmod

OK that means all is successful up to the point you try to upload the values.

The only thing stopping that is the possibility your entry in the secret file is incorrect by one number or some such.

I tried the code with my setup and it worked ok, so somethings different.

Try a direct route....change to suit your codes
from this..
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

to this.....

unsigned long myChannelNumber = 12345;
const char * myWriteAPIKey = "AB12AAAAWRITE004A";

I missed this... change delay back to 20000


mb1234

I tried that but ...


Initialize Ethernet with DHCP:
  DHCP assigned IP 192.168.178.75
Problem updating channel. HTTP error code -302


... I still get this

Go Up