Showing posts with label ESP8266. Show all posts
Showing posts with label ESP8266. Show all posts

Wednesday, March 3, 2021

Install Arduino IDE 2.0 beta on Linux Mint 20.1, include ESP32/ESP8266 support.

Arduino IDE 2.0 (beta) is now available (refer to announcement in Arduino blog). It,s my first try to install Arduino IDE 2.0 beta (2.0.0-beta3) on Linux Mint 20.1, tested with VirtualBox 6.1/Windows 10.

Download Arduino IDE 2.0 beta

Visit https://www.arduino.cc/en/software, scroll download to download Arduino IDE 2.0 beta for Linux 64 bits (X86-64).

Run arduino-ide

Extract the downloaded file to a any folder you want. Run arduino-ide in Terminal
$ ./arduino-ide

Install board

In a fresh new installed Arduino IDE 2.0, no board is installed, you will be report with error:

Compilation error: Error: 2 UNKNOWN: platform not installed

Click Boards Manager on left, search to install Arduino AVR Boards by Arduino.

Add permission to user, to access USB

In a new Linux, you (as a regular user) have no permission to access USB port, and report with error:

avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied

Open Terminal, enter the command to add permission:
$ sudo usermod -a -G dialout <username>
$ sudo chmod a+rw <port>

Done.



Install board support for ESP32/ESP8266


Menu > File> Preferences
Enter the url in the "Additional Board Manager URLs":

Then you can add board of ESP32/ESP8266 in Boards Manager.

Setup Python

By default Linux Mint 20.1 pre-install Python3, but no Python2. Currently, esptool for ESPs call python. You can create a symlinks /usr/bin/python to python3 by installing python-is-python3.

$ sudo apt install python-is-python3

Optionally, you can prevent Python 2 from being installed as a dependency of something in the future:

$ sudo apt-mark hold python2 python2-minimal python2.7 python2.7-minimal libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib

And you have to install pyserial with pip:

$ sudo apt install python3-pip
$ pip3 install pyserial

Add permission to your user (if not do in above steps):

Additionally, if you cannot download your code to board caused by:
avrdude: ser_open(): can't open device "/dev/xxx": Permission denied

$ sudo usermod -a -G dialout <username>
$ sudo chmod a+rw <port>











Sunday, August 16, 2020

NodeMCU (ESP8266) + 1.44" 128x128 TFT with ST7735 SPI driver (KMR1441_SPI V2)





This video show how to driver 1.44" 128x128 TFT with ST7735 SPI driver (KMR1441_SPI V2) with NodeMCU (ESP8266) using ssd1306 library. Using Arduino IDE.

- In Arduino IDE, open library manager, search ST7735, and install ssd1306 library.

- Open Example > ssd1306 > demos > st7735_demo

- Connect ESP8266 to LCD
ESP8266 LCD
===================
3V3 VCC
GND GND
D1 A0 (D/C)
D2 CS (CS)
RX RESET (RES)
D7 SDA (DIN)
D5 SCK (CLK)
LED (Open in my test)

Saturday, July 11, 2020

BasicOTA: Program ESP32 (or ESP8266) via ArduinoOTA, on Windows 10/Arduino IDE

One of the very useful feature of ESP32/ESP8266 is OTA (Over-The-Air) programming; such that you can update the firmware without physically connect to the device.

With ESP32/ESP8266 core for Arduino installed to your Arduino IDE, you can find ArduinoOTA examples of BasicaOTA and OTAWebUpdater under Examples for ESP32 Dev Module.


This post show trying BasicOTA examples on ESP32-DevKitC using Arduino IDE running on Windows 10, and fix the problem I faced:  Network Ports no shown and No response from device.


Basically, the default DevKitC firmware have no OTA function. So you have to flash the first firmware with OTA function using serial port (USB). After then, you can update your firmware via OTA without physical connected serial port. Both the computer used to update the firmware and the ESP devices to be updated have to be in the same network.

The ESP device I used in this example is ESP32-DevKitC with ESP32-WROOM-32 module. It should be a relatively old and original version.


Somebody (include me) reported there are no Network Ports shown up. 


Somebody suggested to install Python, somebody suggested to reboot router, or install Bonjour... All of them not work for me. In order to eliminate the problem outside my computer, I decide to use Windows 10's Mobile hotspot.


Open BasicOTA example and change ssid and password according to my network setting. Upload the ESP32 device via serial port (USB, or COM5 in this example).

Turn on Windows 10's Mobile hotspot with matched ssid/password.

Now the Network ports shown, with attached device (the programmed ESP32 with OTA function) and its assigned ip.


Once Network port selected, the Arduino IDE's Serial Monitor not work on network. So I run Putty to monitor the output from ESP device via serial port.

Now, upload your second program via OTA. But still fail due to No response from device.


Now, open Windows Security and Allow espota.exe to communicate through Windows Defender Firewall.

Upload again, and Done uploading.
(In the various steps, the assigned ip of the ESP device may be changed. Double check the ip in Putty and select the Network port accordingly.)


Thursday, June 25, 2020

Install ESP32/ESP8266 to Arduino IDE on Ubuntu 20.04, with setup Pythton & serial

After install JDK (OpenJDK) and Arduino IDE on Ubuntu 20.04, you can add support of ESP32 (or ESP8266).

May be you will be with Python 2 related error of:
exec: "python": executable file not found in $PATH
ModuleNotFoundError: No module named 'serial'

