#define IO_USERNAME "Systembolaget"
#define IO_KEY "aio_yAsV46FTitInnZz7pp8cAre063cJ"
#define WIFI_SSID "iPhone von Andreas"
#define WIFI_PASS "kb3cwxokgj7r"

#include "AdafruitIO_WiFi.h"

// Enable WiFi communication with the necessary credentials
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);

// Set up the AIO feeds
AdafruitIO_Feed *value_pm10 = io.feed("value_pm10");
AdafruitIO_Feed *value_humidity = io.feed("value_humidity");
AdafruitIO_Feed *value_temperature = io.feed("value_temperature");

const int timeIntervalPM10 = 5000; // Time between events in milliseconds
const int timeIntervalHumidity = 11000; // Time between events in milliseconds
const int timeIntervalTemperature = 7500; // Time between events in milliseconds

unsigned long timeNowPM10 = 0; // Timestamp for when the event was executed
unsigned long timeNowHumidity = 0; // Timestamp for when the event was executed
unsigned long timeNowTemperature = 0; // Timestamp for when the event was executed

float massConcentrationPm10p0 = 12.34;
float ambientHumidity = 56.78;
float ambientTemperature = 90.12;

void setup()
{
  Serial.begin(115200);

  // Connect to io.adafruit.com
  io.connect();

  // Wait for a connection
  while (io.status() < AIO_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }

  // You are connected
  Serial.println();
  Serial.println(io.statusText());
}

void loop()
{
  // io.run(); is required and should be executed at the start of the
  // loop() function to keep your client, your product, connected to
  // io.adafruit.com
  io.run();

  sendPM10();
  sendHumidity();
  sendTemperature();
}

void sendPM10()
{
  // Is it time to execute the event code?
  if (millis() - timeNowPM10 >= timeIntervalPM10)
  {
    value_pm10->save(massConcentrationPm10p0);
    Serial.println("Sending PM10");

    // Update the timestamp with the time at which the event was executed
    timeNowPM10 = millis();
  }
}

void sendHumidity()
{
  // Is it time to execute the event code?
  if (millis() - timeNowHumidity >= timeIntervalHumidity)
  {
    value_humidity->save(ambientHumidity);
    Serial.println("Sending Humidity");

    // Update the timestamp with the time at which the event was executed
    timeNowHumidity = millis();
  }
}

void sendTemperature()
{
  // Is it time to execute the event code?
  if (millis() - timeNowTemperature >= timeIntervalTemperature)
  {
    value_temperature->save(ambientTemperature);
    Serial.println("Sending Temperature");

    // Update the timestamp with the time at which the event was executed
    timeNowTemperature = millis();
  }
}