Having trouble with Adafruit IO connection

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
lecandas
 
Posts: 2
Joined: Fri Apr 02, 2021 4:24 pm

Having trouble with Adafruit IO connection

Post by lecandas »

Hello,

I want to upload some analog data to Adafruit IO.
Here is my tryout sketch, I am using AZ-Delivery ESP-32 Dev Kit C V4

I am getting an error when I do

Code: Select all

io.connect();
Logs:

Code: Select all

Hello!
Connecting to Wi-Fi........
Connected with IP: 192.168.1.118

------------------------------------
Connected...
Connecting to Adafruit IO
Status: 1 - Network disconnected., MQTT: [E][WiFiClientSecure.cpp:133] connect(): start_ssl_client: -1
2, Network: 1
Status: 1 - Network disconnected., MQTT: 2, Network: 1
Status: 1 - Network disconnected., MQTT: 2, Network: 1
Status: 1 - Network disconnected., MQTT: 2, Network: 1
Status: 1 - Network disconnected., MQTT: 2, Network: 1
Status: 1 - Network disconnected., MQTT: [E][WiFiClientSecure.cpp:133] connect(): start_ssl_client: -1
2, Network: 1
Status: 1 - Network disconnected., MQTT: 2, Network: 1
Sketch

Code: Select all

// System
#include <Arduino.h>
// OLED
#include <Adafruit_I2CDevice.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Fonts
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>
// WIFI & TIME
#include <WiFi.h>
#include <time.h>
// Cloud IO
#include <AdafruitIO_Wifi.h>

// Secrets
#include "config.h"

// NTP server to request epoch time
const char* ntpServer = "pool.ntp.org";

// Pot moisture percentage
int pot1 = 100;
int pot2 = 100;
int pot3 = 100;
int pot4 = 100;
int pot5 = 100;

// init
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASSWORD);

// set up the cloud io feed
AdafruitIO_Feed *feed1 = io.feed("pot-1");
AdafruitIO_Feed *feed2 = io.feed("pot-2");
AdafruitIO_Feed *feed3 = io.feed("pot-3");
AdafruitIO_Feed *feed4 = io.feed("pot-4");
AdafruitIO_Feed *feed5 = io.feed("pot-5");

void startSerial() {
  Serial.begin(9600);
  while(! Serial);
  Serial.println("Hello!");
}

void checkLCD() {
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.clearDisplay();
  display.display();
}

void startWifi() {
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.println("------------------------------------");
  Serial.println("Connected...");
}

void startTime() {
  configTime(0, 0, ntpServer);
}

void startCloudIO() {
    // connect to io.adafruit.com
  Serial.println("Connecting to Adafruit IO");
  io.connect();
 
  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print("Status: ");
    Serial.print(io.status());
    Serial.print(" - ");
    Serial.print(io.statusText());
    Serial.print(", MQTT: ");
    Serial.print(io.mqttStatus());
    Serial.print(", Network: ");
    Serial.print(io.networkStatus());
    Serial.println();
    delay(500);
    if (io.status() == AIO_DISCONNECTED) {
      io.connect();
      delay(1000);
    }
  }
 
  // we are connected
  Serial.println();
  Serial.println(io.statusText());
}

void setup() {
  startSerial();
  checkLCD();
  startWifi();
  startTime();
  startCloudIO();
}

void checkCloudIO() {
  // Check is required for all sketches.
  // it should always be present at the top of your loop function. 
  // it keeps the client connected to io.adafruit.com, and processes any incoming data.
  io.run();
}

void updatePotReadings() {
  pot1 = 100;
  pot2 = 93;
  pot3 = 44;
  pot4 = 52;
  pot5 = 99;
}

void flashAndClean() {
  display.clearDisplay();
  display.display();
  display.invertDisplay(true);
  delay(50);
  display.invertDisplay(false);
  delay(50);
  display.invertDisplay(true);
  delay(50);
  display.invertDisplay(false);
}

void drawCentreString(const String &buf, int y) {
  int16_t x1, y1;
  uint16_t w, h;
  display.getTextBounds(buf, 0, y, &x1, &y1, &w, &h);
  display.setCursor(round(64 - w/2), y);
  display.println(buf);
}

void printPotInfo() {
  flashAndClean();
  display.setTextColor(WHITE);

  display.setFont(&FreeSansBold12pt7b);
  drawCentreString(F("POT 1"), 25);
  display.setFont(&FreeSansBold18pt7b);
  drawCentreString(String(pot1) + "%", 60);
  display.display();
  delay(3000);

  flashAndClean();
  
  display.setFont(&FreeSansBold12pt7b);
  drawCentreString(F("POT 2"), 25);
  display.setFont(&FreeSansBold18pt7b);
  drawCentreString(String(pot2) + "%", 60);
  display.display();
  delay(3000);

  flashAndClean();

  display.setFont(&FreeSansBold12pt7b);
  drawCentreString(F("POT 3"), 25);
  display.setFont(&FreeSansBold18pt7b);
  drawCentreString(String(pot3) + "%", 60);
  display.display();
  delay(3000);

  flashAndClean();

  display.setFont(&FreeSansBold12pt7b);
  drawCentreString(F("POT 4"), 25);
  display.setFont(&FreeSansBold18pt7b);
  drawCentreString(String(pot4) + "%", 60);
  display.display();
  delay(3000);

  flashAndClean();
  
  display.setFont(&FreeSansBold12pt7b);
  drawCentreString(F("POT 5"), 25);
  display.setFont(&FreeSansBold18pt7b);
  drawCentreString(String(pot5) + "%", 60);
  display.display();
  delay(3000);
  
  flashAndClean();
}

unsigned long getTime() {
  time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return(0);
  }
  time(&now);
  return now;
}

void uploadToAdafruitIO() {
  Serial.println("Uploading pot information to AdafruitIO");
  feed1->save(pot1);
  feed2->save(pot2);
  feed3->save(pot3);
  feed4->save(pot4);
  feed5->save(pot5);
  Serial.println("Uploading finished!");
}

void loop() {
  checkCloudIO();
  updatePotReadings();
  printPotInfo();
  uploadToAdafruitIO();
  delay(6000);
}


User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Having trouble with Adafruit IO connection

Post by adafruit_support_mike »

[moved to the adafruit.io forum]

User avatar
lecandas
 
Posts: 2
Joined: Fri Apr 02, 2021 4:24 pm

Re: Having trouble with Adafruit IO connection

Post by lecandas »

I still couldn't find any solution for the issue

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”