In 20.04 LTS, the python included in the base system is Python 3.8. Python 2.7 has been moved to universe and is not included by default in any new installs. (referene: Ubuntu 20.04 LTS (Focal Fossa) release notes)

Here show how to fix it:


First, install board to Boards Manager:

Menu > File> Preferences
Enter the url in the "Additional Board Manager URLs":
https://dl.espressif.com/dl/package_esp32_index.json (for ESP32)
http://arduino.esp8266.com/stable/package_esp8266com_index.json (for ESP8266)

To enter more than one URL, separate it with a comma.

Menu > Tools > Board > Boards Manager…
Search and install ESP32 (or ESP8266)

When you build your code for ESP32/ESP8266, if you report with error of :
exec: "python": executable file not found in $PATH

It's because Python 2 is not installed on Ubuntu 20.04. You can create a symlinks /usr/bin/python to python3 by installing python-is-python3.

$ sudo apt install python-is-python3

Optionally, you can prevent Python 2 from being installed as a dependency of something in the future:

$ sudo apt-mark hold python2 python2-minimal python2.7 python2.7-minimal libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib

Then you may be report with error:
ModuleNotFoundError: No module named 'serial'

Because pyserial is not installed in Python 3 by default. Install with pip:

$ sudo apt install python3-pip
$ pip3 install pyserial

Additionally, if you cannot download your code to board caused by:
avrdude: ser_open(): can't open device "/dev/xxx": Permission denied

Add permission to your user:

$ sudo usermod -a -G dialout <username>
$ sudo chmod a+rw /dev/xxx


Next:
ESP32-DevKitC + 2.8inch 240x320 SPI TFT (ILI9341) using TFT_eSPI library
ESP32 + 1.3 inch 240x240 IPS LCD (ST7789 SPI interface), using TFT_eSPI library
NodeMCU (ESP8266) + 1.44" 128x128 TFT with ST7735 SPI driver (KMR1441_SPI V2), using ssd1306 library

Monday, May 18, 2020

ESP8266 (NodeMCU) - Get current weather data from OpenWeatherMap, using ArduinoJson

This exercise run on ESP8266 (NodeMCU), to get current weather data from OpenWeatherMap.


OpenWeatherMap provide easy-to-work weather APIs. Before you can use the APIs, you have to sign-up your account with email, for free.

After sign-up, and email varified, you can get various weather info using APIs with your api key. For example to get current weather:
http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=<your key here>

This example with your api key will be sent you in the confirm email. You can paste it in browser, to receive the response in JSON form.


Copy the returned JSON to the input box in ArduinoJson Assistant, it will return you the Memory pool size, and example to Parsing program.





ArduinoJson is a simple and efficient JSON library for embedded C++. Support various platforms of Arduino framework. Already included in Platform IO.



For connection between ESP8266 and the I2C OLED, refer last post "ESP8266 (NodeMCU/ESP8266 DevKitC) + SSD1306 I2C OLED, Platform IO".

Here is my exercise run on ESP8266 (NodeMCU), to get current weather data from OpenWeatherMap, using libraries ESP8266WiFi, ESP8266HTTPClient, ArduinoJson, and display on I2C OLED using library ESP8266_SSD1306.

#include <Arduino.h>
#include "SSD1306Wire.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

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

const String api_1 = "http://api.openweathermap.org/data/2.5/weather?q=";
const String qLocation = "London,uk";
const String api_2 = "&units=metric"; //request return temp in celsius
const String api_3 = "&APPID=";
const String api_key = "09816ba66b72ec172f3b8e1982c962ff";  //your api key
const String rqs = api_1 + qLocation + api_2 + api_3 + api_key;

SSD1306Wire  display(0x3c, D3, D5);

//Visit ArduinoJson Assistant (https://arduinojson.org/v6/assistant/)
//To get Memory pool size
StaticJsonDocument<800> doc;

void sendRqs(){

  HTTPClient http;
  http.begin(rqs);
  int httpCode = http.GET();

  if (httpCode > 0) { 

    String response = http.getString();
    //display.clear();
    //display.drawString(0, 0, "response:");
    //display.drawString(0, 10, response);
    //display.display();

    Serial.println(response);

    // Deserialize the JSON document
    DeserializationError error = deserializeJson(doc, response);

    // Test if parsing succeeds.
    if (error) {

      String errorStr = error.c_str();

      display.clear();
      display.drawString(0, 0, "deserializeJson() error:");
      display.drawString(0, 10, errorStr);
      display.display();

      Serial.println(errorStr);

      delay(10000);
    }else{
      Serial.println("deserializeJson() no error.");

      //--- Copy from ArduinoJson Assistant
      float coord_lon = doc["coord"]["lon"];
      float coord_lat = doc["coord"]["lat"];

      JsonObject weather_0 = doc["weather"][0];
      int weather_0_id = weather_0["id"];
      const char* weather_0_main = weather_0["main"];
      const char* weather_0_description = weather_0["description"];
      const char* weather_0_icon = weather_0["icon"];

      const char* base = doc["base"];

      JsonObject main = doc["main"];
      float main_temp = main["temp"];
      float main_feels_like = main["feels_like"];
      float main_temp_min = main["temp_min"];
      float main_temp_max = main["temp_max"];
      int main_pressure = main["pressure"];
      int main_humidity = main["humidity"];

      int visibility = doc["visibility"];

      float wind_speed = doc["wind"]["speed"];
      int wind_deg = doc["wind"]["deg"];

      int clouds_all = doc["clouds"]["all"];

      long dt = doc["dt"];

      JsonObject sys = doc["sys"];
      int sys_type = sys["type"];
      int sys_id = sys["id"];
      const char* sys_country = sys["country"];
      long sys_sunrise = sys["sunrise"];
      long sys_sunset = sys["sunset"];

      int timezone = doc["timezone"];
      long id = doc["id"];
      const char* name = doc["name"];
      int cod = doc["cod"];

      //--- End of ArduinoJson Assistant ---

      // Print values.
      Serial.println("temp_min: " + String(main_temp_min));
      Serial.println("temp_max: " + String(main_temp_max));

      display.clear();
      display.drawString(0, 0, name);
      display.drawString(0, 10, "temp_min: " + String(main_temp_min));
      display.drawString(0, 20, "temp_max: " + String(main_temp_max));

      display.display();
    }

  }else{
    display.clear();
    display.drawString(0, 0, "http.GET() == 0");
    display.display();
    Serial.println("http.GET() == 0");
  }
  
  http.end();   //Close connection

}

