Problem connecting reliably to Thingspeak over Ethernet
4 views (last 30 days)
Show older comments
I have been trying to set up a Feather MO plus Ethernet wing to connect to my Thingspeak account. The project will report temperature, humidity, atmospheric pressure and calculate dew point using a BMP280 sensor board every 60 seconds. I’ve written my own code, I’ve also tried other peoples code, I’ve updated all the libraries. The programs are short and simple and they will run for a while (several hours), then fail. When reporting via the IDE they report -304 or -302 errors first, then -301. When the code runs directly connected to the internet it runs for longer but then fails after a few days.
In my latest attempt I took a Mathworks code example directly from Github and only made minor changes to suit the Thingspeak format that I was sending the data to (see below). This code doesn’t use the BME280 or any other code at all, it is just the Mathworks code. The results were the same, it runs for a few hours then reports –304 (Timeout) a few times then recovers, then -302 (Unexpected fail) and then finally, repeated -301 (Connect fail). I suppose I could work around the issue with a Watch Dog Timer but that shouldn’t be necessary.
I now suspect that the problem may be with my router/modem settings (Arris DG3270A) – an area I know very little about. Can anyone suggest what the problem may be and how to fix it? Thank you.
Michael
/*
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 2020, The MathWorks, Inc.
*/
#include <Ethernet.h>
#include "Ethernetsecrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
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); //changed to 4 from 1
EthernetClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
// Initialize our values
int number1 = 56;
int number2 = random(20,30);
int number3 = random(900,1000);
int number4 = random(0,20);
void setup() {
Ethernet.init(10); // 10 is for 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));
}
number1 = 56;
number2 = random(20,30);
number3 = random(900,1000);
number4 = random(0,20);
delay(60000); // Wait 60 seconds to update the channel again
}
0 Comments
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on REST API 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!