2018年1月31日 星期三

NodeMCU

NodeMCU

nodemcu

 What is NodeMCU?

The NodeMCU development board is a powerful solution to program microcontrollers and be part of the
 Internet of Things (IoT). The NodeMCU development board, based on ESP8266EX, is a cute module with 
a microcontroller, integrated Wi-Fi receiver, and transmitter. NodeMCU supports several programing languages; 
hence, it is very easy to upload programs from any computer over a micro-USB port. I have been playing with the 
NodeMCU for quite a while now and, I have to say, it is a lot more fun than the other available IoT modules. 
When it comes to prototyping — just another perfect, relatively cheap, easy-to-learn, and user-friendly 
minuscule magic module!

Features

  • Open-source,Interactive,Programmable,Low cost,Simple,Smart,WI-FI enabled
  • Deep sleep power <10uA, Power down leakage current < 5uA
  • Wake up and transmit packets in < 2ms
  • Standby power consumption of < 1.0mW
  • Compatible with Arduino IDE
  • Easy to access wireless router
  • Based on Lua 5.1.4 (without debug, os module.)
  • Event-Drive programming preferred.
  • Build-in json, file, timer, pwm, i2c, spi, 1-wire, net, mqtt, coap, gpio, wifi, adc, uart and system api.
  • GPIO pin re-mapped, use the index to access gpio, i2c, pwm.
  • Both Floating Point and Integer versions of the firmware can be built.

Specifications

CategoriesItemsParameters
WiFiStandardCCC/FCC/CE/TELEC/SRRC
Protocols802.11 b/g/n
Frequency Range2.4G~2.5G (2400M~2483.5M)
HardwareMCUXtensa L106
RAM50K
Flash4MB
Peripheral InterfaceUART/SDIO/SPI/I2C/I2S/IR Remote Control
GPIO/PWM
Power Supply4.5V~9V(10VMAX),Support USB power supply
Operating Voltage3.0~3.6V
Operating CurrentAverage: 80mA(200mA MAX)
Standby Current<200uA
Transmission Rate110-460800bps
Port Driving Capability15mA
Operating Temperature Range-40℃~+125℃
Storage Temperature Range-40℃~+125℃
Board Size48mm x 26mm
Weight~7g
SoftwareWi-Fi ModeStation/SoftAP/SoftAP+Station
SecurityWPA/WPA2
EncryptionWEP/TKIP/AES
Firmware Upgrade
UART Download/OTA (via network)
Download and Write Firmware via Host
SoftwareDevelopment
Supports Cloud Server Development/
SDK for Custom Firmware Development
Network ProtocolsIPv4,TCP/UDP/HTTP/FTP
User ConfigurationAT Instruction Set,Cloud Server,Android/iOS App

Pin Definition

NODEMCU-DEVKIT-INSTRUCTION-EN
Note:D0(GPIO16) can only be used as GPIO read/write,no interrupt supported no pwm/i2c/ow 
supported.
You can see the new GPIO map as below:
IO indexESP8266 pinIO indexESP8266 pin
0 [*]GPIO167GPIO13
1GPIO48GPIO15
2GPIO59GPIO3
3GPIO010GPIO1
4GPIO211GPIO9
5GPIO1412GPIO10
6GPIO12
Micro USB port - it use for upload the program and update firmware,you can also charge the battery
Reset button - to have a reset
Flash button - it use for update the firmware

Hardware Resource

The NodeMCU is a  fully open–source hardware,you can get more informations at below links:

Getting  Started with the Osoyoo NodeMCU Kit

Tutorial and sample projects(Arduino IDE)

 FAQ

You can list you question here or contact with support@vership.com for technology support.
When I connect stuff to some of the pins, the NodeMCU stops working. Whats up with that?
The ESP8266 uses some of the pins as 'boot mode' pins so on boot they must be set to certain values:
  • CH_PD (EN) should be always pulled high (it will disable the entire module if low)
  • RST should be always pulled high (it will disable the entire module if low)
  • GPIO 0 sets whether the bootloader is active, it must be pulled HIGH during power up/reset for 
  • the user program to run. If its pulled LOW, it will activate the bootloader. The built-in red LED on
  •  #0 pulls it up
  • GPIO 2 must be pulled high on power up/reset.
  • GPIO 15 must be pulled low on power up/reset.
My NodeMCU board keeps crashing and resetting, whats up with that?
The most common reason for crashes is power failure. Make sure you're powering the NodeMCU
 with a good ~5V power supply, and if you're using a USB-Serial cable, that its plugged into the 
mainboard of your computer or through a powered hub!
I can't seem to find the Serial port on my computer for the NodeMCU?
Don't forget to install the Serial port driver(CP2104 driver / CH340 driver) or your computer, they are required!
 I can't get Lua to respond to my commands
Make sure your terminal software is sending correct line endings! The default PuTTY settings may be 
wrong when trying to talk to Lua on an ESP8266. Lua expects CRLF "\r\n" line endings, and apparently 
PuTTY defaults to just LF "\n"!

Reference

ESP8266 Datasheet & Other Documents: http://www.esp8266.com/wiki/doku.php
NodeMCU office website: http://www.nodemcu.com/

Getting started with Osoyoo NodedMCU by using Arduino IDE

Getting started with Osoyoo NodedMCU 

by using Arduino IDE


The Osoyoo NodeMCU comes pre-programmed with Lua interpretter, but you don't have to use it! Instead, 
you can use the Arduino IDE which may be a great starting point for Arduino lovers to familiarize themselves
 with the technologies surrounding the IoT.Note that when you use the NodeMCU board with the Arduino IDE ,
it will write directly to the firmware, erasing the NodeMCU firmware.So if you want to back to Lua SDK,use the 
"flasher" to re-install the firmware.
The NodeMCU programming can be as easy as in Arduino,the main difference is the distribution of pins in the 
nodemcu board.Following below operations and enjoy your first NodeMCU & Arduino IDE travel!

1.CONNECT YOUR NODEMCU TO THE COMPUTER

Use the USB cable to connect your NodeMCU to the computer,you will see the  blue onboard LED flicker when 
powered up, but they will not stay lit.

2.INSTALL THE COM/SERIAL PORT DRIVER

In order to upload code to the ESP8266 and use the serial console, connect any data-capable micro USB cable to 
ESP8266 IOT Board and the other side to your computer's USB port.
The new version NodeMCUv1.0 comes with the CP2102 serial chip,you can download and install the driver here.
The NodeMCUv0.9 comes with the CH340 serial chip,you can download and install the driver here.

3.INSTALL THE ARDUINO IDE 1.6.4 OR GREATER

Download Arduino IDE from Arduino.cc (1.6.4 or greater) - don't use 1.6.2! You can use your existing IDE if you 
have already installed it.

4.INSTALL THE ESP8266 BOARD PACKAGE

nto Additional Board Manager URLs field in the Arduino v1.6.4+ preferences 
(Open Arduino IDE–>File–>Perferences–>Settings).
reference
Enter the link and click "OK" to save your changes.
save link
Next, use the Board Manager to install the ESP8266 package
board manager
Enter the Boards Manager and find the board type as below:
install in the board manager
Scroll the Broads Manager screen down to the bottom, you will see  A module called 
“esp8266 by esp8266 Community” (see following picture), select the latest version and 
click "Install".
installed
The ESP8266 package has benn installed successfully.
Note: You'd better close the Arduino IDE and restart it again.

5.SETUP ESP8266 SUPPORT

When you've restarted, select NodeMCU 0.9 (or NodeMCU 1.0) from the Tools->Board dropdown
board types
Config the Board menu and choose the right Port for your device.
  • CPU Frequency:80MHz,
  • Flash Size:4M(3M SPIFFS),
  • Upload Speed:112500
tools
Now just proceed as the Arduino: Start your sketching!
Note: 115200 baud upload speed is a good place to start - later on you can try higher 
speeds but 115200 is a good safe place to start.

6.BLINK TEST

