setHostname using AdafruitIO_WiFi

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
MHz000
 
Posts: 13
Joined: Tue Oct 27, 2015 5:37 am

setHostname using AdafruitIO_WiFi

Post by MHz000 »

how do I set the WiFi hostname while using AdafruitIO_WiFi?
Thank-you

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

Re: setHostname using AdafruitIO_WiFi

Post by adafruit_support_mike »

You don’t, really. That behavior lives in the WiFiNINA library:

https://www.arduino.cc/en/Reference/WiFiNINAsetHostname

AdafruitIO_WiFi uses WiFiNINA, so the code would be:

Code: Select all

    WiFi.setHostname( “my-server” );

User avatar
MHz000
 
Posts: 13
Joined: Tue Oct 27, 2015 5:37 am

Re: setHostname using AdafruitIO_WiFi

Post by MHz000 »

Mike
thank-you for answer my question. However I'still be in doubt where to put in the WiFi.setHostname statement. Maybe I have to include an other h-file or change the config.h. Please have a look at the config.h as is used by me. Compiling result is always:

'WiFi' does not name a type

I'm on a WIN10 system using platformIO to program an ESP8266 processor.
The source code is from
https://github.com/callimero/vindriktni ... ticles_2IO


Thank-you so much

/************************ Adafruit IO Config *******************************/
#define IO_USERNAME "***"
#define IO_KEY "***"

#define WIFI_SSID "***"
#define WIFI_PASS "***"

//WiFi.setHostname( "my-server" ); // <<<<<<<<<<<<<<<<<

#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || \
defined(ADAFRUIT_PYPORTAL)
#if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
#define SPIWIFI SPI
#define SPIWIFI_SS 10 // Chip select pin
#define NINA_ACK 9 // a.k.a BUSY or READY pin
#define NINA_RESETN 6 // Reset pin
#define NINA_GPIO0 -1 // Not connected
#endif
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS,
NINA_ACK, NINA_RESETN, NINA_GPIO0, &SPIWIFI);
#else
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
#endif
//WiFi.setHostname( "my-server"); // <<<<<<<<<<<<<<<<<

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

Re: setHostname using AdafruitIO_WiFi

Post by adafruit_support_mike »

Your config.h file should contain the line:

Code: Select all

#include "AdafruitIO_WiFi.h"
The file AdafruitIO_WiFi.h in a wrapper that brings in the appropriate header for the board you’re using. The ESP8266 section is:

Code: Select all

#include "wifi/AdafruitIO_ESP8266.h" 
typedef AdafruitIO_ESP8266 AdafruitIO_WiFi;
The file wifi/AdafruitIO_ESP8266.h pulls in several other headers:

Code: Select all

#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
The file ESP8266WiFi.h is the base ESP8266 interface. The last line of that file is:

Code: Select all

 extern ESP8266WiFiClass WiFi;
That creates the variable ‘WiFi’.

The file Adafruit_IO_Arduino/src/wifi/AdafruitIO_ESP8266.cpp uses WiFi to set up the ESP8266’s wifi:

Code: Select all

     WiFi.begin(_ssid, _pass);
So if your ESP8266 can talk to the network at all, the variable WiFi must exist.

User avatar
MHz000
 
Posts: 13
Joined: Tue Oct 27, 2015 5:37 am

Re: setHostname using AdafruitIO_WiFi

Post by MHz000 »

Mike
I'm steamrolled by your detailed answer - thank you! As a lightweight C++ user I tried to implement all your suggestions allthough not understandig them in full. Unfortuneally the compil error remains:
src\config.h:37:1: error: 'WiFi' does not name a type
Maybe I have put the WiFi.setHostname( "my-server"); in the wrong place or in the wrong file or somthing else is wrong with my sources or is missing.
Testing whithout calling "WiFi.setHostname( "my-server" );" by
io.connect(); // connect to io.adafruit.com
while (io.status() < AIO_CONNECTED) { // wait for a connection
Serial.print(".");
delay(500);
}
Serial.println(); // we are connected
Serial.println(io.statusText());
shows a established connection is in place.

Mike thank you for time and patience

/************************ Adafruit IO Config *******************************/

#define IO_USERNAME "***"
#define IO_KEY "***"
#define WIFI_SSID "worldgate"
#define WIFI_PASS "balt1m0r3"

#include "AdafruitIO_WiFi.h"
#include "wifi/AdafruitIO_ESP8266.h"
typedef AdafruitIO_ESP8266 AdafruitIO_WiFi;

