ESP01Realy connecting with alexa

BASIC ESP8266 CONNECTION THROUGH FTDI

April 24th, 2017. I finally managed to upload a simple Arduino sketch onto my ESP-01 (esp8266) module, after I think roughly 3 years since I got it. I am so ashamed. But I decided that I will try to improve on this and that I will try to document decently every thing I achieve with these little modules.

Okay the components I needed to obtain these are few and simple, but it still took me ages to decide and get them.

  • FTDI usb to serial converter, with a 3.3V output: in this case I got one with both 5V and 3.3V operation, which is selected through a jumper. The main reason I didn’t use a simple Arduino to program the esp8266 is that one or two previous attempts had failed and got me discouraged.
  • 1x 10kΩ resistor (just for pull up)
  • 2x tactile button switches
  • 1x ESP8266 esp-01 board

The tactile switches and the resistor are not essential, but allow switching between flashing mode (writing to the esp8266 bootloader when the GPIO0 pin is to ground) and normal operation mode without having to replace wires by hand every time.

Below are the schematics for connections to flash the esp8266. Quite straightforward, there is the option with tactile switches and the one without. Make sure RX–>TX and TX–>RX, unless you are using an Arduino to program the esp8266 and not an FTDI converter.

After I managed to upload the first sketch (blink), I actually used another example to see if I could connect to my local WiFi network. From the ESP8266WiFi library, I used the WiFiWebServer example, which instantiates a web server in the ESP8266, which accepts GET requests. By connecting an LED to its GPIO2 pin, I could easily switch on and off the LED from a webpage. This was cool indeed. 90% of the work was actually getting the components and getting the right connections to flash the firmware onto the ESP8266. Even though in 2015 I think that the Arduino IDE could already flash the firmware using something called esptool, it has become now ridiculously easy to do it by just selecting “Generic ESP8266 module” in the target board of the Arduino IDE.

CONNECTION:




CODE:

#ifdef ENABLE_DEBUG

   #define DEBUG_ESP_PORT Serial

   #define NODEBUG_WEBSOCKETS

   #define NDEBUG

#endif 


#include <Arduino.h>

#if defined(ESP8266)

  #include <ESP8266WiFi.h>

#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)

  #include <WiFi.h>

#endif


#include "SinricPro.h"

#include "SinricProSwitch.h"


#define WIFI_SSID         "TambreResidence"

#define WIFI_PASS         "8088694461"

#define APP_KEY           "d1fb4ea1-4aad-4538-8733-646fcffaf697"

#define APP_SECRET        "af6ad023-3383-46f2-a9d1-07b0c4ee72f3-f518b279-5d55-4017-b7cc-d99d8cddc3fa"


#define SWITCH_ID_1       "63f6f9965ec7d92a47135a62"

#define RELAYPIN_1        0


#define BAUD_RATE         9600  // Reduce baud rate


bool onPowerState1(const String &deviceId, bool &state) {

  Serial.printf("[Callback] Device 1 (ID: %s) turned %s\r\n", deviceId.c_str(), state ? "on" : "off");

  digitalWrite(RELAYPIN_1, state ? HIGH : LOW);

  return true; // request handled properly

}


// setup function for WiFi connection

void setupWiFi() {

  Serial.println("[WiFi]: Connecting");


  #if defined(ESP8266)

    WiFi.setSleepMode(WIFI_MODEM_SLEEP);  // Adjust sleep mode

    WiFi.setAutoReconnect(true);

  #elif defined(ESP32)

    WiFi.setSleep(false); 

    WiFi.setAutoReconnect(true);

  #endif


  WiFi.begin(WIFI_SSID, WIFI_PASS);


  int attempts = 0;

  while (WiFi.status() != WL_CONNECTED && attempts < 20) {  // Increase connection attempts

    Serial.print(".");

    delay(500);

    attempts++;

  }


  if (WiFi.status() == WL_CONNECTED) {

    Serial.printf("\r\n[WiFi]: Connected! IP Address: %s\r\n", WiFi.localIP().toString().c_str());

  } else {

    Serial.println("\r\n[WiFi]: Connection failed. Check credentials and try again.");

  }

}


// setup function for SinricPro

void setupSinricPro() {

  // add devices and callbacks to SinricPro

  pinMode(RELAYPIN_1, OUTPUT);


  Serial.println("[SinricPro] Adding devices and callbacks");

  

  SinricProSwitch& mySwitch1 = SinricPro[SWITCH_ID_1];

  mySwitch1.onPowerState(onPowerState1);


  // setup SinricPro

  Serial.println("[SinricPro] Setting up SinricPro");

  SinricPro.onConnected([](){ Serial.println("[SinricPro] Connected to SinricPro"); }); 

  SinricPro.onDisconnected([](){ Serial.println("[SinricPro] Disconnected from SinricPro"); });

  

  Serial.println("[SinricPro] Beginning SinricPro");

  SinricPro.begin(APP_KEY, APP_SECRET);

}


// main setup function

void setup() {

  Serial.begin(BAUD_RATE); 

  Serial.println("\r\n\r\n[Main] Starting setup");

  setupWiFi();

  setupSinricPro();

  Serial.println("[Main] Setup completed");

}


void loop() {

  Serial.println("[Main] Looping");

  SinricPro.handle();

}


Post a Comment

My Instagram

Made with by Pi-girl | Copyright © 2020 Pi-girl