void setup() {

  Serial.begin(9600);

  display.init();
  display.setContrast(255);
  display.clear();

  display.drawString(0, 0, "ESP32/OLED");
  display.display();

  WiFi.begin(ssid, password);
  display.drawString(0, 10, "Connecting...");
  display.display();
  while (WiFi.status() != WL_CONNECTED) {
    delay(800);
  }
}

void loop() {
  display.clear();
  if (WiFi.status() == WL_CONNECTED) {
    display.drawString(0, 0, "CONNECTED");
    display.display();

    sendRqs();

  }else{
    display.drawString(0, 0, "NOT CONNECTED");
    display.display();
  }
  
  Serial.println("DONE");
  delay(10000);
}




Saturday, May 16, 2020

ESP8266 (NodeMCU/ESP8266 DevKitC) + SSD1306 I2C OLED, Platform IO.

NodeMCU + SSD1306 I2C OLED

Steps to new a PlatformIO project, for NodeMCU board of Arduino framework, using library and example of ESP8266_SSD1306, to display on I2C SSD1306 OLED display. Tested on Windows 10/VirtualBox.


- Connect NodeMCU to I2C SSD1306 OLED:


- Refer to previous post to Install VS Code/PlatformIO IDE on Ubuntu 20.04.

- Make sure the library ESP8266_SSD1306 is installed.


- Create a new project using NodeMCU board and Arduino framework.

- Open PlatformIO - Libraries page, copy example of ESP8266_SSD1306 library to our main.c.

- Edit platformio.ini to specify upload_port.

- Finally, Build and Upload. Done.


Espressif ESP8266 DevKitC + SSD1306 I2C OLED

Then I tried to test it on Espressif ESP8266 DevKitC, with module ESP-WROOM-02D.

It's found from ESP-WROOM-02D/02U Datasheet, I2C is assigned to IO14 (SCL), IO2 (SDA).


So I re-connect accordingly.

ESP8266 DevKitC <-> I2C SSD1306 OLED
3V3 - VCC
GND - GND
IO2 - SDA
IO14 - SCL

And edit main.c, change the code:

from:
SSD1306Wire  display(0x3c, D3, D5);

to:
SSD1306Wire  display(0x3c, 2, 14);


Re-build, and upload...done.


Next:
ESP8266 (NodeMCU) - Get current weather data from OpenWeatherMap, using ArduinoJson


Friday, May 8, 2020

Install VS Code/PlatformIO IDE on Ubuntu 20.04, to program Arduino/ESP8266/STM32.

This video show how to install VS Code/PlatformIO IDE on Ubuntu 20.04 (run on Windows 10/VirtualBox), to program Arduino. And also set upload port in platformio.ini and add USB permission to user.


Visit Visual Studio Code download page to download .deb version for Ubuntu.

After downloaded, open Terminal and switch to the downloaded folder and run the command to install VS Code:

$ sudo apt install ./<file>.deb

(reference: Visual Studio Code on Linux)

After installed, search and run VSCode.

In VS Code, select Extensions tab, enter platformIo in the search box, and click to install platformIO IDE.


(This screen shot after PlatformIO IDE installed. Before install, you can see a Install button.)

If you report with error of ModuleNotFoundError: No module named 'distutils.***'

Enter the command to install python3-distutils:

$ sudo apt-get install python3-distutils

(reference: https://github.com/platformio/platformio-vscode-ide/issues/907)

Close and restart VS Code to complete installation.

Now you can select PlatformIO on the left, and create a New Project. For Arduino Uno, steps refer to the above video.

May be you have to specify the upload port in platformio.ini. Open platformio.ini and add the code:
upload_port = /dev/ttyACM0

where /dev/ttyACM0 is the USB port.

Also have to add permission of the USB port to user. Enter the command in Terminal:
$ sudo usermod -a -G dialout <username>
$ sudo chmod a+rw /dev/ttyACM0

more:
2.8" 320*240 TFT Touch Screen shield (ILI9341 8 bit I/F) on Uno, using MCUFRIEND_kbv and Adafruit GFX Libraries
TFT Touch Screen shield (ILI9341 8 bit) + Uno, calibration and simple touch drawing example.


Platform IO + ESP8266/ESP32

With PlatformIO IDE installed, you can also program ESP8266/ESP32 board of Arduino framework. This video show the steps.



The following video show how to install library in PlatformIO, for I2C SSD1306 OLED driver for ESP8266/ESP32.


This example run on ESP32 (WEMOS Lolin32 with integrated SSD1306/OLED driver).

#include <Wire.h>
#include <SSD1306Wire.h>

SSD1306Wire display(0x3C, 5, 4);

void setup() {
  // put your setup code here, to run once:

  display.init();
  display.flipScreenVertically();
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_16);
}

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

  display.clear();
  display.drawString(0, 0, "ESP32/OLED");
  display.drawString(0, 20, "Hello PlatformIO");
  display.display();

  delay(1000);
  
}


