Skip to content

Instantly share code, notes, and snippets.

@Tomokatsu-Sakamoto
Last active January 24, 2018 02:38
Show Gist options
  • Save Tomokatsu-Sakamoto/514054ab091c4de3f8d48f2778a0b170 to your computer and use it in GitHub Desktop.
Save Tomokatsu-Sakamoto/514054ab091c4de3f8d48f2778a0b170 to your computer and use it in GitHub Desktop.
ESP-WROOM-02 を用いたコントローラー(親機側)
///////////////////////////////////////////////////////////////////////
// ESP-WROOM-02 のチップIDを用いて SSID を生成する
void getSSID(
char *buff ) //
{
sprintf( buff, "ESP_%6X", ESP.getChipId( ) );
}
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUDP.h>
#include <ST7032.h>
ST7032 lcd;
#define BUFF_SIZE 16
char lineBuff[ BUFF_SIZE ]; // LCDの1行分バッファ
// UDP setting
WiFiUDP UDP;
// WiFi setting
IPAddress myIP(192, 168, 4, 1);
void setup( )
{
Serial.begin( 115200 );
Serial.println( "" );
lcd.begin( 8, 2 );
lcd.setContrast( 30 ); // コントラスト設定
lcd.setCursor( 0, 0 );
lcd.printf( "Wait " );
lcd.setCursor( 0, 1 );
lcd.printf( "Connect." );
WiFi.mode( WIFI_AP ); // Wi-Fi のアクセスポイントとして動作
getSSID( lineBuff );
WiFi.softAP( lineBuff );
WiFi.config( myIP, WiFi.gatewayIP( ), WiFi.subnetMask( ) );
UDP.begin( 893 ); //
WiFi.printDiag( Serial );
Serial.println( "UDP.begin!" );
}
void rcvWiFi( char *cBuff, int iSize )
{
static unsigned char cCount = 0;
cCount++;
lcd.clear( );
sprintf( cBuff, "L:%3d(%02X", iSize, cCount );
lcd.print( cBuff );
if ( iSize > 8 ) {
iSize = 8;
}
UDP.read( cBuff, iSize );
cBuff[ iSize ] = '\0'; // 表示用に '\0' を付加
Serial.println( cBuff );
UDP.flush( );
lcd.setCursor( 0, 1 );
lcd.printf( cBuff );
}
void loop( )
{
int iSize;
if ( ( iSize = UDP.parsePacket( ) ) > 0 ) {
Serial.printf( "Receive : %d ", iSize );
rcvWiFi( lineBuff, iSize );
}
delay( 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment