Arduino built in example "WriteMultipleFields" gets error code -301
Show older comments
I am using an Arduino Uno R3 board and Ethernet shield. I replaced the Channel ID and the write API key with my own in the secrets.h file.
It connects with DHCP ok.
Arduino IDE 1.8.12
But comes back with error code -301. Serial Monitor:
Initialize Ethernet with DHCP:
DHCP assigned IP 10.10.1.170
Problem updating channel. HTTP error code -301
What am I missing?
/*
WriteMultipleFields
Description: Writes values to fields 1,2,3 and 4 in a single ThingSpeak update every 20 seconds.
Hardware: Arduino Ethernet
!!! IMPORTANT - Modify the secrets.h file for this project with your network connection and ThingSpeak channel details. !!!
Note:
- Requires the Ethernet library
ThingSpeak ( https://www.thingspeak.com ) is an analytic IoT platform service that allows you to aggregate, visualize, and
analyze live data streams in the cloud. Visit https://www.thingspeak.com to sign up for a free account and create a channel.
Documentation for the ThingSpeak Communication Library for Arduino is in the README.md folder where the library was installed.
See https://www.mathworks.com/help/thingspeak/index.html for the full ThingSpeak documentation.
For licensing information, see the accompanying license file.
Copyright 2018, The MathWorks, Inc.
*/
#include "ThingSpeak.h"
#include <Ethernet.h>
#include "secrets.h"
byte mac[] = SECRET_MAC;
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(10, 10, 1, 76);
IPAddress myDns(10, 10, 1, 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
}
void loop() {
// set the fields with the values
ThingSpeak.setField(1, number1);
ThingSpeak.setField(2, number2);
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
7 Comments
Vinod
on 7 Jul 2020
Do you see any data in your ThingSpeak channel? Does the network you are connecting to put up a captive portal where you have to sign in before you can access the internet?
Jonathan Baker
on 8 Jul 2020
Vinod
on 9 Jul 2020
We investigated this on our end and are unable to reproduce what you see. It appears to be some kind of connectivity issue on your device. Perhaps the IP address is clashing with some other device on the network. Do you see this printed in the serial console?
"Failed to configure Ethernet using DHCP"
If so, try different IP addresses. You will neet to log in to your gateway (10.10.1.1) and find an IP address that is not used. If the connection is successful, you should be able to see the new connection from your device in the gateway/router's page.
Jonathan Baker
on 24 Jul 2020
Vinod
on 28 Jul 2020
You can go to the Channel's Data Import/Export tab and download all historical data. If you see the number of entries increasing, it most likely points to a data encoding issue in what is being sent to ThingSpeak. You should be able to see that when you download the CSV file from the import/export tab.
Gerardo Spinelli
on 5 Nov 2020
I have the same problem with an Arduino MKR 1000. But in my case he issue is intermittent.
Vinod
on 12 Nov 2020
Answers (2)
Have you made sure you're using the latest version of the library? Are you unable to see channel updates with the examples that ship with the library?
tebraxin tebraxin
on 7 Sep 2021
0 votes
hello to everybody
i'm also using Arduino Uno with ethernet shield, and I still have error 301 with the example code (write multiple field)
I already read a lot of forum, questions etc.. all around internet but I still have the problem.
My network work cooretly with all my divecies,
I tried to change internal IP address, DNS IP address, cables, example codes form library Tingspeak... without solving the problem
thanks
Communities
More Answers in the ThingSpeak Community
Categories
Find more on ThingSpeak in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