Reference:
~ My old post: ESP32 + OLED Module

Next:
ESP8266 (NodeMCU/ESP8266 DevKitC) + SSD1306 I2C OLED, Platform IO.


PlatformIO + STM32

PlatformIO also can program STM32, such as NUCLEO-F401RE Board.



KEY FEATURES of NUCLEO-F401RE Board:

- Common features
  • STM32 microcontroller in LQFP64 package
  • 1 user LED shared with Arduino™
  • 1 user and 1 reset push-buttons
  • 32.768 kHz crystal oscillator
  • Board connectors:Arduino™ Uno V3 expansion connectorST morpho extension pin headers for full access to all STM32 I/Os
  • Flexible power-supply options: ST-LINK, USB VBUS or external sources
  • On-board ST-LINK debugger/programmer with USB re-enumeration capability: mass storage, Virtual COM port and debug port
  • Comprehensive free software libraries and examples available with the STM32Cube MCU Package
  • Support of a wide choice of Integrated Development Environments (IDEs) including IAR™, Keil® and GCC-based IDEs

- NUCLEO-F401RE Board-specific features
  • External SMPS to generate Vcore logic supply
  • 24 MHz HSE
  • Board connectors:External SMPS experimentation dedicated connectorMicro-AB or Mini-AB USB connector for the ST-LINKMIPI® debug connector
  • Arm® Mbed Enabled™ compliant
Reference: NUCLEO-F401RE

Steps to create  a new project for NUCLEO-F401RE Board, using Arduino and Mbed framework.


Error: libusb_open() failed with LIBUSB_ERROR_ACCESS

To make PlatformIO run on Ubuntu, in addition to add USB permission to user as describe above, you have to install udev rules (99-platformio-udev.rules) also.

Enter the commands in Terminal:

$ curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules

$ sudo service udev restart

After this file is installed, physically unplug and reconnect your board.


Update VS Code for Ubuntu

If update of VS Code is available and you are asked to Download Update.


Just close VS Code and run the commands in Terminal:

$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install code # or code-insiders

reference: https://code.visualstudio.com/docs/setup/linux

Updated 1.46.0 currently@2020-06-11



Tuesday, October 29, 2019

Monday, April 15, 2019

Install both ESP32 and ESP8266 on Arduino IDE, run on Ubuntu.

Before install ESP32/8266 on Arduino IDE running on Ubuntu, python is needed to be installed. Otherwise, you will be reported with error of:
exec: "python": executable file not found in $PATH

To install python and wget on Ubuntu, run the command in terminal:
$ sudo apt install python
$ sudo apt install python-pip
$ pip install wget

The simplest way to install ESP32/8266 on Arduino IDE is using Boards Manager,

To add boards manager, click in the menu
> File > Preferences >

Enter https://dl.espressif.com/dl/package_esp32_index.json (for ESP32) or/and http://arduino.esp8266.com/stable/package_esp8266com_index.json (for ESP8266) in Additional Boards Manager URLs. When add multiple URLs, separating them with commas.

This video show how:


If you run with error of:
ImportError: No module named serial.tools.list_ports

Most likely the pyserial is too old, re-install it with:
$ pip install pyserial


Updated@2020-06-26:
If you are looking for Ubuntu 20.04, read it Install ESP32/ESP8266 to Arduino IDE on Ubuntu 20.04, with setup Pythton & serial.

Friday, October 12, 2018

DIY Weather Station & WiFi Sensor Station || ESP8266, Nextion LCD

DIY Weather Station & WiFi Sensor Station || ESP8266, Nextion LCD

It's a great video show you how to create a weather station along with a WiFi sensor station. The sensor station measures local temperature and humidity data and sends it, through WiFi, to the weather station. The weather station then displays the data on its LCD screen. It also grabs the current temperature and humidity data in your city from the Internet and displays it as well on the LCD screen.

Friday, June 16, 2017

Python run on Raspberry Pi (and PC running Ubuntu) to plot serial data from ESP8266/NodeMCU

Last post show a simple program run on ESP8266/NodeMCU to read Analog Input and send to Serial. And display the data on Raspberry Pi 3 using Arduino IDE Serial Plotted. This post show a Python example run on Raspberry Pi 3/Raspbian Jessie with PIXEL, to plot the serial data graphically using matplotlib library.



pyserialplot.py
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import serial
import platform

print("Python version: " + platform.python_version())
print("matplotlib version: " + mpl.__version__)

fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1030)
xdata, ydata = [0]*100, [0]*100
SerialIn = serial.Serial("/dev/ttyUSB0",9600)

def update(data):
    line.set_ydata(data)
    return line,

def run(data):
    global xdata, ydata
    x,y = data
    if (x == 0):
        xdata = [0]*100
        ydata = [0]*100
    del xdata[0]
    del ydata[0]
    xdata.append(x)
    ydata.append(y)
    line.set_data(xdata, ydata)
    return line,