#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
extern ESP8266WiFiClass WiFi;
//WiFi.setHostname( "my-server" ); // <<<<<<<<<<<<<<<<<

#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || \
defined(ADAFRUIT_PYPORTAL)
// Configure the pins used for the ESP32 connection
#if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
// Don't change the names of these #define's! they match the variant ones
#define SPIWIFI SPI
#define SPIWIFI_SS 10 // Chip select pin
#define NINA_ACK 9 // a.k.a BUSY or READY pin
#define NINA_RESETN 6 // Reset pin
#define NINA_GPIO0 -1 // Not connected
#endif
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS,
NINA_ACK, NINA_RESETN, NINA_GPIO0, &SPIWIFI);
#else
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
#endif
//WiFi.setHostname( "my-server");

//-------------main.cpp -----------------------------------------------------------------------
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "config.h"

#include "DHT.h"
#define DHTPIN 0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float t, newT; // DHT values
float h, newH; // as floatingP

AdafruitIO_Feed *pm25 = io.feed("PM25"); // set up the 'PM25' feed
AdafruitIO_Feed *pm1 = io.feed("PM1");
AdafruitIO_Feed *pm10 = io.feed("PM10");
AdafruitIO_Feed *hum = io.feed("Humidity");
AdafruitIO_Feed *temp = io.feed("Temperature");
//--------------------------------------ESP-01---pin assignment-------------------
constexpr static const uint8_t PIN_UART_RX = 2; // GPIO02 on ESP-01 Vindrikt yellow
constexpr static const uint8_t PIN_UART_TX = 0; // GPIO00 DHT11

SoftwareSerial sensorSerial(PIN_UART_RX, PIN_UART_TX);
uint8_t serialRxBuf[255];
uint8_t rxBufIdx = 0;

unsigned long currentMillis; // clock cycles
unsigned long previousMillis = 0; // last time DHT updated
const long interval = 2000; // update DHT intervall

void clearRxBuf();
void parseState();
bool isValidHeader();
bool isValidChecksum();
void handleUart();


void setup() {
Serial.begin(115200);
delay(200);
Serial.printf("\n\nCompiled from: %s at: %s %s", __FILE__, __DATE__, __TIME__);
Serial.println("\nIKEA windrikining");
sensorSerial.begin(9600); // Software Serial for Sensor
while (! Serial); // wait for serial monitor to open
Serial.print("Connecting to Adafruit IO");

io.connect(); // connect to io.adafruit.com
while (io.status() < AIO_CONNECTED) { // wait for a connection
Serial.print(".");
delay(500);
}
Serial.println(); // we are connected
Serial.println(io.statusText());
>>>>received ok! msg

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

Re: setHostname using AdafruitIO_WiFi

Post by adafruit_support_mike »

Try putting the WiFi.setHostname() in setup().

User avatar
MHz000
 
Posts: 13
Joined: Tue Oct 27, 2015 5:37 am

Re: setHostname using AdafruitIO_WiFi

Post by MHz000 »

Hi Mike et all
adafruit_support_mike wrote:Try putting the WiFi.setHostname() in setup().
I've already done this by using Adafruit supplied example "adafruit_00publish"

Code: Select all

void setup() {
  Serial.begin(115200);
  delay(200);
  Serial.printf("\n\nCompiled from: %s  at: %s %s", __FILE__, __DATE__, __TIME__);
  Serial.println("\nadafruit_IO testing");
  while(! Serial);                            // wait for serial monitor to open
  WiFi.setHostname("IKEA-1");
  //WiFi.begin();
  Serial.print("Connecting to Adafruit IO");
  io.connect();                               // connect to io.adafruit.com
  while(io.status() < AIO_CONNECTED) {        // wait for a connection
    Serial.print(".");
    delay(5000);
  }
  Serial.print("Mac address: "); Serial.println(WiFi.macAddress());
  Serial.print("IP address : "); Serial.println(WiFi.localIP());
  Serial.print("WiFi name  : "); Serial.println(WiFi.getHostname());

  Serial.println();
  Serial.println(io.statusText());            // we are connected
}
Here the result:
Connecting to Adafruit IO.
Mac address: B4:E6:2D:23:1B:2F
IP address : 192.168.178.21
WiFi name : ESP-231B2F

I would be pleased some one send a modifed and working version of the scetch. Pls have a look at the attaced sources
Thank-you so much
Attachments
config.h
(2.11 KiB) Downloaded 5 times
adafruitio_00_publish.cpp
(1.55 KiB) Downloaded 2 times

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”