Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot setting hostname ESP32 (Arduino) #4074

Closed
alongkornchaisen001 opened this issue Jun 8, 2020 · 1 comment
Closed

Cannot setting hostname ESP32 (Arduino) #4074

alongkornchaisen001 opened this issue Jun 8, 2020 · 1 comment

Comments

@alongkornchaisen001
Copy link

alongkornchaisen001 commented Jun 8, 2020

I'm use only WiFi.setHostname("MyHostname_ESP32"); but It's not work anymore.

My code

#include <ESPmDNS.h>
#include <WiFi.h>
 
const char* ssid = "My SSID";
const char* password =  "My Password";
 
void setup() {
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
  WiFi.setHostname("myHost_ESP");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("Connecting...\n\n");
  }
 
  if(mdns_init()!= ESP_OK){
    Serial.println("mDNS failed to start");
    return;
  }
 
  int nrOfServices = MDNS.queryService("http", "tcp");
   
  if (nrOfServices == 0) {
    Serial.println("No services were found.");
  } 
   
  else {
     
    Serial.print("Number of services found: ");
    Serial.println(nrOfServices);
     
    for (int i = 0; i < nrOfServices; i=i+1) {
 
      Serial.println("---------------");
       
      Serial.print("Hostname: ");
      Serial.println(MDNS.hostname(i));
 
      Serial.print("IP address: ");
      Serial.println(MDNS.IP(i));
 
      Serial.print("Port: ");
      Serial.println(MDNS.port(i));
 
      Serial.println("---------------");
    }
  }
 
}
 
void loop() {}

@alongkornchaisen001
Copy link
Author

alongkornchaisen001 commented Jun 11, 2020

I change my some code and already fix it

#include <ESPmDNS.h>
#include <Arduino.h>
#include <Thing.h>
#include <WebThingAdapter.h>
#include <analogWrite.h>
#include <WiFi.h>

#define MIN(a,b) (((a)<(b))?(a):(b))
#define HOSTNAME "ESP32"

const char* ssid = "ssid";
const char* password = "password";

const int lampPin = LED_BUILTIN;


#define LEDC_CHANNEL_0     0
#define LEDC_TIMER_13_BIT  13
#define LEDC_BASE_FREQ     5000

WebThingAdapter* adapter;

const char* lampTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice lamp("mylamp", "My LED 2", lampTypes);

ThingProperty lampOn("on", "Whether the lamp is turned on", BOOLEAN, "OnOffProperty");
ThingProperty lampLevel("level", "The level of light from 0-100", NUMBER, "BrightnessProperty");

bool lastOn = false;

void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
    uint32_t duty = (8191 / valueMax) * MIN(value, valueMax);
    ledcWrite(channel, duty);
}

void setup(void){
    pinMode(lampPin, OUTPUT);
    digitalWrite(lampPin, LOW); // initially off

    // Setup timer and attach timer to a led pin
    ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
    ledcAttachPin(lampPin, LEDC_CHANNEL_0);
    
    Serial.begin(115200);
    Serial.println("");
    Serial.print("Connecting to \"");
    Serial.print(ssid);
    Serial.println("\"");

    WiFi.mode(WIFI_STA);  
    WiFi.begin(ssid, password);
    WiFi.setHostname(HOSTNAME); 
    Serial.println("");   
    
    while (WiFi.status() != WL_CONNECTED) {     
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.print("Subnet Mask: ");
    Serial.println(WiFi.subnetMask());
    Serial.print("Gateway IP: ");
    Serial.println(WiFi.gatewayIP());
    Serial.print("DNS 1: ");
    Serial.println(WiFi.dnsIP(0));
    Serial.print("DNS 2: ");
    Serial.println(WiFi.dnsIP(1));
    Serial.print("Hostname: ");
    Serial.println(WiFi.getHostname());
      
    adapter = new WebThingAdapter("led-lamp", WiFi.localIP());

    lamp.addProperty(&lampOn);
    lamp.addProperty(&lampLevel);
    adapter->addDevice(&lamp);
    adapter->begin();
    
    Serial.println("HTTP server started");
    Serial.print("http://");
    Serial.print(WiFi.localIP());
    Serial.print("/things/");
    Serial.println(lamp.id);

    #ifdef analogWriteRange
        analogWriteRange(255);
    #endif
}

void loop(void){
    adapter->update();
    int level = map(lampLevel.getValue().number, 0, 100, 255, 0);
    int levelShow = 255 - level;
   
    bool on = lampOn.getValue().boolean;
    digitalWrite(lampPin, on ? LOW : HIGH);
  
    if (lampOn.getValue().boolean) {

        Serial.print("Percent(%): ");
        Serial.println(lampLevel.getValue().number);
        ledcAnalogWrite(LEDC_CHANNEL_0, level);

        analogWrite(lampPin, level);
         
        Serial.print("level : ");
        Serial.println(levelShow);
        
        Serial.print(lamp.id);
        Serial.print(": ");
        Serial.println(on);

        Serial.println("*************************************");
    } else {
        Serial.print("Percent(%) : ");
        Serial.println(lampLevel.getValue().number);
        ledcAnalogWrite(LEDC_CHANNEL_0, 0);
        analogWrite(lampPin, 255); 
        
        Serial.print("Level : ");
        Serial.println(levelShow);
    
        Serial.print(lamp.id);
        Serial.print(": ");
        Serial.println(on);

        Serial.println("*************************************");
    }
    delay(500);
    lastOn = on;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant