Saturday, March 13, 2021

MQTT BROKER on ESP8266 or ESP32

 Since lately I received some messages about the old MQQT broker on ESP8266 I was thinking that it might be a good idea to restart the project.

The plan will be to moved it back to ESP32, where was originally started, in fact was started on ESP31B.

I will do that if there is some interest in this project, also if we can share the work.

Please let me know in the comments if this project makes sense or not. 


You can see more for:

ESP8266  https://myesp8266.blogspot.com/2016/11/mqtt-broker-on-esp8266-5.html

ESP32 https://myesp8266.blogspot.com/2016/01/mqtt-broker-on-esp32.html

Tuesday, November 10, 2020

Wemos D1 mini reconnecting all the time to Arduino IDE

 Lately I've acquired some Wemos D1 mini from Amazon and I've noticed an annoying behavior.

Every 2,3 seconds a disconnect and a reconnect sound was produced making impossible any software development on them along with any debugging. 

The problem was a the 3V3 on the CH340C (USB to serial).

After I've added a 100uF capacitor on the 3V3 and not everything is fine again.



Saturday, March 28, 2020

I2C Scanner

Since always when I am working with I2C devices I need the scanner I will post here for me and for others:

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(115200);
  Serial.println(F("\nI2C Scanner"));

  byte error, address;
  int nDevices;

  Serial.println(F("Scanning..."));

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {

    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print(F("I2C device found at address 0x"));
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println(F("  !"));

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print(F("Unknow error at address 0x"));
      if (address<16)
        Serial.print(F("0"));
      Serial.println(address,HEX);
    } 
  }
  if (nDevices == 0)
    Serial.println(F("No I2C devices found\n"));
  else
    Serial.println(F("done\n"));

}


void loop()
{}

Tuesday, December 18, 2018

Christmas special offer

Christmas is almost here and the PacktPub has a great offer for you.

Every ebook, every video for only $5. Go and browse the latest titles and maybe you will find to books you will read the next year.

The ESP8266 book is in the offer. Get it and start some new IoT project in the 2019!



Sunday, September 23, 2018

ESP8266 Home Automation Projects - Errata

After the ESP8266 Home Automation Projects book was published, the wunderground.com service is not offering free services anymore, so the code presented in the Getting data from the internet part need to be reviewed.

There are two options in here: either you are subscribing to the wunderground.com service, paying a monthly subscription or you can choose other provider like OpenWeatherMap.org

Go to openweathermap.org an create an account. After login you will find your own API Key that will be used later in the code.

Now the code for getting current data is:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const String OPENWEATHERMAP_API_KEY = "YOUR_OPENWEATHERMAP_API_KEY";
const String OPENWEATHERMAP_COUNTRY = "us";
const String OPENWEATHERMAP_CITY = "New York";
const String dataURL = "http://api.openweathermap.org/data/2.5/weather?q="+OPENWEATHERMAP_CITY+","+OPENWEATHERMAP_COUNTRY+"&APPID="+OPENWEATHERMAP_API_KEY;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(dataURL);

  if(WiFi.status() == WL_CONNECTED)
  {
    HTTPClient http;
    http.begin(dataURL);
    int httpCode = http.GET();
    if(httpCode> 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      // file found at server
      if(httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    }
  }
}

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

To check if the formed link is working, get the output from:

Serial.println(dataURL);

and check it in a browser. You should see the response in JSON format. I encourage you to test the links first in browser and later on the ESP8266.


For more information on how to get data you can access the link: https://openweathermap.org/current

At this time the free account has:

OpenWeatherMap Free Account



If you need to get the data every minute you can use the following code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const String OPENWEATHERMAP_API_KEY = "YOUR_OPENWEATHERMAP_API_KEY";
const String OPENWEATHERMAP_COUNTRY = "us";
const String OPENWEATHERMAP_CITY = "New York";
const String dataURL = "http://api.openweathermap.org/data/2.5/weather?q="+OPENWEATHERMAP_CITY+","+OPENWEATHERMAP_COUNTRY+"&APPID="+OPENWEATHERMAP_API_KEY;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(dataURL);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(WiFi.status() == WL_CONNECTED)
  {
    HTTPClient http;
    http.begin(dataURL);
    int httpCode = http.GET();
    if(httpCode> 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      // file found at server
      if(httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      }
    }

  }
delay(10*6000)
}

To make use of the results you will need to parse the JSON response and extract the data you need.