0

Adafruit IO show me the temperature
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.
Please be positive and constructive with your questions and comments.

Adafruit IO show me the temperature

by Zagorov93 on Sun Sep 15, 2019 3:22 pm

Hello
I read many things in the forum but i can't find how to clear this mistake which the stream window is giving me :
2019/09/15 10:11pmerrorspublication to Relay4Temp failed: publish is not authorized, user is temporarily blocked.

I will post you the code if you can help me

Code: Select all | TOGGLE FULL SIZE
#include <DHTesp.h> //

  //Google Assistant Home Automation
  #include <Arduino.h> //
  #include <WiFi.h> //
  #include <analogWrite.h>
  #include "Adafruit_MQTT.h"
  #include "Adafruit_MQTT_Client.h"
 
  #define doorSwitch        2
  #define Relay1            5
  #define Relay2            18
  #define Relay3            19
 
  #define WLAN_SSID       "Galaxy S8+"             // Your SSID
  #define WLAN_PASS       "puux8218"        // Your password


/** *********** DHT11 - begin ************************** */
/** Initialize DHT sensor */
DHTesp dht;
/** Task handle for the light value read task */
TaskHandle_t tempTaskHandle = NULL;
/** Pin number for DHT11 data pin */
// Този пин трябва да промениш
// в зависимост от твоето свързване
int dhtPin = 4;
// Това са променливите в които се съхранява
// измерената температура и влажност
float Temperature = 0; // Температура
float Humidity =0;     // Влажност

/** ----------------------------------
// Initialize temperature sensor
   Извиква се в void setup() {}
*/
void initDHT11() { 
  dht.setup(dhtPin, DHTesp::DHT11);
}

// Reading temperature and humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)


void readTempAndHumidity() {
  TempAndHumidity lastValues = dht.getTempAndHumidity();
 Temperature = lastValues.temperature;
 Humidity =lastValues.humidity;
}
/** *********** DHT11 end ************************** */ 
  /************************* Adafruit.io Setup *********************************/
 
  #define AIO_SERVER      "io.adafruit.com" //Adafruit Server
  #define AIO_SERVERPORT  1883                   
  #define AIO_USERNAME    "Zagorov93"            // Username
  #define AIO_KEY         "AIO_KEY"   // Auth Key
 
  //WIFI CLIENT
  WiFiClient client;
 
  Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT,
  AIO_USERNAME, AIO_KEY);
 
  Adafruit_MQTT_Publish AC_off = Adafruit_MQTT_Publish(&mqtt,
  AIO_USERNAME "/feeds/Relay2"); // Feeds name should be same everywhere
 
  Adafruit_MQTT_Subscribe Oven = Adafruit_MQTT_Subscribe(&mqtt,
  AIO_USERNAME "/feeds/Relay1");
  Adafruit_MQTT_Subscribe AC = Adafruit_MQTT_Subscribe(&mqtt,
  AIO_USERNAME "/feeds/Relay2");
  Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt,
  AIO_USERNAME "/feeds/Relay3Slider");

// ------- Температура и влажност ----------------
  Adafruit_MQTT_Publish TempHum = Adafruit_MQTT_Publish(&mqtt,
  AIO_USERNAME "/feeds/Relay4Temp");

 
  void MQTT_connect();
 
  void setup() {
    /* Задаване на скорост на трансфер на данни между
    серийният порт на компютъра и ESP32 */
    Serial.begin(115200);
   // Инициализация на DHT11
   initDHT11(); 
 
    //Дефиниране на типа входно/изходни пинове
    pinMode(doorSwitch, INPUT);
    pinMode(Relay1, OUTPUT);
    pinMode(Relay2, OUTPUT);
    pinMode(Relay3, OUTPUT);
   
    // Свързване към безжичната мрежа.
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(WLAN_SSID);
 
    WiFi.begin(WLAN_SSID, WLAN_PASS);

    //Изчакване на връзка 
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    //Извеждане на информация относо свързаността
    Serial.println();
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    //Създаване на връзка за предаване на данни
    mqtt.subscribe(&Light1);
    mqtt.subscribe(&AC);
    mqtt.subscribe(&Oven);
// Създаване на връзка за предаване на данни за температура и влажност
 //   mqtt.publish(&TempHum);
   
   
  }