Now,we will try to light the onboard LED as a simple test.
Enter this into the sketch window (and save since you'll have to)
void setup() {
pinMode(D0, OUTPUT)}
void loop() {
digitalWrite(D0, HIGH);
delay(1000);
digitalWrite(D0, LOW);
delay(1000);
}
Upload the sketch via the IDE
upload
The sketch will start immediately - you'll see the LED blinking every sencond.
 Now just proceed as the Arduino: Start your sketching!

Use NodeMCU to display MQTT IOT message on I2C1602 LCD

NodeMCU Lesson 18—Display MQTT IOT message on I2C1602 LCD

Use NodeMCU to display MQTT IOT message on I2C1602 LCD

About NodeMCU
NodeMCU is a very popular Micro controller with wifi access. It is based on ESP8266 - a cheap but powerful chip  and fully
support Arduino IDE. If you familiar with Arduino IDE, you can program NodeMCU in no time.
Objective
In this project, we will use NodeMCU to subscribe IOT message from MQTT broker and display the message on 1602 I2C LCD display.
MQTT is a Machine to Machine IOT  protocol.  This protocol allows one device(sender) to publish data to a MQTT  broker with
specified Topic, another device(receiver) which subscribes the same topic can easily get message from sending device.
Parts and Devices
Devices
qty
NodeMCU board
1
I2C LiquidCrystal Display(16x2 LCD )
1
jumper wires
some
Circuit Connection Graph
Programming Prerequisite
Before running the project, we need set up the  Arduino IDE as per following procedures:
Library Installation:
In Arduino IDE, we need install MQTT client library to communicate with MQTT broker,  please download
the library from following link:
You also need to install LiquidCrystal_I2C library ,download link:
http://osoyoo.com/wp-content/uploads/2016/12/LiquidCrystal_I2C.rar
Unzip above file, move the unzipped folder to Arduino IDE library folder
(you can also import these two zip files to Arduino library from IDE).
Download sample code from following link
esp8266_i2c_1602.txt , copy the code into Arduino IDE. Before running the code,
please do following changes to fit your wifi and MQTT setting:
1)Line 19 and 20:
const char* ssid = "your_hotspot_ssid";
const char* password = "your_hotspot_password";
You need change these 2 lines to match your wifi SSID and password
2)Line 21
const char* mqtt_server = "broker.mqtt-dashboard.com";
You can use your own MQTT broker URL or IP address to set above mqtt_server value.
You can also use some famous free MQTT server to test the project such as
"broker.mqtt-dashboard.com",  "iot.eclipse.org" etc
If you want to install your own MQTT broker in Ubuntu Linux, please read this article
http://osoyoo.com/2016/09/07/how-to-install-mosquitto-mqtt-server-on-linux
3)if your MQTT server require username and password authentication,  you need change line 86
Running the code
Before running the code, you need know the I2C address of your LCD display. normally
 it should be 0x27 or 0x3f. You can use test the address by running following program in
http://osoyoo.com/2014/12/07/16x2-i2c-liquidcrystal-displaylcd/,After you get correct I2C address,
update the I2C value  in the Arduino code(download link: esp8266_i2c_1602.txt )
After you running the code, please open the serial terminal window in upright corner of Arduino IDE.
If wifi hotspot name and password setting is ok and MQTT broker is connected, you will see following result:
led-nodemcu
After the NodeMCU connected to wifi and MQTT broker,  it will subscribe to a MQTT broker and waiting
for message. Let us publish a message from a MQTT client and test if NodeMCU can get this message.
You can select any MQTT client tool to send message to MQTT broker. simply use google to search
 MQTT client in internet, or search MQTT in Apple store or Google Play, you can find many free MQTT
client software. In  my case, I uses MQTTBox for Windows and did following configuration:
After click Save button, enter into Topic to publish screen,do configuration as following picture:
Make sure the topic your NodeMCU code client.subscribe("OsoyooCommand") 
fuction mush match MQTTBOX Topic to publish value
After you publish a "Hello World" message from payload text field, you i2c LCD
will display the "Hello World".


NodeMCU Lesson 17— Servo & MQTT

NodeMCU Lesson 17— Servo & MQTT


Introduction

In this lesson, we will show how to remote control a servo motor via MQTT protocol.

Preparation

Hardware:
Software:

Connection

Overhere,we use A0 as the analog input pin.

Upload Sketch

Connect the NodeMCU to computer via USB cable,open this sketch by using Arduino IDE(Version1.6.4+):
Edit the code to fit your own WiFi and MQTT settings as following operations:
1)Hotspot Configration:
const char* ssid = "your_hotspot_ssid";
const char* password = "your_hotspot_password";
Find above code line,put your own ssid and password on there.
2)MQTT  Server  Address Setting
const char* mqtt_server = "broker.mqtt-dashboard.com";
You can use your own MQTT broker URL or IP address to set above mqtt_server value. You can also use some famous free MQTT server to test the project such as  "broker.mqtt-dashboard.com",  "iot.eclipse.org" etc.
3)MQTT Client Settings
If your MQTT broker require clientID,username and password authentication,you need to change
if (client.connect(clientId.c_str()))
to
if (client.connect(clientId,userName,passWord)) //put your clientId/userName/passWord here
If not,just keep them as default.
After do that,choose the coresponding board type and port type as below,then upload the sketch to the NodeMCU.
  • Board:"NodeMCU 0.9(ESP-12 Module)"
  • CPU Frequency:"80MHz"
  • Flash Size:"4M (3M SPIFFS)"
  • Upload Speed:"115200"
  • Port: Choose your own Serial Port for your NodeMCU

MQTT Client Settings

About how to config the MQTT client,check this link.
Topics Settings:
  • Topic to publish: OsoyooCommand
  • Payload Type: Decimal Array

Running Result

Onece the upload done,if wifi hotspot name and password setting is ok and MQTT broker is connected, open the Serial Monitor,you will see following result:
The NodeMCU serial port will show the IP address and the connection satatus, then print the message for the nodemcu subscription:The rotation angle of this servo.
Overhere,we should note:After upload down,the servo motor will rotate to a position of 90 degrees,we'd better to set it to 0 degree.Do as following operations:
1.   Open your MQTT client,make sure they are "Conneted" to the MQTT broker.
2.   Type "0" to the “Payload” frame,choose "Decimal Array" for your payload type,then publish this data.
After completed above operations, you can see the servo motor turns to 0 degree and the Serial Monitor will output:"[OsoyooCommand: Rotation angle is:   0 °]"
Then,you can control the servo by publishing any integer between 0 and 180 via MQTT protocol.
The SG90 micro servo is a  180 degree servo motor,so its rotation angle can not be greater than 180 degrees.

NodeMCU Lesson 16 — Stepper Motor & MQTT

NodeMCU Lesson 16 — Stepper Motor & MQTT


Introduction

In this lesson, we will show how to remote control a stepper motor via MQTT protocol.

Preparation

Hardware:
Software:

Connection

First,connect the step motor with the bridge,next connect the bridge board to the NodeMCU:
NodeMCUStepper Bridge Board
VinPower+
GNDPower-
D1IN1
D2IN3
D5IN2
D6IN4

Upload Sketch

Connect the NodeMCU to computer via USB cable,open this sketch by using Arduino IDE(Version1.6.4+):
You can change is line to set the speed of stepper motor:
  myStepper.setSpeed(80);
Edit the code to fit your own WiFi and MQTT settings as following operations:
1)Hotspot Configration:
const char* ssid = "your_hotspot_ssid";
const char* password = "your_hotspot_password";
Find above code line,put your own ssid and password on there.
2)MQTT  Server  Address Setting
const char* mqtt_server = "broker.mqtt-dashboard.com";
You can use your own MQTT broker URL or IP address to set above mqtt_server value. You can also use some famous free MQTT server to test the project such as  "broker.mqtt-dashboard.com",  "iot.eclipse.org" etc.
3)MQTT Client Settings
If your MQTT broker require clientID,username and password authentication,you need to change
if (client.connect(clientId.c_str()))
to
if (client.connect(clientId,userName,passWord)) //put your clientId/userName/passWord here
If not,just keep them as default.
After do that,choose the coresponding board type and port type as below,then upload the sketch to the NodeMCU.
  • Board:"NodeMCU 0.9(ESP-12 Module)"
  • CPU Frequency:"80MHz"
  • Flash Size:"4M (3M SPIFFS)"
  • Upload Speed:"115200"
  • Port: Choose your own Serial Port for your NodeMCU