def data_gen():
    x = 9
    while True:
        if (x >= 9):
            x = 0
        else:
            x += 0.1
            
        try:
            inRaw = SerialIn.readline()
            inInt = int(inRaw)
        except:
            inInt = 0
            
        yield x, inInt

ani = animation.FuncAnimation(fig, run, data_gen, interval=0, blit=True)
plt.show()


This python example can be run on both Python 2 and 3. To run this on Raspberry Pi/Raspbian Jessie with PIXEL, matplotlib library is need.

For Python 2:
$ sudo apt-get install python-matplotlib

For Python 3:
$ sudo apt-get install python3-matplotlib

In the following code, we have to get the port connected.
SerialIn = serial.Serial("/dev/ttyUSB0",9600)

In Raspberry Pi/Raspbian Jessie with PIXEL, it is /dev/ttyUSB0 normal. We can check it with:
$ ls /dev/ttyUSB0*




Run on Ubuntu:

The Python script work on PC/Ubuntu also. This video show running on Ubuntu 17.04/Payton 3.6 (actually behind Windows 10/VirtualBox).


In order to run with newest Python 3.6 on Ubuntu 17.04, you have to install Python 3.6 and corresponding IDLE and pip.

And install packages using command:
$ sudo python3.6 -m pip install numpy
$ sudo python3.6 -m pip install matplotlib
$ sudo python3.6 -m pip install pyserial


About Saving graph of matplotlib:

Suppose I can click the Save button on matplotlib navigation toolbar to save the graph to file (reference: matplotlib - Interactive navigation).

But when I test it on Raspberry Pi with matplotlib 1.4.2, it is not work.

When test it on PC running Ubuntu 17.04/Python 3.6 with matplotlib 2.0.2, I can save the graph. But the navigation toolbar disappear after saved.



Thursday, June 15, 2017

ESP8266/NodeMCU read Analog Input and send to Raspberry Pi via Serial/USB

This example of ESP8266/NodeMCU read input from A0, and output to Serial. The receiving side is a Raspberry Pi 3 running Raspbian Jessie with PIXEL and Arduino IDE installed, the result is display graphically using Arduino IDE's Serial Plotter. All job done on Raspberry Pi 3. ESP8266/NodeMCU connect to the Raspberry Pi 3 with USB.


To run the whole in Raspberry Pi, you have to:
- Install Arduino IDE on Raspberry Pi/Raspbian Jessie with PIXEL
Add Arduino core for ESP8266 to Arduino IDE

The simple program run on ESP8266/NodeMCU, ESP8266_AnalogIn_SerialOut.ino. (Suppose it work on other Arduino also, such as Uno.)
const int analogIn = A0;
int analogVal = 0;
bool led = 1;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

  digitalWrite(LED_BUILTIN, led);
  led = !led; 
  analogVal = analogRead(analogIn);
  Serial.println(analogVal);
  delay(500);
}

Next:
A Python example run on Raspberry Pi 3/Raspbian Jessie with PIXEL, to plot the serial data graphically using matplotlib library.

Add Arduino core for ESP8266 to Arduino IDE (run on Raspberry Pi/Raspbian Jessie with PIXEL)

Last post show how to "Install and run Arduino IDE on Raspberry Pi/Raspbian Jessie with PIXEL". We can add Arduino core for ESP8266 to Boards Manager, such that we can develop ESP8266/NodeMCU program on Raspberry Pi directly, without using PC. For sure, the compiling time will be longer than on PC.

Add Additional Board Manager URL for ESP8266 board:
> File > Preference
> Add "http://arduino.esp8266.com/stable/package_esp8266com_index.json" in Additional Board Manager URLs.

Add ESP8266 board to Arduino IDE:
- Open Boards Manager in Arduino IDE
- Search "esp8266" or "NodeMCU", you will find "esp8266 by ESP8266 Community". Install it.

This video show how to, and run the Blink example on ESP8266/NodeMCU. The Raspberry Pi 3 connect to a 4" 800x480 HDMI IPS LCD Display, so it display in low resolution.

Next:
ESP8266/NodeMCU read Analog Input and send to Raspberry Pi via Serial/USB
A Python example run on Raspberry Pi 3/Raspbian Jessie with PIXEL, to plot the serial data graphically using matplotlib library.


Thursday, June 1, 2017

NodeMCU/ESP8266 Arduino Core example, simple http server to output PWM

Last post show a dummy example of NodeMCU/ESP8266 Arduino Core to output PWM to control color/brightness of RGB LED.

It's another example to setup a simple HTTP-like server, to receive request from client, output PWM, to control color/brightness of RGB LED.


NodeMCU_WiFiWebServer_RGB.ino
/*
 *  This sketch run on NodeMCU (ESP8266),
 *  demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pins depending on the request,
 *  to control the brightness of RGB LED connected to:
 *    D0 : BLUE
 *    D1 : GREEN
 *    D2 : RED
 *    
 *    http://server_ip/rgb/rrggbb/
 *    where rr is the value set RED
 *    where gg is the value set GREEN
 *    where bb is the value set BLUE
 *    then terminate with '/'
 *  server_ip is the IP address of the NodeMCU, will be 
 *  printed to Serial when the module is connected.
 */

#include <ESP8266WiFi.h>

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

