So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By istruttoresp
#86399 hi guys.
i'm new with ESP and not so new with arduino but... i'm here asking help.

i'm trying to connect to home wify with ESP but with a basic wify test it'works and with another sketch not.

the strange thing is:
the same sketch, with ssid Home, doesn't works
the same sketch , with ssid lab, in the same network but another AP it works well.

i'm trying to hunderstand the error but nothing to do.
the basic wifi sketch i made use the ESP8266WiFi.h library
the other sketch :
#include <Arduino.h>
#include <ESP8266WiFi.h>
//#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wik ... -libraries
#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wik ... -libraries
#include <StreamString.h>

//ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

I tryied to comment the line with wifi multi because i read this library can store AP data into the flash memory so...though it could be the problem but ...nothing to do.

i tryied with other 2 NodeMcu shield ... same problem.

i think there is a library conflict but i don't know where.

this is the wifi_test code: (this works)

Code: Select all#include <ESP8266WiFi.h>
#define WLAN_SSID       "Home"
#define WLAN_PASS       "BalgaL0nn076"

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

  Serial.println(F("Viva la Figa"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(2000);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:

}





The other sketch (works with thetering of phone very well too) but with AP home it doesn't works.

Code: Select all/*
 Version 0.3 - March 06 2018
 Grensom Version
*/

#include <Arduino.h>
#include <ESP8266WiFi.h>
//#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> //  https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
#include <StreamString.h>

//ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;

///////////WiFi Setup/////////////

#define MyApiKey "xxxxxxxxxxxx" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define WLAN_SSID "xxxxxxxxxxx" // TODO: Change to your Wifi network SSID
#define WLAN_PASS "xxxxxxxxxxx" // TODO: Change to your Wifi network password

/////////////////////////////////

#define HEARTBEAT_INTERVAL 300000 // 5 Minutes

uint64_t heartbeatTimestamp = 0;
bool isConnected = false;

void setPowerStateOnServer(String deviceId, String value);
void setTargetTemperatureOnServer(String deviceId, String value, String scale);



// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here

void turnOn(String deviceId) {
  if (deviceId == "xxxxxxxxxxxxxxxx") // Device ID of first device
  { 
    Serial.print("Turn on device id: ");
    Serial.println(deviceId);   
}
}

void turnOff(String deviceId) {
   if (deviceId == "xxxxxxxxxxxxxxxxxxx") // Device ID of first device
   { 
     Serial.print("Turn off Device ID: ");
     Serial.println(deviceId);
}
}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
  switch(type) {
    case WStype_DISCONNECTED:
      isConnected = false;   
      Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
      break;
    case WStype_CONNECTED: {
      isConnected = true;
      Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
      Serial.printf("Waiting for commands from sinric.com ...\n");       
      }
      break;
    case WStype_TEXT: {
        Serial.printf("[WSc] get text: %s\n", payload);
        // Example payloads

        // For Switch or Light device types
        // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html

        // For Light device type
        // Look at the light example in github
         
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject((char*)payload);
        String deviceId = json ["deviceId"];     
        String action = json ["action"];
       
        if(action == "setPowerState") { // Switch or Light
            String value = json ["value"];
            if(value == "ON") {
                turnOn(deviceId);
            } else {
                turnOff(deviceId);
            }
        }
      }
      break;
    case WStype_BIN:
      Serial.printf("[WSc] get binary length: %u\n", length);
      break;
  }
}

void setup() {
  Serial.begin(115200);
  delay(10);
    // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(2000);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // server address, port and URL
  webSocket.begin("iot.sinric.com", 80, "/");

  // event handler
  webSocket.onEvent(webSocketEvent);
  webSocket.setAuthorization("apikey", MyApiKey);
 
  // try again every 5000ms if connection has failed
  webSocket.setReconnectInterval(5000);   // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
}

void loop() {
  webSocket.loop();
 
  if(isConnected) {
      uint64_t now = millis();
     
      // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
      if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
          heartbeatTimestamp = now;
          webSocket.sendTXT("H");         
      }
  }   
}

// If you are going to use a push button to on/off the switch manually, use this function to update the status on the server
// so it will reflect on Alexa app.
// eg: setPowerStateOnServer("deviceid", "ON")

// Call ONLY If status changed. DO NOT CALL THIS IN loop() and overload the server.

void setPowerStateOnServer(String deviceId, String value) {
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  root["deviceId"] = deviceId;
  root["action"] = "setPowerState";
  root["value"] = value;
  StreamString databuf;
  root.printTo(databuf);
 
  webSocket.sendTXT(databuf);
}


thaks for any kind of help