Matrix portal and Thingspeak

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tolisn
 
Posts: 15
Joined: Mon Feb 06, 2023 3:12 am

Matrix portal and Thingspeak

Post by tolisn »

Hi.

I'm trying to read some values from a thingspeak channel using the Adafruit Matrix Portal. I tried using the Thinspeak library but I get compilation error beyond my understanding.

Is there a demo or how-to for connecting to thingspeak using the matrix portal ?

User avatar
tolisn
 
Posts: 15
Joined: Mon Feb 06, 2023 3:12 am

Re: Matrix portal and Thingspeak

Post by tolisn »

I finally got it to work using the thingspeak lib. I will post the code here once I finish.

User avatar
tolisn
 
Posts: 15
Joined: Mon Feb 06, 2023 3:12 am

Re: Matrix portal and Thingspeak

Post by tolisn »

The below code works but I have a weird problem. When I power the unit while the Arduino IDE is launced the the unit power correclty and connects to the wifi network and to thingspeak. It also shows The time and the values of the thingspeak channel. When I close the Arduino IDE the unit does not connect to the wifi network. Why is this happening ?

Code: Select all

#include <Adafruit_Protomatter.h>
#include <Fonts/FreeSerifItalic9pt7b.h> // Large friendly font
#include <Fonts/TomThumb.h> // small font

#include <WiFi.h>
#include "secrets.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "ThingSpeak.h"

#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF

#if defined(_VARIANT_MATRIXPORTAL_M4_) // MatrixPortal M4
  uint8_t rgbPins[]  = {7, 8, 9, 10, 11, 12};
  uint8_t addrPins[] = {17, 18, 19, 20, 21};
  uint8_t clockPin   = 14;
  uint8_t latchPin   = 15;
  uint8_t oePin      = 16;
#endif

Adafruit_Protomatter matrix(
  64,          // Width of matrix (or matrices, if tiled horizontally)
  6,           // Bit depth, 1-6
  1, rgbPins,  // # of matrix chains, array of 6 RGB pins for each
  4, addrPins, // # of address pins (height is inferred), array of pins
  clockPin, latchPin, oePin, // Other matrix control pins
  true,       // No double-buffering here (see "doublebuffer" example)
  -2);         // Row tiling: two rows in "serpentine" path

char ssid[] = SECRET_SSID;   // your network SSID (name)
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient client;

// Weather station channel details
unsigned long weatherStationChannelNumber = SECRET_CH_ID_WEATHER_STATION;
const char* readAPIKey = SECRET_READ_APIKEY; // your ThingSpeak Read API Key

int statusCode = 0;
int field[8] = {1, 2, 3, 4, 5, 6, 7, 8};

int OutTemp;         // Field 1
float pressure;      // Field 2
int percentHumid;    // Field 3
float BattVolt;      // Field 4
float PanelVoltage;  // Field 5
float PanelCurrent;  // Field 6

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
int timeOffsetHours = 1; // Adjust this offset according to your needs

unsigned long previousTimeMillis = 0;
unsigned long previousTSUpdateMillis = 0;

bool displayTSValues = false;

void setup() {
  Serial.begin(115200);      // Initialize serial
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo native USB port only
  }

  // WiFi.mode(WIFI_STA);

  ThingSpeak.begin(client);  // Initialize ThingSpeak

  // 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 an open or WEP network
      Serial.print(".");
      delay(5000);
    }
    Serial.println("\nConnected");
  }
  
  // Initialize matrix...
  ProtomatterStatus status = matrix.begin();
  Serial.print("Protomatter begin() status: ");
  Serial.println((int)status);
  if (status != PROTOMATTER_OK) {
    // DO NOT CONTINUE if matrix setup encountered an error.
    while (1);
  }
  matrix.fillScreen(0); // clear the screen
  matrix.setCursor(5, 15); // set the cursor position
  matrix.setFont(&TomThumb); // Use nice bitmap font
  matrix.setTextColor(RED); // set the text color
  matrix.show(); // and show it

  timeClient.begin(); // Initialize NTPClient
  timeClient.setTimeOffset(timeOffsetHours * 10800); // Set the time offset in seconds
 }

void loop() {
  unsigned long currentMillis = millis();

  // Update time every second
  if (currentMillis - previousTimeMillis >= 1000) {
    previousTimeMillis = currentMillis;

    timeClient.update();
    String currentTime = timeClient.getFormattedTime();

    matrix.fillScreen(0); // clear the screen

    // Display current time
    matrix.setCursor(0, 15);
    matrix.setFont(&FreeSerifItalic9pt7b); // Use large friendly font
    matrix.setTextColor(WHITE);
    matrix.print(currentTime);

    if (displayTSValues) {
      // Display ThingSpeak values
      matrix.setCursor(0, 38);
      matrix.setFont(&TomThumb); // Use small font
      matrix.setTextColor(RED);
      matrix.print("Out Temp: " + String(OutTemp) + "°C");

      matrix.setCursor(0, 45);
      matrix.setTextColor(GREEN);
      matrix.print("Humidity: " + String(percentHumid) + " %");

      matrix.setCursor(0, 52);
      matrix.setTextColor(BLUE);
      matrix.print("Pressure: " + String(pressure) );

      matrix.setCursor(0, 59);
      matrix.setTextColor(CYAN);
      matrix.print("Batt. Volt: " + String(BattVolt) + " V");
    }

    matrix.show(); // and show it
  }

  // Update ThingSpeak values every 15 seconds
  if (currentMillis - previousTSUpdateMillis >= 15000) {
    previousTSUpdateMillis = currentMillis;
    
    // Read and store all the latest field values
    statusCode = ThingSpeak.readMultipleFields(weatherStationChannelNumber, readAPIKey);

    if (statusCode == 200) {
      // Fetch the stored data
      OutTemp = ThingSpeak.getFieldAsInt(field[0]); // Field 1
      pressure = ThingSpeak.getFieldAsFloat(field[1]); // Field 2
      percentHumid = ThingSpeak.getFieldAsInt(field[2]); // Field 3
      BattVolt = ThingSpeak.getFieldAsFloat(field[3]); // Field 4
      PanelVoltage = ThingSpeak.getFieldAsFloat(field[4]); // Field 5
      PanelCurrent = ThingSpeak.getFieldAsFloat(field[5]); // Field 6

      displayTSValues = true;
    }
    else {
      Serial.println("Problem reading channel. HTTP error code " + String(statusCode));
      displayTSValues = false;
    }
  }

  delay(100);
}

User avatar
tolisn
 
Posts: 15
Joined: Mon Feb 06, 2023 3:12 am

Re: Matrix portal and Thingspeak

Post by tolisn »

I got is working, there was a while statement just before the Serial.begin(9600) statement that made the program wait for a com port before executing. Since no comport was available to connect to it never continued to show anything.

Locked
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”