int ledB = D0;
int ledG = D1;
int ledR = D2;

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

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

  // prepare GPIOs for RGB LED
  pinMode(D0, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  analogWriteRange(99); //PWM: 0~99
  
  // Connect to WiFi network
  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");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int valR, valG, valB;
  String subStringR, subStringG, subStringB;
  int index = req.indexOf("/rgb/");
  if(index != -1){
    if(req.charAt(index+11)=='/'){
      subStringR = req.substring(index+5, index+7);
      subStringG = req.substring(index+7, index+9);
      subStringB = req.substring(index+9, index+11);
      Serial.println("R: " + subStringR);
      Serial.println("G: " + subStringG);
      Serial.println("B: " + subStringB);

      valR = subStringR.toInt();
      valG = subStringG.toInt();
      valB = subStringB.toInt();
      Serial.println("valR: " + String(valR));
      Serial.println("valG: " + String(valG));
      Serial.println("valB: " + String(valB));
      
    }
    else{
      Serial.println("Not terminated with /");
      client.stop();
      return;
    }
  }
  else {
    Serial.println("No /rgb/ found");
    client.stop();
    return;
  }

  // Set GPIOs according to the request
  // No check valid of the requested setting
  analogWrite(ledR, valR);
  analogWrite(ledG, valG);
  analogWrite(ledB, valB);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIOs of RGB is now ";
  s += String(valR) +":" + String(valG) + ":" + String(valB);
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}


Connection same as in last post:



Compare with ESP32 version: ESP32/Arduino core for ESP32 example, simple Web control RGB LED

NodeMCU/ESP8266 Arduino Core analog output PWM

Example of NodeMCU with ESP8266 Arduino Core to output PWM by calling analogWrite(), to control brightness of RGB LED.



NodeMCU_PWM.inf
int ledB = D0;
int ledG = D1;
int ledR = D2;