void loop() {
  // Измерване на температурата и влажността
  readTempAndHumidity();
  // ----------------
  MQTT_connect(); 
 
  Adafruit_MQTT_Subscribe *subscription;

  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &Oven) {
      Serial.print(F("Oven Got: "));
      const char* OvenCommand = (const char *)Oven.lastread;
      Serial.println(OvenCommand);
     
      if (strcmp(OvenCommand, "ON") == 0){
          digitalWrite(Relay1, HIGH);
      } else {
          digitalWrite(Relay1, LOW);
      }
      Serial.println("Done");
    }
   
    if (subscription == &AC) {
      Serial.print(F("AC Got: "));
      const char* ACCommand = (const char *)AC.lastread;
      Serial.println(ACCommand);
     
      if (strcmp(ACCommand, "ON") == 0){
          digitalWrite(Relay2, HIGH);
      } else {
          digitalWrite(Relay2, LOW);
      }
      Serial.println("Done");
    }
   
    if (subscription == &Light1) {
      Serial.print(F("Light Got: "));
      Serial.println((char *)Light1.lastread);
     
      uint8_t Light1_slide = atoi((char *)Light1.lastread) * 255/100;
      Serial.println(Light1_slide);
      analogWrite(Relay3, Light1_slide);
      Serial.println("Done");
    }

// --------------- Температура и влажност ---------------------
/*
 if (subscription == &TempHum) {
  Serial.println("Temperature: " + String(Temperature,0));
  Serial.println("Humidity: " + String(Humidity,0));
 

    }

   */
    Serial.println("Break");
   
  }

  if (digitalRead(Relay2) == HIGH ) {
    check_door();
  }
 
  Serial.println("Temperature: " + String(Temperature,0));
  Serial.println("Humidity: " + String(Humidity,0));
  // String sendStr = String("{ \"Temperature\": ") + String(Temperature,0) + String(",\"Humidity\":") + String(Humidity,0) + String("}");
 // String sendStr = "{\"Temperature\":123,\"Humidity\":456}";
    String webString = "{\"Temperature\":123}";
  //(username)/groups/(group name or key)
   Serial.println(webString);
  char buffer[200];
  uint8_t len = webString.length();
  webString.toCharArray((char *)buffer,len);
  //   mqtt.sendPacket(buffer,len);
  // mqtt.publish("esp32/temperature", tempString);
   // mqtt.publish("Zagorov93/Feeds/Relay4Temp", buffer); // towa e imeto Relay4Temp Грешката е в този текст
    mqtt.publish("Relay4Temp", buffer);
    // Виж този пример и се опитай да го направиш по него

/*
//payload formation begins here
String payload ="{";
payload +="\"Temp\":"; payload +=10; payload +=",";
payload +="\"Humi\":"; payload +=20;
payload +="}";

char attributes[1000];
payload.toCharArray( attributes, 1000 );
client.publish("test", attributes); //topic="test" MQTT data post
  */
}


void check_door() {
  if(digitalRead(doorSwitch) != HIGH) {
    delay(10000);
   
    if(digitalRead(doorSwitch) != HIGH) {
      AC_off.publish("OFF");
      digitalWrite(Relay2,LOW);
    }
  } 
}

void MQTT_connect() {
  int8_t ret;

  //Проверка дали вече е осъществена връзка
  if (mqtt.connected()) {
    return;
  }

  //Осъществяване на връзка
  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
 
  while ((ret = mqtt.connect()) != 0) { //Връща 0, ако връзката се осъществи
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);
    retries--;
    if (retries == 0) {
      while (1);
    }
  }
 
  Serial.println("MQTT Connected!"); 
}


Regards.

Zagorov93
 
Posts: 1
Joined: Sun Sep 15, 2019 1:13 pm

Re: Adafruit IO show me the temperature

by brubell on Mon Sep 16, 2019 9:18 am

Hi Zagorov93,

From the IO Website, are you able to update/add new data to the Relay2 feed?

brubell
 
Posts: 732
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.