Realtek Ameba RTL8195 Technical Workshop
Realtek Ameba RTL8195 Technical Workshop
Agus Kurniawan
1st Edition, 2018
Table of Contents
Realtek Ameba RTL8195 Technical Workshop
Preface
1. Preparing Development Environment
1.1 Realtek Ameba RTL8195
1.2 Electronics Components
1.2.1 Arduino Starter Kit
1.2.2 Fritzing
1.2.3 Cooking-Hacks: Arduino Starter Kit
1.2.4 Arduino Sidekick Basic kit v2
1.2.5 Grove - Starter Kit for Arduino
1.2.6 DFRobot - Arduino Kit for Beginner v3
1.3 Development Tools
1.4 Unboxing
1.5 Testing
2. Setting Up Realtek Ameba RTL8195
2.1 Getting Started
2.2 Installing Arduino Software
2.3 Configuring Arduino for Realtek Ameba RTL8195
2.4 Connecting Realtek Ameba RTL8195 board to Computer
2.5 Hello Realtek Ameba RTL8195: Blinking LED
2.6 Updating Program
3. Writing and Reading Digital Data
3.1 Getting Started
3.2 Wiring
3.3 Writing a Program
3.4 Testing
4.2 Hello UART
4.2.1 Preparation
4.2.2 Connecting to Realtek Ameba RTL8195 LOG UART
4.2.3 Wiring
4.2.4 Writing a Program
4.2.5 Testing
5. Working with Analog I/O
5.1 Getting Started
5.2 Demo Analog Output (PWM) : RGB LED
5.2.1 Wiring
5.2.2 Writing Program
5.2.3 Testing
5.3 Demo Analog Input: Working with Potentiometer
5.3.1 Wiring
5.3.2 Writing Program
5.3.3 Testing
5.4 Writing Analog Output.
5.4.1 Wiring
5.4.2 Writing Program
5.4.3 Testing
6. Working with I2C
6.1 Getting Started
6.2 Writing Program
6.3 Writing Program
6.4 Testing
7. Working with SPI
7.1 Getting Started
7.2 Wiring
7.3 Writing a Program
7.4 Testing
8.1 Getting Started
8.2 EEPROM Demo
9. Building Internet of Things Application
91 Getting Started
9.2 Scanning Existing WiFi Hotspot
9.3 Building IoT Application
9.3.1 Wiring
9.3.2 Writing Application
9.3.3 Testing
Preface
This book was written to help anyone want to get started with Realtek Ameba RTL8195 board
development. It describes the basic elements of Realtek Ameba RTL8195 development using Arduino and Mbed software.
Agus Kurniawan
1.1 Realtek Ameba RTL8195
Realtek Ameba RTL8195 is development board from Realtek. This board provides an IoT solution inside the familiar Arduino form factor. The Ameba Board supports WiFi, GPIO, NFC, I2C, UART, SPI, PWM and ADC operations, and can support Ethernet via Arduino-compatible headers, providing you a great IoT platform for your next project.
The following is a list of forms of Realtek Ameba RTL8195 board.
The following is the board specification:
32-bit ARM Cortex M3 (166MHz)
1MB ROM, 2MB SDRAM and 512KB SRAM Integrated with 802.11 b/g/n 1x1 WiFi
NFC Tag with Read/Write Function
10/100 Ethernet MII/ RMII/RGMII Interface USB OTG
SDIO Device/SD Card Controller Hardware SSL Engine
Maximum 30 GPIOs
2 SPI Interfaces support both master and slave mode
3 UART Interfaces, including 2 HS-UART and one log UART 4 I2C Interfaces support both master and slave mode
2 I2S/PCM Interfaces support both master and slave mode 4 PWM Interfaces
Officially you can buy this board on https://www.amebaiot.com/en/ameba-arduino-faq/.
1.2 Electronics Components
We need electronic components to build our testing, for instance, Resistor, LED, sensor devices and etc. I recommend you can buy electronic component kit. We can use electronics kit from Arduino to be developed on Realtek Ameba RTL8195. The following is a list of electronics kit which can be used in our case.
1.2.1 Arduino Starter Kit
Store website: http://arduino.cc/en/Main/ArduinoStarterKit
1.2.2 Fritzing
Store website: http://shop.fritzing.org/ .
1.2.3 Cooking-Hacks: Arduino Starter Kit
1.2.4 Arduino Sidekick Basic kit v2
1.2.5 Grove - Starter Kit for Arduino
Another option, you can buy this kit on Seeedstudio, http://www.seeedstudio.com/depot/Grove-Starter-Kit-for-Arduino-p-1855.html .
1.2.6 DFRobot - Arduino Kit for Beginner v3
DFRobot provides Arduino kit too. You can buy it on the following website.
1.3 Development Tools
To develop app with Realtek Ameba RTL8195 board, I use Arduino IDE for text editor. You can learn how to install it on chapter 2.
1.4 Unboxing
1.5 Testing
For testing, I used Realtek Ameba RTL8195 on Windows 10, OS X and Ubuntu.
2. Setting Up Realtek Ameba RTL8195
2.1 Getting Started
In this chapter, we set up Realtek Ameba RTL8195 board development using Arduino software. Then, try to build a simple Realtek Ameba RTL8195 app, Blinking.
2.2 Installing Arduino Software
If you have experience in Arduino development, you can use Arduino IDE to develop Realtek Ameba RTL8195 board too. You can download Arduino software
from https://www.arduino.cc/en/Main/Software. I recommend to use Arduino 1.8.5 or later.
2.3 Configuring Arduino for Realtek Ameba RTL8195
This section is to configure Realtek Ameba RTL8195 on Arduino IDE. You computer should be connected to Internet network. Now open your Arduino and open Preferences so you get a dialog that is shown in Figure below.
Put this link to Additional Boards Manager URLs as shown Figure above.
https://github.com/Ameba8195/Arduino/raw/master/release/package_realtek.com_ameba_index.json
Click Install button to install the board.
If completed, you can verify that Realtek Ameba RTL8195 board is shown in target list.
2.4 Connecting Realtek Ameba RTL8195 board to Computer
Based on the datasheet, Realtek Ameba RTL8195 board pinout is shown in Figure below.
If you're working on Windows platform, you can see Realtek Ameba RTL8195 detect on Device Manager.
2.5 Hello Realtek Ameba RTL8195: Blinking LED
In this section, we build a blinking LED program using Blink program from Arduino software. We need a LED and jumper cables to implement this demo.
The following is pinout of Realtek Ameba RTL8195.
Let's start to write our Blink program. In this demo, I use Arduino 1.8.5.
You can perform the following wiring:
LED is connected to Realtek Ameba RTL8195 pin 7
LED GND is connected to Realtek Ameba RTL8195 pin GND.
Then, you can see Blink source code as follows.
#define MYLED 7
void setup() {
Select serial port for your Realtek Ameba RTL8195 board, for instance COM6 in Windows platform. On my MacOS, it's selected on /dev/cu.usbmodem1422.
After pressed, you can see LED is blinking.
2.6 Updating Program
3. Writing and Reading Digital Data
3.1 Getting Started
To write a digital data on Realtek Ameba RTL8195 board, we can use digitalWrite() and use
digitalRead() to read data from a digital output. To use Realtek Ameba RTL8195 digital I/O pins, we must define them using pinMode() with passing OUTPUT or INPUT parameter.
The following is a scheme of Realtek Ameba RTL8195 board pinouts for digital Input/Output.
In this chapter, we build a program to illustrate how Realtek Ameba RTL8195 digital input/output work. We need a LED and a pushbutton.
3.2 Wiring
We build the following wiring for the demo.
LED is connected to Realtek Ameba RTL8195 pin 7
Push button is connected to Realtek Ameba RTL8195 pin 8
3.3 Writing a Program
To create a program, we just open Arduino IDE and write this code.
int led = 7;
int pushButton = 8; int state = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(pushButton, INPUT); }
void loop() {
state = digitalRead(pushButton); digitalWrite(led,state);
3.4 Testing
Now you can upload and run this program to Realtek Ameba RTL8195 board. Then, press Reset button.
4. Serial Communication (UART)
4.1 Getting Started
Realtek Ameba RTL8195 provides UART which can be accessed via Serial library or SoftwareSerial. Further information about Serial object, you can read it
on https://www.arduino.cc/en/Reference/Serial . We can call Serial.read() to read one byte from UART and Serial.write() to write one byte into UART.
UART is represented as RX and TX pins. You can see UART pins on Arduino Uno board. The following is Realtek Ameba RTL8195 pinout.
4.2 Hello UART
In this section, we try to use UART on Realtek Ameba RTL8195 board.
4.2.1 Preparation
On Realtek Ameba RTL8195, you can use UART pins on other digital pins. We can communicate with Realtek Ameba RTL8195 board from computer via UART using SoftwareSerial. In this
scenario, we need Serial hardware. The following is a list of UART tool you can use for this demo.
SparkFun FTDI Basic Breakout - 3.3V, https://www.sparkfun.com/products/9873
FTDI Cable 5V VCC-3.3V I/O, https://www.sparkfun.com/products/9717
Foca, http://imall.itead.cc/foca.html
In this scenario, I use Foca tool from iTead. We connect UART/Serial tool to Realtek Ameba RTL8195 log UART pins.
4.2.2 Connecting to Realtek Ameba RTL8195 LOG UART
Now we connect UART tool to Realtek Ameba RTL8195 LOG UART. The following is our wiring:
UART/Serial tool Tx is connected to Realtek Ameba RTL8195 Pin LOGUART_RX UART/Serial tool Rx is connected to Realtek Ameba RTL8195 Pin LOGUART_TX
4.2.3 Wiring
In this scenario, we use the same wiring from chapter 3. We will show pressed state from push button on Serial.
4.2.4 Writing a Program
To use Serial object, we need to initialize it by calling Serial.begin(baudrate). In this case, we can user baudrate 9600 on Realtek Ameba RTL8195.
Open Arduino software and write this program.
int led = 7;
int pushButton = 8; int state = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(pushButton, INPUT); Serial.begin(9600);
}
state = digitalRead(pushButton); digitalWrite(led,state);
Serial.print("State="); Serial.println(state); delay(300);
}
4.2.5 Testing
Now you can upload and run program. Don't forget to set board target with Realtek Ameba RTL8195. Read section 2.5 to upload the program.
On computer which is attached with UART/Serial too, run your serial application, for instance, I use CoolTerm, http://freeware.the-meiers.org . Instal and run this app. Now you can connect
UART/Serial USB tool to your computer.
If done, click OK button.
5. Working with Analog I/O
5.1 Getting Started
Realtek Ameba RTL8195 board provides Analog I/O which can be connected to sensor or actuator devices. Arduino Uno has PWM on digital pins: 3,4, 8, 9, 10, 11, 12, and 13. See the following of Realtek Ameba RTL8195 Analog input which is represented A0, A1, A2. Realtek Ameba RTL8195 also has analog output pin on DAC.
The following is pinout of Realtek Ameba RTL8195.
In this chapter, we try to access Realtek Ameba RTL8195 Analog I/O using Arduino software. There are three scenarios for our cases:
Controlling RGB LED
Reading Analog input using Potentiometer Writing Analog output.
5.2 Demo Analog Output (PWM) : RGB LED
In this scenario we build a program to control RGB LED color using Realtek Ameba
RTL8195 Analog output (PWM). RGB LED has 4 pins that you can see it on Figure below.
To understand these pins, you can see the following Figure.
Pin 1: Red
Pin 2: Common pin Pin 3: Green
Pin 4: Blue
Now we can start to build a program and hardware implementation.
5.2.1 Wiring
For our testing, we configure the following PWM pins.
RGB LED pin 1 (red) is connected to Realtek Ameba RTL8195 pin 9
RGB LED pin 2 is connected to Realtek Ameba RTL8195 3.3V (VCC +3.3V) or GND (depending on your RGB LED)
RGB LED pin 3 (green) is connected to Realtek Ameba RTL8195 pin 10 RGB LED pin 4 (blue) is connected to Realtek Ameba RTL8195 pin 11
5.2.2 Writing Program
To display a certain color, we must combine colors from red, green, blue. Realtek Ameba RTL8195 provides API for PWM like Arduino API such as analogWrite() and analogRead() with analog value from 0 to 255.
Let's start to build a program. Firstly, open Arduino Software. Then, write these scripts.
int redPin = 9; int greenPin = 10; int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600);
}
void loop() {
setColor(255, 0, 0); // red
Serial.println("red"); delay(1000);
setColor(0, 255, 0); // green
Serial.println("green"); delay(1000);
setColor(0, 0, 255); // blue
Serial.println("blue"); delay(1000);
setColor(255, 255, 0); // yellow
Serial.println("yellow"); delay(1000);
setColor(175, 0, 175); // purple
Serial.println("purple"); delay(1000);
setColor(0, 255, 255); // aqua
Serial.println("aqua"); delay(1000);
}
void setColor(int red, int green, int blue) {
}
Save this program as PWMDemo.
This program will generate six colors: red, green, blue, yellow, purple, and aqua.
Upload and run the program. You should see several color on RGB LED.
5.3 Demo Analog Input: Working with Potentiometer
In this section, we learn how to read analog input on Realtek Ameba RTL8195 board. For
illustration, I use Potentiometer as analog input source. Our scenario is to read analog value from Potentiometer. Then, display it on Serial Monitor.
Realtek Ameba RTL8195 has Analog inputs on A0, A1, A2. If you want to work with many analog inputs, you must expand it using ICs based ADC. In this section, we are working on Realtek Ameba RTL8195 Analog inputs.
Let's start!.
5.3.1 Wiring
To understand Potentiometer, you see its scheme in Figure below.
You can connect VCC to Realtek Ameba RTL8195 board on 3V3 pin (VCC +3.3V). Vout to Realtek Ameba RTL8195 board Analog input A0. In addition, GND to Realtek Ameba RTL8195 board GND.
5.3.2 Writing Program
Firstly, create a program using Arduino IDE. To read analog input, we can use analogRead() function. Ok, Let's write these scripts.
int val = 0;
void setup() {
Serial.begin(9600); }
void loop() {
val = analogRead(A0); Serial.print("ADC="); Serial.println(val); delay(300);
Save this code as ADCDemo.
5.3.3 Testing
5.4 Writing Analog Output.
In this section, we learn how to work with analog ouput. Realtek Ameba RTL8195 provides DAC on DAC0 pin. We explore this pin.
5.4.1 Wiring
For demo, we connect a LED to DAC pin. You also need UART tool to connect Realtek Ameba RTL8195 LOG UART. The following is our wiring demo.
5.4.2 Writing Program
The program will write a value to analog output using analogWrite().
Write this sketch program.
int val = 0;
void setup() {
void loop() {
analogWrite(DAC0, val); Serial.print("DAC="); Serial.println(val);
5.4.3 Testing
6. Working with I2C
6.1 Getting Started
The I2C (Inter-Integrated Circuit) bus was designed by Philips in the early '80s to allow easy communication between components which reside on the same circuit board. TWI stands for Two Wire Interface and for most marts this bus is identical to I²C. The name TWI was introduced by Atmel and other companies to avoid conflicts with trademark issues related to I²C.
I2C bus consists of two wires, SDA (Serial Data Line) and SCL (Serial Clock Line). You can see I2C pins on Realtek Ameba RTL8195 board, shown in Figure below.
For testing, I used PCF8591 AD/DA Converter module with sensor and actuator devices. You can find it on the following online store:
Amazon, http://www.amazon.com/PCF8591-Converter-Module-Digital-Conversion/dp/B00BXX4UWC/
eBay, http://www.ebay.com
Dealextreme, http://www.dx.com/p/pcf8591-ad-da-analog-to-digital-digital-to-analog-converter-module-w-dupont-cable-deep-blue-336384
Aliexpress, http://www.aliexpress.com/
This module has mini form model too, for instance, you can find it on Amazon,
http://www.amazon.com/WaveShare-PCF8591T-Converter-Evaluation-Development/dp/B00KM6X2OI/ .
This module use PCF8591 IC and you can read the datasheet on the following URLs.
http://www.nxp.com/documents/data_sheet/PCF8591.pdf
6.2 Writing Program
We use PCF8591 AD/DA Converter as I2C source. You can connect PCF8591 AD/DA Converter to Realtek Ameba RTL8195 board directly.
The following is our wiring lab:
PCF8591 AD/DA Converter SDA --> Realtek Ameba RTL8195 SDA PCF8591 AD/DA Converter SCL --> Realtek Ameba RTL8195 CLK
PCF8591 AD/DA Converter VCC --> Realtek Ameba RTL8195 VCC 3.3V (+3.3V) PCF8591 AD/DA Converter GND --> Realtek Ameba RTL8195 GND
6.3 Writing Program
We use I2C on Realtek Ameba RTL8195 board using Wire library like Arduino way. PCF8591 AD/DA Converter module has three sensor devices: Thermistor, Photo-voltaic cell and
Potentiometer. This module runs on I2C bus with address 0x90. In this case, we read all sensor data.
Open Arduino IDE and write this code.
#include "Wire.h"
#define PCF8591 (0x90 >> 1) // I2C bus address #define PCF8591_ADC_CH0 0x00 // thermistor
#define PCF8591_ADC_CH1 0x01 // photo-voltaic cell #define PCF8591_ADC_CH2 0x02
#define PCF8591_ADC_CH3 0x03 // potentiometer
byte ADC1, ADC2, ADC3;
void setup()
Wire.beginTransmission(PCF8591); Wire.write(PCF8591_ADC_CH0); Wire.endTransmission();
Wire.requestFrom(PCF8591, 2); delay(300);
ADC1=Wire.read(); ADC1=Wire.read();
Serial.print("Thermistor="); Serial.println(ADC1);
// read photo-voltaic cell
Wire.beginTransmission(PCF8591); Wire.write(PCF8591_ADC_CH1); Wire.endTransmission();
Wire.requestFrom(PCF8591, 2); delay(300);
ADC2=Wire.read(); ADC2=Wire.read();
Serial.print("Photo-voltaic cell="); Serial.println(ADC2);
Wire.beginTransmission(PCF8591); Wire.write(PCF8591_ADC_CH3); Wire.endTransmission();
Wire.requestFrom(PCF8591, 2); delay(300);
ADC3=Wire.read(); ADC3=Wire.read();
Serial.print("potentiometer="); Serial.println(ADC3);
6.4 Testing
Now you can upload and run the program to Realtek Ameba RTL8195 board board.
7. Working with SPI
7.1 Getting Started
The Serial Peripheral Interface (SPI) is a communication bus that is used to interface one or more slave peripheral integrated circuits (ICs) to a single master SPI device; usually a microcontroller or microprocessor of some sort.
SPI in Arduino Uno board can be defined on the following pins:
MOSI on DOUT pin (Digital pin 11) MISO on DIN pin (Digital pin 12) SCLK on SCK (Digital pin 13)
You can see these SPI pins on Realtek Ameba RTL8195 board, shown in Figure below.
We can only use one SPI on Realtek Ameba RTL8195 board with SPI master mode. We develop program based SPI using SPI library, https://www.arduino.cc/en/Reference/SPI .
7.2 Wiring
To develop SPI loopback, we can connect MOSI pin to MISO pin. This means you connect pin 12 to pin 11 using cable. You also need UART tool to connect Realtek Ameba RTL8195 LOG UART.
7.3 Writing a Program
Firstly, we write a program for Realtek Ameba RTL8195. Write these codes on Arduino IDE.
#include <SPI.h>
sendData = randomDigit();
recvData = SPI.transfer(sendData);
7.4 Testing
8. Working with EEPROM
8.1 Getting Started
EEPROM stands for Electrically Erasable Programmable Read-Only Memory and is a type of non-volatile memory used in computers and other electronic devices to store small amounts of data that must be saved when power is removed.
8.2 EEPROM Demo
In this section, we try to access EEPROM on Realtek Ameba RTL8195. We do the following actions:
allocate memory from 0x4000 to 0xFC000 read one word memory using readWord() write one word memory using writeWord()
To achieve these actions, you can start to write program on Arduino software.
Write this program.
#include <FlashMemory.h>
void setup() {
unsigned int value;
/* request flash size 0x4000 from 0xFC000 */
FlashMemory.begin(0xFC000, 0x4000);
/* read one word (32-bit) from 0xFC000 plus offset 0x3F00 */
value = FlashMemory.readWord(0x3F00);
printf("value is 0x%08X\r\n", value);
if (value == 0xFFFFFFFF) { value = 0;
} else { value++; }
/* write one word (32-bit) to 0xFC000 plus offset 0x3F00 */
FlashMemory.writeWord(0x3F00, value); }
Save this program as EEPROMDemo.
Now you can compile and upload the program into Realtek Ameba RTL8195 board.
If done, open serial app to see program output. Press Reset button.
9. Building Internet of Things Application
91 Getting Started
Realtek Ameba RTL8195 has built-in WIFi module. In this chapter, we explore how Realtek Ameba RTL8195 connect to Internet network.
9.2 Scanning Existing WiFi Hotspot
In this section, we try to scan existing WiFi hotspot. We use program samples from Realtek Ameba RTL8195. You can use the program sample on File -> Examples -> ScanNetworks.
You will get this program.
#include <WiFi.h>
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600); while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present"); // don't continue:
while (true); }
String fv = WiFi.firmwareVersion(); if (fv != "1.1.0") {
Serial.println("Please upgrade the firmware"); }
Serial.print(mac[1], HEX); Serial.print(":");
Serial.print(mac[2], HEX); Serial.print(":");
Serial.print(mac[3], HEX); Serial.print(":");
Serial.print(mac[4], HEX); Serial.print(":");
Serial.println(mac[5], HEX); }
void listNetworks() {
// scan for nearby networks:
Serial.println("** Scan Networks **"); int numSsid = WiFi.scanNetworks(); if (numSsid == -1) {
Serial.println("Couldn't get a wifi connection"); while (true);
}
// print the list of networks seen:
Serial.print("number of available networks:"); Serial.println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet < numSsid; thisNet++) { Serial.print(thisNet);
Serial.print(") ");
Serial.print(WiFi.SSID(thisNet)); Serial.print("\tSignal: ");
Serial.print(WiFi.RSSI(thisNet)); Serial.print(" dBm");
Serial.print("\tEncryptionRaw: ");
printEncryptionTypeEx(WiFi.encryptionTypeEx(thisNet)); Serial.print("\tEncryption: ");
printEncryptionType(WiFi.encryptionType(thisNet)); }
}
void printEncryptionTypeEx(uint32_t thisType) {
/* Arduino wifi api use encryption type to mapping to security type. * This function demonstrate how to get more richful information of security type.
break;
case SECURITY_WPA_TKIP_PSK: Serial.print("WPA TKIP"); break;
case SECURITY_WPA_AES_PSK: Serial.print("WPA AES"); break;
case SECURITY_WPA2_AES_PSK: Serial.print("WPA2 AES"); break;
case SECURITY_WPA2_TKIP_PSK: Serial.print("WPA2 TKIP"); break;
case SECURITY_WPA2_MIXED_PSK: Serial.print("WPA2 Mixed"); break;
case SECURITY_WPA_WPA2_MIXED: Serial.print("WPA/WPA2 AES"); break;
} }
void printEncryptionType(int thisType) {
// read the encryption type and print out the name:
You also need to connect Realtek Ameba RTL8195 LOG UART using UART tool.
After compiled and run the program to Realtek Ameba RTL8195, you can open a serial tool. Press Reset button if you don't see anything.
9.3 Building IoT Application
In this section, we build IoT application on Realtek Ameba RTL8195. We need three LEDs. We will turn on/off LEDs from a browser.
9.3.1 Wiring
We connect three LEDs to digital pin 8, 9 and 10. You also need to connect Realtek Ameba RTL8195 LOG UART through UART tool.
The following is my wiring.
9.3.2 Writing Application
Now you can open Arduino software. Write this program.
#include <WiFi.h>
char ssid[] = "ssid"; char pass[] = "pass";
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); delay(10);
// prepare GPIO5
pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); digitalWrite(led1, 0); digitalWrite(led2, 0); digitalWrite(led3, 0);
// Connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: "); Serial.println(ssid);
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
char ips[24];
IPAddress ip = WiFi.localIP();
sprintf(ips, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); Serial.println(ips);
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available(); if (!client) {
return; }
// Wait until the client sends some data
while(!client.available()){ delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r'); Serial.println(req);
Serial.println("invalid request"); client.stop();
return; }
// Set GPIO2 according to the request
digitalWrite(led1, val1); digitalWrite(led2, val2); digitalWrite(led3, val3);
// 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\n";
Serial.println("Client disonnected");
Save this program as IoTDemo.
9.3.3 Testing
Open a browser and navigate to http://<server>/gpio1/1. This URL will turn on LED 1.
You can turn off LED 1 by calling URL http://<server>/gpio1/0.
Source Code
Contact
If you have question related to this book, please contact me at [email protected] . My blog: