ESP-WROOM-02を使ってみる

この間aitendoに寄った際にESP-WROOM-02I2Cキャラクタ液晶モジュールなどを買ったので、使ってみました。

ESP-WROOM-02はCPUからRF回路まですべて一体となっているモジュールなので、別のマイコンから制御する必要はありません。GPIO、UART・I2Cなどシリアル通信、A/D変換も備えていてワンチップマイコンと同じ感覚で使うことができます。さらにArduino IDEで開発できてしまうため、短時間でのプロトタイピングが可能です。

以下はESP-WROOM-02をWiFiアクセスポイントとWebサーバーとして動作させ、またWebページのフォームに入力した文字列をI2Cキャラクタ液晶モジュールに表示させるプログラムです。
サンプルスケッチなどを参考にして作成しました。

プログラム:

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <string.h>
#define I2C_LCD_ADDR 0x3e

const char *ssid = "esp8266-i2clcd-text-AP";
const char *password = "thereisnospoon1510";
const int channel = 11;
const int ssid_hidden =0;

ESP8266WebServer server(80);

void handleRoot()
{
  server.send(200, "text/html", "<!DOCTYPE html><html><body><h1>You are connected</h1><p>Text to display: </p><form action=\"/write\" method=\"get\"><input type=\"text\" name=\"text\" size=\"40\"> <input type=\"submit\" value=\"Submit\"></form></body></html>");
}

void handleWrite()
{
  lcd_printStr(0, 1, "                ");
  lcd_printStr(0, 1, server.arg(0));
  
  server.send(200, "text/html", "<!DOCTYPE html><html><body><h1>Wrote successfully</h1><p><a href=\"/\">Return</a></p></body></html>");
}

void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for(uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

void setup()
{
  delay(1000);
  Serial.begin(115200);
  delay(10);
  Serial.println("");
  Wire.begin(2, 14);
  delay(40);

  lcd_init();

  //Prepare server
  WiFi.softAP(ssid, password, channel, ssid_hidden);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleRoot);
  server.on("/write", handleWrite);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
  
  //Show my IP address on LCD
  String ipStr=String(myIP[0])+'.'+String(myIP[1])+'.'+String(myIP[2])+'.'+String(myIP[3]);
  lcd_printStr(ipStr);
}

void loop()
{
  server.handleClient();
}

void lcd_init()
{
  uint8_t cmd_init1[]={0x38, 0x39, 0x14, 0x70, 0x56, 0x6c};
  uint8_t cmd_init2[]={0x38, 0x0c, 0x01};

  lcd_cmd(cmd_init1, sizeof(cmd_init1));
  delay(201); //wait time >200ms
  lcd_cmd(cmd_init2, sizeof(cmd_init2));
  delayMicroseconds(1100);  //wait time >1.08ms
}

void lcd_cmd(uint8_t *cmd, size_t len)
{
  size_t i;
  for(i=0; i<len; i++){
    Wire.beginTransmission(I2C_LCD_ADDR);
    Wire.write(0x00);
    Wire.write(cmd[i]);
    Wire.endTransmission();
    delayMicroseconds(27);    // 26.3us
  }
}

void lcd_write(uint8_t *cmd, size_t len)
{
  size_t i;
  for(i=0; i<len; i++){
    Wire.beginTransmission(I2C_LCD_ADDR);
    Wire.write(0x40);
    Wire.write(cmd[i]);
    Wire.endTransmission();
    delayMicroseconds(27);    // 26.3us
  }
}

void lcd_setpos(uint8_t x, uint8_t y)
{
  uint8_t cmd=0x80;
  if(y>0) cmd|=(0x40+(x&0x7f));
  else cmd|=(x&0x7f);
  lcd_cmd(&cmd, 1);
}

void lcd_printStr(String str)
{
  char lbuf[17];
  str.toCharArray(lbuf, 17);
  lcd_write((uint8_t *)lbuf, strlen(lbuf));
}

void lcd_print(char str[])
{
  lcd_write((uint8_t *)str, strlen(str));
}

void lcd_printStr(uint8_t x, uint8_t y, String str)
{
  char lbuf[17];
  str.toCharArray(lbuf, 17);
  lcd_setpos(x, y);
  lcd_write((uint8_t *)lbuf, strlen(lbuf));
}

void lcd_print(uint8_t x, uint8_t y, char str[])
{
  lcd_setpos(x, y);
  lcd_write((uint8_t *)str, strlen(str));
}

回路図:
esp8266-i2clcd-text

動作させるとLCDにIPアドレスが表示されます。
IMG_20151012_210751

APに接続し、ブラウザで192.168.4.1にアクセスします。
Screenshot_2015-10-12-21-08-22a Screenshot_2015-10-12-21-08-40

テキストボックスに文字を打ち込み、SubmitボタンでLCDに表示します。
Screenshot_2015-10-12-21-09-09 Screenshot_2015-10-12-21-09-15
IMG_20151012_210925
正しく表示されました。
表示先を電光掲示板など大きめの画面にすれば、手軽なメッセージボードとして応用できそうです。

参考にしたサイト:
ESP8266 – ESP-WROOM-02 の Arduino 環境で I2C 制御 – Qiita
技適済み格安高性能Wi-FiモジュールESP8266をArduinoIDEを使ってIoT開発する為の環境準備を10分でやる方法 – Qiita
ESP8266 – ESP-WROOM-02でWeb Serverを動かす – Qiita

情報機器やインターネットに接続できるESP-WROOM-02は無限の可能性を秘めています。このようなモジュールの普及で、今後のIoTがより加速するかもしれません。

What do you think?

Note: Your email address will not be published

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

%d人のブロガーが「いいね」をつけました。