XRAD
Well-known member
I have this sketch working perfectly with adafruit feather M0 wifi ATWINC1500, but I want to use my teensy 4.1 for my project. I can connect to wifi and openweather.com with my API and 'GET' my data string using adafruit ATWINC1500 breakout...but I can't seem to parse it. I am using arduinojson 5.13.5 (yes, I know there is a newer '6' version..but ...this was working fine on the M0)
NO errors on compile or upload to teensy 4.1.....
any thoughts or pointers?
my arduino sketch:
NO errors on compile or upload to teensy 4.1.....
any thoughts or pointers?
my arduino sketch:
Code:
#include "arduino_secrets.h"
#include <WiFi101.h>
#include <ArduinoJson.h>//version=5.13.5!!! Not updated to version 6 yet
#define MOSI 11
#define MISO 12
#define SCK 13
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PSW;// your network PASSWORD ()
//open weather map api key(get the free version, takes a few hours before working)
String apiKey = SECRET_APIKEY;
//the city you want the weather for
String location = "canton,US";//use correct city,country code
int status = WL_IDLE_STATUS;
char server[] = "api.openweathermap.org";
WiFiClient client;
void setup() {
//Configure pins for Adafruit ATWINC1500 Feather
//using hardware mosi 11, miso 12, sck 13
WiFi.setPins(8, 7, 4, 2);//(cs,irq, rst, enabl);
// 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
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// 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:
status = WiFi.begin(ssid, pass);
// wait 5 seconds for connection:
delay(5000);
}
// you're connected now, so print out the data:
Serial.println("You're connected to the network");
//show some info
printCurrentNet();
printWifiData();
}
void loop() {
getCurrentWeather();
delay(5000);
getWeatherForcast();
delay(5000);
}
void getCurrentWeather() {
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.print("GET /data/2.5/weather?");//current
//client.print("GET /data/2.5/forecast?");//future
client.print("q=" + location);
client.print("&appid=" + apiKey);
client.print("&cnt=1");//just for current forecast
//client.print("&cnt=3");//forcast x3 ahead
client.println("&units=imperial");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
} else {
Serial.println("unable to connect");
}
delay(1000);
String line = "";
while (client.connected()) {
line = client.readStringUntil('\n');
Serial.println(line);
Serial.println("parsingValues");
//create a json buffer where to store the json data
StaticJsonBuffer<5000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(line);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
//get the data from the json tree
String currentWeather = root["weather"][0]["main"];
float visNow = root["visibility"];
float tempNow = root["main"]["temp"];
float pressNow = root["main"]["pressure"];
float humNow = root["main"]["humidity"];
float windNow = root["wind"]["speed"];
String myCity = root["name"];
Serial.println(currentWeather);
Serial.print("Vis: ");
Serial.println(visNow);
Serial.print("Temp: ");
Serial.println(tempNow);
Serial.print("Press: ");
Serial.println(pressNow);
Serial.print("Humidity: ");
Serial.println(humNow);
Serial.print("Wind Speed: ");
Serial.println(windNow);
Serial.print("Location: ");
Serial.println(myCity);
//jsonBuffer.clear();//not needed
}
}
void getWeatherForcast() {
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.print("GET /data/2.5/forecast?");
client.print("q=" + location);
client.print("&appid=" + apiKey);
client.print("&cnt=3");
client.println("&units=metric");
client.println("Host: api.openweathermap.org");
client.println("Connection: close");
client.println();
} else {
Serial.println("unable to connect");
}
delay(1000);
String line = "";
delay(4000);
while (client.connected()) {
line = client.readStringUntil('\n');
//Serial.println(line);
Serial.println("parsingValues");
//create a json buffer where to store the json data
StaticJsonBuffer<5000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(line);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
//get the data from the json tree
String nextWeatherTime0 = root["list"][0]["dt_txt"];
String nextWeather0 = root["list"][0]["weather"][0]["main"];
String nextWeatherTime1 = root["list"][1]["dt_txt"];
String nextWeather1 = root["list"][1]["weather"][0]["main"];
String nextWeatherTime2 = root["list"][2]["dt_txt"];
String nextWeather2 = root["list"][2]["weather"][0]["main"];
// Print values.
Serial.println(nextWeatherTime0);
Serial.println(nextWeather0);
Serial.println(nextWeatherTime1);
Serial.println(nextWeather1);
Serial.println(nextWeatherTime2);
Serial.println(nextWeather2);
}
}
void printWifiData() {
// print your WiFi shield'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 : ");
Serial.print(mac[5], HEX);
Serial.print(" : ");
Serial.print(mac[4], HEX);
Serial.print(" : ");
Serial.print(mac[3], HEX);
Serial.print(" : ");
Serial.print(mac[2], HEX);
Serial.print(" : ");
Serial.print(mac[1], HEX);
Serial.print(" : ");
Serial.println(mac[0], HEX);
}
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 : ");
Serial.print(bssid[5], HEX);
Serial.print(" : ");
Serial.print(bssid[4], HEX);
Serial.print(" : ");
Serial.print(bssid[3], HEX);
Serial.print(" : ");
Serial.print(bssid[2], HEX);
Serial.print(" : ");
Serial.print(bssid[1], HEX);
Serial.print(" : ");
Serial.println(bssid[0], HEX);
// 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();
}
Last edited: