Beruflich Dokumente
Kultur Dokumente
• Bluetooth
• Wireless internet
• Local network
• Servers
Internet of Things
void setup(){
Serial.begin(9600);
BTSerial.begin(9600); //9600 is the default value (BAUD4)
}
void loop() {
if (BTSerial.available()) { //If the Bluetooth module has data...
while(BTSerial.available()) { //keep reading while there is data.
delayMicroseconds(10); //small delay to prevent cutting the message short.
Serial.print((char) BTSerial.read()); //Write each byte to Serial monitor.
}
Serial.println(""); //Print the line.
}
if (Serial.available()){ //Check if anything has been entered to the serial monitor
while(Serial.available()){ //Keep reading until there is no data left.
delayMicroseconds(10);
BTSerial.write(Serial.read()); //Write data from serial to the bluetooth module for sending.
}
}
delay(50);
}
NodeMCU 1.0
• Built-in ESP8266, ”ESPduino”
• Works with Arduino IDE, not
officially though
• 12 V regulator for input voltage
• Price: 5 – 10 €
• 13 GPIO, only one PWM and ADC
(0-1 V)
• Pins work with 0 - 3,3 V!
• Good tutorial:
http://www.makeuseof.com/tag/
meet-arduino-killer-esp8266/
NodeMCU 1.0 Pinout
NodeMCU setup
• Additional Board Manager URL:
– http://arduino.esp8266.com/stable/package_esp8266com_index.json
NodeMCU setup
• Add the link to the Additional Board Manager URLs
NodeMCU setup
• Search the board manager for esp8266
• Install the library
NodeMCU setup
• Select the
NodeMCU 1.0 as
your board
• Additional settings
appear under the
board menu
• They can be left as
they are
• Higher Upload
Speed reduces your
upload times
NodeMCU setup
• If the device doesn’t appear in the port menu
after installing the library and connecting the
board you can try installing the USB to Serial
chip drivers.
• The chip is the ch340g
• Drivers can be found on the NodeMCU git:
https://github.com/nodemcu/nodemcu-
devkit/tree/master/Drivers
Logic level converter
• Logic shifter 3v3 <-> 5 V
• Price: Sparkfun 2,5 €
• Bi-directional, works both ways
• https://learn.sparkfun.com/tutorial
s/logic-levels
Example 2 – IP2IP UDP
1/2 2/2
#include <ESP8266WiFi.h> void sendPackets(){
#include <WiFiUdp.h> char message[128];
if (Serial.available()) {
char networkName[32] = "name"; int i = 0;
char networkPassword[32] = "password"; while(Serial.available()){
delayMicroseconds(10);
uint16_t udpPort = 2020; //UDP port number. Has to be the same in the two devices. message[i] = (char)(Serial.read()); //Save data from serial to a message buffer.
i++;
IPAddress targetIP(10, 100, 53, 46); //IP of the device you are sending messages to. }
message[i] = NULL;
WiFiUDP udp; Serial.print("Sent: ");
Serial.println(message);
void connectToWifi(){ udp.beginPacket(targetIP, udpPort); //Start communication
Serial.print("\nConnecting to "); udp.write(message, i); //Send message. i is the length of the buffer.
Serial.print(networkName); udp.endPacket();
WiFi.begin(networkName, networkPassword); }
while (WiFi.status() != WL_CONNECTED){ }
Serial.print(".");
delay(500); void setup() {
} Serial.begin(57600);
Serial.print("\nConnected with IP Address: "); WiFi.mode(WIFI_STA);
Serial.println(WiFi.localIP()); connectToWifi();
} udp.begin(udpPort);
}
• Adds to setup:
• broadcastIP = changeToBroadcast();
• Adds a function:
IPAddress changeToBroadcast(){
IPAddress broadcastIP(WiFi.localIP().operator[](0) | ~WiFi.subnetMask().operator[](0),
WiFi.localIP().operator[](1) | ~WiFi.subnetMask().operator[](1),
WiFi.localIP().operator[](2) | ~WiFi.subnetMask().operator[](2),
WiFi.localIP().operator[](3) | ~WiFi.subnetMask().operator[](3));
return broadcastIP;
}
• Changes:
• udp.beginPacket(targetIP, udpPort); udp.beginPacket(broadcastIP, udpPort);
Blynk
• Blynk is a free IoT server
• Great GUI builder on a phone application
• Free version has some limitations to the complexity
of the GUI but not much
• Very easy to use
• Instructables has full instructions for setting up Blynk
using a NodeMCU
• http://www.instructables.com/id/Simple-Led-
Control-With-Blynk-and-NodeMCU-Esp8266-/
ESP8266 after Blynk
• Using the ESP8266 without an app like Blynk
requires some coding skills, although lots of
ready-made code can be found on the internet.
• There will also be some extra examples in the
end of the slides
• You can connect with the module directly
within its range or anywhere through internet.
• For internet connection you’ll need a website to
which the module sends data or where you can
send data to the module.
More reading and links
• Google & Youtube - search for projects, solutions to occurring problems
and data sheets for components
• http://www.blynk.cc/ - Homepage of the Blynk software, getting
started,
community forums
• http://www.esp8266.com/ - Everything on ESP8266, wiki
• https://www.adafruit.com/ - Learning materials, guides, example
projects,
forums, store
• https://github.com/ - Largest code host, lots of projects and sample
code
• http://allaboutee.com/ - good ESP8266 tutorials
• https://nurdspace.nl/ESP8266/ - ESP8266 info and basic list of AT
commands
Non proven older examples
• Will be found in a separate zip in the exercise
materials
ESP8266 ADC server (just
HTML)
• Download NodeMCU.zip (place it in your sketchbook folder)
• Attach potentiometer to ADC pin (and 3.3V & GND)
• Upload ESP8266_Pot example
• Access the webserver by typing in the ip of the ESP8266 in a
browser
• (the sketch prints it in the serial monitor, if you missed it push
reset)
• Upload Inspirator
• Become inspired
ESP8266 ADC (socket)
• There is an even faster way to transfer data to / from a webserver
WEBSOCKETS
• https://en.wikipedia.org/wiki/WebSocket
ESP8266 ADC (socket)
• We can make this even more complicated!
• Let’s put the server files (html, js, etc.) in
• SPIFFS (SPI flash file system),
• (there is external SPI flash on the ESP8266)
• Download .zip from
• https://github.com/esp8266/arduino-esp8266fs-
plugin/releases/tag/0.3.0
• Create ‘tools’ directory in the sketchbook directory
• Copy the ESP8266FS folder there
• (the path will look like
<home_dir>/Arduino/tools/ESP8266FS/tool/esp8266fs.jar).
• Restart Arduino IDE
ESP8266 ADC (socket)
• Download the ESP8266 Websockets library:
• https://github.com/Links2004/arduinoWebSockets
• Upload the NodeMCUSocket sketch
• Write the IP down that the ESP gets (it prints it in the
serial monitor)
• Modify the menuinterface.js file to have the correct ip
for the socket server
• Upload the sketch data to SPIFFS (close serial monitors
before)
• (ToolsESP8266 sketch data upload)
This is the final
exercise
• Course assistant will now be on-
call at the Lab at times announced
later
• Most likely at the same times as
the exercises were
• You can also contact the assistant
for meetings or help at:
eemeli.mykra@aalto.fi