MQTT Client Settings

About how to config the MQTT client,check this link.
Topics Settings:
  • Topic to Publish: OsoyooCommand

Running Result

Onece the upload done,if wifi hotspot name and password setting is ok and MQTT broker is connected, open the Serial Monitor,you will see following result:
The NodeMCU serial port will show the IP address and the connection satatus,then print the command published by the MQTT client.
As you can see from the sketch,When the payload on the publish topic is "1",the stepper motor rotates clockwise,and the Serial Monitor output is as follows:
"Command from MQTT broker is : [OsoyooCommand clockwise "
When the payload on the publish topic is "2",the stepper motor rotates counter clockwise ,and the Serial Monitor output is as follows:
"Command from MQTT broker is : [OsoyooCommand counterclockwise "

NodeMCU Lesson 15 — I2C 1602 LCD & MQTT

NodeMCU Lesson 15 — I2C 1602 LCD & MQTT

Introduction

In this lesson, we will show how to use the NodeMCU to subscribe to messages that published by MQTT client,then display these messages on a 1602 I2C LCD display.

Preparation

Hardware:
Software:

Connection

NodeMCUI2C 1602 LCD
VinVCC
GNDGND
D1SCL
D2SDA

Upload Sketch

Connect the NodeMCU to computer via USB cable,open this sketch by using Arduino IDE(Version1.6.4+):
About this line:
LiquidCrystal_I2C lcd(0x27, 20, 4);
*0x27 means the address of this 1602 I2C LCD display,different LCD may have different address,if the LCD do not work,please connect your 1602 I2C LCD dispaly to your NodeMCU,then upload below code to your NodeMCU, you will get the I2C address from the Serial Monitor.You can check following link to get more usages about 1602 I2C LCD dispaly  : http://osoyoo.com/2014/12/07/16x2-i2c-liquidcrystal-displaylcd/.
Edit the code to fit your own WiFi and MQTT settings as following operations:
1)Hotspot Configration:
const char* ssid = "your_hotspot_ssid";
const char* password = "your_hotspot_password";
Find above code line,put your own ssid and password on there.
2)MQTT  Server  Address Setting
const char* mqtt_server = "broker.mqtt-dashboard.com";
You can use your own MQTT broker URL or IP address to set above mqtt_server value. You can also use some famous free MQTT server to test the project such as "broker.mqtt-dashboard.com",  "iot.eclipse.org" etc.
3)MQTT Client Settings
If your MQTT broker require clientID,username and password authentication,you need to change
if (client.connect(clientId.c_str()))
to
if (client.connect(clientId,userName,passWord)) //put your clientId/userName/passWord here
If not,just keep them as default.
After do that,choose the coresponding board type and port type as below,then upload the sketch to the NodeMCU.
  • Board:"NodeMCU 0.9(ESP-12 Module)"
  • CPU Frequency:"80MHz"
  • Flash Size:"4M (3M SPIFFS)"
  • Upload Speed:"115200"
  • Port: Choose your own Serial Port for your NodeMCU

MQTT Client Settings

About how to config the MQTT client,check this link.
Topics Settings:
  • Topic to publish: OsoyooCommand
  • Payload Type: Strings/JSON/XML/Characters

Running Result

Onece the upload done,if wifi hotspot name and password setting is ok and MQTT broker is connected, open the Serial Monitor,you will see following result:
The NodeMCU serial monitor will show the IP address and the connection satatus,then print the "publish data".As you can see from the sketch,the initial data is 1.
Then,you can type other message into the "Payload" and click the "Publish" button,the NodeMCU will subscribe the data and dispaly it on the LCD:

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...