Not sure if this is the correct place in the forum for this issue, but we're using the WiFi Rev2, so it seems like a good place to start.
I'm a high school teacher and a couple of students are building a project that requires calling ThingSpeak to get the current temperature as reported by our weather station. (public channel 930894, field 7).
We're using WiFiNINA. Our first attempt was using the thingspeak library, but get -304 errors ("Timeout waiting for server to respond"). When we play with settings, we sometimes get -301 errors ("Failed to connect to ThingSpeak"). Then we tried a sketch including HttpClient and directly attempting to GET the data from "https://api.thingspeak.com/channels/930894/fields/7/last.txt". We get http error code -1.
There is something about the configuration that is off, because when I took the arduino home and connected it to my wifi there, it worked as expected. But, the students, myself, and our tech person cannot figure out the problem. ip address, dns server, gateway, and subnet are all correct (tried both Wifi.config and the auto default).
Our code (based on combining the WiFiNina example with the ThingSpeak read example) is below. Sorry for the mess, as it's gotten sloppy during our attempts at trouble-shooting. Any help would be greatly appreciated. Thank you.
#include <WiFiNINA.h>
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
char ssid[] = "XXXXXXXXXXXX"; // your network SSID (name)
char pass[] = "XXXXXXXXXXXX"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
int status = WL_IDLE_STATUS;
IPAddress local_ip = IPAddress(172, 17, 0, 90);
IPAddress dns_server = IPAddress(192, 168, 10, 3);
IPAddress gateway = IPAddress(172, 17, 0, 1);
IPAddress subnet = IPAddress(255, 255, 254, 0);
// Counting channel details
unsigned long counterChannelNumber = 930894;
const char * myCounterReadAPIKey = "XXXXXXXXXXXX";
unsigned int counterFieldNumber = 7;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
WiFi.config(local_ip, dns_server, gateway, subnet);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
WiFi.setDNS(IPAddress(192,168, 10, 3));
ThingSpeak.begin(client); //initialize thingspeak
}
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
WiFi.setDNS(IPAddress(192, 168, 10, 3));
Serial.println("DNS set");
printCurrentNet();
printWifiData();
}
void loop() {
int statusCode = 0;
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
//Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(10000);
}
Serial.println("\nConnected");
}
// Read in field 1 of the private channel which is a counter
float count = ThingSpeak.readFloatField(counterChannelNumber, counterFieldNumber, myCounterReadAPIKey);
delay(10000);
// Check the status of the read operation to see if it was successful
statusCode = ThingSpeak.getLastReadStatus();
if(statusCode == 200){
Serial.println("Temperature: " + String(count));
}
else{
Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
}
delay(15000); // No need to read the counter too often.
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
//print subnet and gateway
IPAddress sub = WiFi.subnetMask();
Serial.print("Subnet Mask: ");
Serial.println(sub);
IPAddress gtwy = WiFi.gatewayIP();
Serial.print("Gateway: ");
Serial.println(gtwy);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}