void setup() {
  pinMode(D0, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  //Set PWM frequency 500, default is 1000
  //Set range 0~100, default is 0~1023
  analogWriteFreq(500);
  analogWriteRange(100);
}

// the loop function runs over and over again forever
void loop() {
  analogWrite(ledR, 0);
  analogWrite(ledG, 0);
  analogWrite(ledB, 0);
  delay(500);
  analogWrite(ledR, 100);
  analogWrite(ledG, 100);
  analogWrite(ledB, 100);
  delay(500);
  analogWrite(ledR, 0);
  analogWrite(ledG, 0);
  analogWrite(ledB, 0);
  delay(500);

  int i;
  for(i=0; i<100; i++){
    analogWrite(ledR, i);
    delay(10);
  }
  analogWrite(ledR, 0);
  
  for(i=0; i<100; i++){
    analogWrite(ledG, i);
    delay(10);
  }
  analogWrite(ledG, 0);
  
  for(i=0; i<100; i++){
    analogWrite(ledB, i);
    delay(10);
  }
  analogWrite(ledB, 0);

  for(i=0; i<100; i++8){
    analogWrite(ledR, i);
    analogWrite(ledG, i);
    analogWrite(ledB, i);
    delay(10);
  }

  for(i=100; i>0; i--){
    analogWrite(ledR, i);
    analogWrite(ledG, i);
    analogWrite(ledB, i);
    delay(10);
  }

}

Reference:
analogWrite(pin, value) enables software PWM on the given pin. PWM may be used on pins 0 to 16. Call analogWrite(pin, 0) to disable PWM on the pin. value may be in range from 0 to PWMRANGE, which is equal to 1023 by default. PWM range may be changed by calling analogWriteRange(new_range).

PWM frequency is 1kHz by default. Call analogWriteFreq(new_frequency) to change the frequency.

Next:
simple http server to output PWM, to control color/brightness of RGB LED.


Tuesday, May 23, 2017

Control GPIOs of NodeMCU (ESP8266) with ESP8266 core for Arduino, to read Buttons and write LEDs

This example show how to read input (D5, D6 & D7) from buttons, and write output (D0, D1 & D2) to LEDs accordingly. For the input, all set INPUT_PULLUP to enable internal pull-up resistor, so no external resistors needed.


Connection:

/*
NodeMCU IO index vs ESP8266 pin
IO index  ESP8266 pin
    0     [*] GPIO16
    1     GPIO5 
    2     GPIO4 
    3     GPIO0
    4     GPIO2
    5     GPIO14  
    6     GPIO12
    7     GPIO13
    8     GPIO15
    9     GPIO3
    10    GPIO1
    11    GPIO9
    12    GPIO10
[*] D0(GPIO16) can only be used as gpio read/write. 
No support for open-drain/interrupt/pwm/i2c/ow.
https://nodemcu.readthedocs.io/en/master/en/modules/gpio/
 */
 
void setup() {
  pinMode(D0, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D5, INPUT_PULLUP);
  pinMode(D6, INPUT_PULLUP);
  pinMode(D7, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {
  if(digitalRead(D5)){
    digitalWrite(D0, LOW);
  }else{
    digitalWrite(D0, HIGH);
  }

  if(digitalRead(D6)){
    digitalWrite(D1, LOW);
  }else{
    digitalWrite(D1, HIGH);
  }

  if(digitalRead(D7)){
    digitalWrite(D2, LOW);
  }else{
    digitalWrite(D2, HIGH);
  }

}


Usage of GPIO:
At the beginning, I want to use D6, D7 and D8 as input. But D8 always return LOW. After googled and found schematic diagram of NodeMCU DevKit v1.0 here: https://github.com/nodemcu/nodemcu-devkit-v1.0/blob/master/NODEMCU_DEVKIT_V1.0.PDF. It's found that GPIO15 (D8) connect to GND via a resistor, so always return LOW.

And MATTERS NEEDING ATTENTION can be found in the schematic diagram:
On every boot/reset/wakeup, GPIO15 MUST keep LOW, GPIO2 MUST keep HIGH. 
GPIO0 HIGH -> RUN MODE, LOW -> FLASH MODE.
When you need to use the sleep mode, GPIO16 and RST should be connected, and GPIO16 will output LOW to reset the system at the time of wakeup.

So, I change using D5.


Wednesday, May 10, 2017

Control GPIO (external IO pins) of NodeMCU (ESP8266) with ESP8266 core for Arduino


With ESP8266 core for Arduino, we can load and run the build-in basic example of "Blink" on NodeMCU to toggle the on board LED. We can also employ the same methods to control other io pin, or GPIO.



Connection:



There are many version of ESP8266, the printed mark on PCB may not the io number of ESP8266 MCU, you have to check it for your board. This example run on NodeMCU with marking and CPU pin assignment:
NodeMCU IO index vs ESP8266 pin
IO index  ESP8266 pin
    0     [*] GPIO16  
    1     GPIO5 
    2     GPIO4 
    3     GPIO0 
    4     GPIO2 
    5     GPIO14  
    6     GPIO12
    7     GPIO13
    8     GPIO15
    9     GPIO3
    10    GPIO1
    11    GPIO9
    12    GPIO10
[*] D0(GPIO16) can only be used as gpio read/write. 
No support for open-drain/interrupt/pwm/i2c/ow.
https://nodemcu.readthedocs.io/en/master/en/modules/gpio/

If you run on NodeMCU and select board of NodeMCU 1.0, you can use D1~D10 to access the pins. Refer to pins_arduino.h. Both D1 and 5 refer to the same pin.



If you run on other module, and select other board, may be the compile will report error of  'D1' was not declared in this scope. You have to specify the io# of the MCU, 5 in this case.


NodeMCU_Blink.ino
/*
NodeMCU IO index vs ESP8266 pin
IO index  ESP8266 pin
    0     [*] GPIO16  
    1     GPIO5 
    2     GPIO4 
    3     GPIO0 
    4     GPIO2 
    5     GPIO14  
    6     GPIO12
    7     GPIO13
    8     GPIO15
    9     GPIO3
    10    GPIO1
    11    GPIO9
    12    GPIO10
[*] D0(GPIO16) can only be used as gpio read/write. 
No support for open-drain/interrupt/pwm/i2c/ow.
https://nodemcu.readthedocs.io/en/master/en/modules/gpio/
 */

/*
 * For NodeMCU (8266) both D1 and 5 refer to the same pin
 * For others, such as "Generic ESP8266 Module", it will
 * report error of: 'D1' was not declared in this scope.
 */
const int io5 = 5;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pins as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  //pinMode(D1, OUTPUT);
  pinMode(io5, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  //digitalWrite(D1, HIGH);
  digitalWrite(io5, HIGH);
  delay(500);
  //digitalWrite(D1, LOW);
  digitalWrite(io5, LOW);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  //digitalWrite(D1, HIGH);
  digitalWrite(io5, HIGH);
  delay(500);
  //digitalWrite(D1, LOW);
  digitalWrite(io5, LOW);
  delay(500);
}

Next:
Control GPIOs of NodeMCU (ESP8266) with ESP8266 core for Arduino, to read Buttons and write LEDs

Monday, June 20, 2016

NodeMCU/ESP8266 + OLED 1.3" 128x64 SPI SH1106, using esp8266-oled-sh1106 library


It's a 1.3" 128x64 OLED of SPI interface, with SH1106 controller. The SH1106 is in general similar to the SSD1306. Main difference is a memory of 132x64 instead of 128x64.

This post show how to connect with NodeMCU and install the library of esp8266-oled-sh1106.

Connection between NodeMCU and the 1.3" 128x64 OLED SPI module with SH1106:
 D5 GPIO14   CLK         - D0 pin OLED display
 D6 GPIO12   MISO (DIN)  - not connected
 D7 GPIO13   MOSI (DOUT) - D1 pin OLED display
 D1 GPIO5    RST         - RST pin OLED display
 D2 GPIO4    DC          - DC pin OLED
 D8 GPIO15   CS / SS     - CS pin OLED display


Download and install the library as shown, and run the example:



Related:
Hello World 1.3 inch IIC/SPI 128x64 OLED x Arduino, using u8glib library
NodeMCU/ESP8266 + OLED 0.96" 128x64 I2C SSD1306 using esp8266-oled-ssd1306 library

Saturday, June 18, 2016

ESP-05(ESP8266) + Arduino Mega, act as simple web server

Last post introduced ESP-05 (a mini ESP8266 board) with simple testing. This post show a very simple web server on Arduino Mega 2560 + ESP-05.

Basically, it's same as the example of "Arduino + ESP8266 - Web Server (III) with firmware 00200.9.5(b1)", except the baud rate.


Mega_ESP05_Web.ino
/*
Arduino Mega 2560 + ESP-05(ESP8266)

ESP-05 running firmware:
AT version:0.40.0.0(Aug  8 2015 14:45:58)
SDK version:1.3.0
Ai-Thinker Technology Co.,Ltd.
Build:1.3.0.2 Sep 11 2015 11:48:04

Connection between Mega & ESP-05, 
refer "Connect with Arduino Mega 2560 via Level Converter" in:
http://goo.gl/wtG89i

Mega + ESP_05 act as station, join WiFi AP of my phone.
Once server setup, you can visit the webpage in ESP-05
by visit the IP show in Serial Monitor, under the command:
AT+CIFSR
+CIFSR:STAIP,"192.168.43.15"

If always show "Module have no response.",
check your connection, or reset ESP-05 by power OFF and ON.

 */
#define ASCII_0 48
#define ESP8266 Serial3

//WiFi hotspot setting on my phone
String SSID = "ssid";
String PASSWORD = "password";

int LED = 13;

boolean FAIL_8266 = false;

String strHTML1 = "<!doctype html>\
<html>\
<head>\
<title>arduino-er</title>\
</head>\
<body>\
<H1>arduino-er.blogspot.com</H1>";

String strHTML2 = "</body>\
</html>";

//String strHTML = "arduino-er.blogspot.com";

#define BUFFER_SIZE 128
char buffer[BUFFER_SIZE];

void setup() {
  pinMode(LED, OUTPUT);
  
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);
  delay(300);
  digitalWrite(LED, HIGH);
  delay(200);
  digitalWrite(LED, LOW);

  do{
    Serial.begin(115200);
    ESP8266.begin(115200);
  
    //Wait Serial Monitor to start
    while(!Serial);
    Serial.println("--- Start ---");

    ESP8266.println("AT+RST");
    delay(1000);
    if(ESP8266.find("ready"))
    {
      Serial.println("Module is ready");
      
      ESP8266.println("AT+GMR");
      delay(1000);
      clearESP8266SerialBuffer();
      
      ESP8266.println("AT+CWMODE=1");
      delay(2000);
      
      //Quit existing AP, for demo
      Serial.println("Quit AP");
      ESP8266.println("AT+CWQAP");
      delay(1000);
      
      clearESP8266SerialBuffer();
      if(cwJoinAP())
      {
        Serial.println("CWJAP Success");
        FAIL_8266 = false;
        
        delay(3000);
        clearESP8266SerialBuffer();
        //Get and display my IP
        sendESP8266Cmdln("AT+CIFSR", 1000);  
        //Set multi connections
        sendESP8266Cmdln("AT+CIPMUX=1", 1000);
        //Setup web server on port 80
        sendESP8266Cmdln("AT+CIPSERVER=1,80",1000);
        
        Serial.println("Server setup finish");
      }else{
        Serial.println("CWJAP Fail");
        delay(500);
        FAIL_8266 = true;
      }
    }else{
      Serial.println("Module have no response.");
      delay(500);
      FAIL_8266 = true;
    }
  }while(FAIL_8266);
  
  digitalWrite(LED, HIGH);
  
  //set timeout duration ESP8266.readBytesUntil
  ESP8266.setTimeout(1000);
}

void loop(){
  int connectionId;
  
  if(ESP8266.readBytesUntil('\n', buffer, BUFFER_SIZE)>0)
  {
    Serial.println("Something received");
    Serial.println(buffer);
    if(strncmp(buffer, "+IPD,", 5)==0){
      Serial.println("+IPD, found");
      sscanf(buffer+5, "%d", &connectionId);
      Serial.println("connectionId: " + String(connectionId));
      delay(1000);
      clearESP8266SerialBuffer();
      
      sendHTTPResponse(connectionId, strHTML1);
      sendHTTPResponse(connectionId, "<hr/>-END-<br/>");
      sendHTTPResponse(connectionId, strHTML2);

      //Close TCP/UDP
      String cmdCIPCLOSE = "AT+CIPCLOSE="; 
      cmdCIPCLOSE += connectionId;
      sendESP8266Cmdln(cmdCIPCLOSE, 1000);
    }
  }
}

void sendHTTPResponse(int id, String response)
{
  String cmd = "AT+CIPSEND=";
  cmd += id;
  cmd += ",";
  cmd += response.length();
  
  Serial.println("--- AT+CIPSEND ---");
  sendESP8266Cmdln(cmd, 1000);
  
  Serial.println("--- data ---");
  sendESP8266Data(response, 1000);
}

boolean waitOKfromESP8266(int timeout)
{
  do{
    Serial.println("wait OK...");
    delay(1000);
    if(ESP8266.find("OK"))
    {
      return true;
    }

  }while((timeout--)>0);
  return false;
}

boolean cwJoinAP()
{
  String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"";
  ESP8266.println(cmd);
  return waitOKfromESP8266(10);
}

//Send command to ESP8266, assume OK, no error check
//wait some time and display respond
void sendESP8266Cmdln(String cmd, int waitTime)
{
  ESP8266.println(cmd);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

//Basically same as sendESP8266Cmdln()
//But call ESP8266.print() instead of call ESP8266.println()
void sendESP8266Data(String data, int waitTime)
{
  //ESP8266.print(data);
  ESP8266.print(data);
  delay(waitTime);
  clearESP8266SerialBuffer();
}

//Clear and display Serial Buffer for ESP8266
void clearESP8266SerialBuffer()
{
  Serial.println("= clearESP8266SerialBuffer() =");
  while (ESP8266.available() > 0) {
    char a = ESP8266.read();
    Serial.write(a);
  }
  Serial.println("==============================");
}