Skip to content

Instantly share code, notes, and snippets.

@Tomokatsu-Sakamoto
Last active January 24, 2018 02:40
Show Gist options
  • Save Tomokatsu-Sakamoto/41a5a03327d63b3d78b014b4fe5c17da to your computer and use it in GitHub Desktop.
Save Tomokatsu-Sakamoto/41a5a03327d63b3d78b014b4fe5c17da to your computer and use it in GitHub Desktop.
ESP-WROOM-02 を用いたコントローラー(子機側)
///////////////////////////////////////////////////////////////////////
// 現在の Wi-Fi 状態を表示
int WiFi_Status( void )
{
int status = WL_IDLE_STATUS; // the Wifi radio's status
Serial.print(
"WiFi.status( ) = " );
switch ( status = WiFi.status( ) ) {
case WL_CONNECTED:
// assigned when connected to a WiFi network;
Serial.println( "WL_CONNECTED" );
break;
case WL_NO_SHIELD:
// assigned when no WiFi shield is present;
Serial.println( "WL_NO_SHIELD" );
break;
case WL_IDLE_STATUS:
// it is a temporary status assigned when WiFi.begin() is called and
// remains active until the number of attempts expires (resulting in
// WL_CONNECT_FAILED) or a connection is established (resulting in
// WL_CONNECTED);
Serial.println( "WL_IDLE_STATUS" );
break;
case WL_NO_SSID_AVAIL:
// assigned when no SSID are available;
Serial.println( "WL_NO_SSID_AVAIL" );
break;
case WL_SCAN_COMPLETED:
// assigned when the scan networks is completed;
Serial.println( "WL_SCAN_COMPLETED" );
break;
case WL_CONNECT_FAILED:
// assigned when the connection fails for all the attempts;
Serial.println( "WL_CONNECT_FAILED" );
break;
case WL_CONNECTION_LOST:
// assigned when the connection is lost;
Serial.println( "WL_CONNECTION_LOST" );
break;
case WL_DISCONNECTED:
// assigned when disconnected from a network;
Serial.println( "WL_DISCONNECTED" );
break;
default:
Serial.println( "? ? ? ? ? ? ? ? ? ?" );
break;
}
return status;
}
///////////////////////////////////////////////////////////////////////
// 現在のネットワーク情報を表示
void WiFi_Information( void )
{
int status = WL_IDLE_STATUS; // the Wifi radio's status
byte mac[6]; // the MAC address of your Wifi shield
Serial.println(
"------------------------------------------------" );
Serial.println(
"--- WiFi Infomation ---" );
Serial.println(
"------------------------------------------------" );
Serial.print(
"WiFi.localIP( ) = " );
Serial.println( WiFi.localIP( ) );
Serial.print(
"WiFi.subnetMask( ) = " );
Serial.println( WiFi.subnetMask( ) );
Serial.print(
"WiFi.gatewayIP( ) = " );
Serial.println( WiFi.gatewayIP( ) );
status = WiFi_Status( );
if ( status != WL_CONNECTED ) {
Serial.println("Couldn't get a wifi connection");
}
else {
// if you are connected, print your MAC address:
WiFi.macAddress( mac );
Serial.print(
"MAC ( Physical Address ) = " );
Serial.print( mac[ 5 ], HEX );
Serial.print( ":" );
Serial.print( mac[ 4 ], HEX );
Serial.print( ":" );
Serial.print( mac[ 3 ], HEX );
Serial.print( ":" );
Serial.print( mac[ 2 ], HEX );
Serial.print( ":" );
Serial.print( mac[ 1 ], HEX );
Serial.print( ":" );
Serial.println( mac[ 0 ], HEX );
}
Serial.println(
"------------------------------------------------" );
Serial.println( );
}
/*
ESP8266 + A/D変換
アナログ入力× 2 とデジタル入力× 2 を UDP で送信
ESP8266 15 : MCP3204 8-CS
13 : MCP3204 9-Din
12 : MCP3204 10-Dout
14 : MCP3204 11-CLK
: MCP3204 1-アナログ入力 1
: MCP3204 2-アナログ入力 2
IO05 : スイッチ 1
IO16 : スイッチ 2
http://tc96.hatenablog.com/entry/2016/09/14/130932
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiUDP.h>
#include <SPI.h>
#define BUFF_SIZE 16
char lineBuff[ BUFF_SIZE ]; // LCDの1行分バッファ
// UDP setting
WiFiUDP UDP;
// WiFi setting
const char *ssid = "ANY";
const char *password = "*";
#define CS 15
#define LED_OUT 16
#define SW1 4
#define SW2 5
#define RETRY_MAX 40
int iLedCnt = 0;
IPAddress HOSTIP;
void reConnect( void )
{
int iStatus;
int iCount = 0;
Serial.println( "scan start" );
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks( );
Serial.println( "scan done" );
if ( n == 0 )
Serial.println( "no networks found" );
else
{
Serial.print( n );
Serial.println( " networks found" );
for ( int i = 0 ; i < n ; ++i )
{
// Print SSID and RSSI for each network found
Serial.print( i + 1 );
Serial.print( ": " );
Serial.print( WiFi.SSID( i ) );
Serial.print( " (" );
Serial.print(WiFi.RSSI( i ) );
Serial.print( ")" );
Serial.print( ( WiFi.encryptionType( i ) == ENC_TYPE_NONE ) ? " " : "*");
delay(10);
WiFi.SSID( i ).toCharArray( lineBuff, BUFF_SIZE );
if (
// ( WiFi.encryptionType( i ) == ENC_TYPE_NONE ) &&
( lineBuff[ 0 ] == 'E' ) &&
( lineBuff[ 1 ] == 'S' ) &&
( lineBuff[ 2 ] == 'P' )
) {
// 暗号化されていない AP に接続する
Serial.println( " connection start!" );
WiFi.begin( lineBuff, password );
while ( ( iStatus = WiFi.status( ) ) != WL_CONNECTED ) {
delay( 500 );
iLedCnt++;
digitalWrite( LED_OUT, ( iLedCnt % 2 ) );
Serial.printf( "%d", iStatus );
iCount++;
if ( iCount >= RETRY_MAX ) {
iCount = 0;
Serial.print( " " );
WiFi_Status( );
WiFi.printDiag( Serial );
WiFi.reconnect( );
}
}
}
else {
Serial.println( " not covered." );
}
}
}
delay( 1000 );
WiFi_Information( );
}
void setup( )
{
ESP.eraseConfig( );
delay( 500 );
Serial.begin( 115200 );
Serial.println( );
Serial.printf("Serial is %d bps", Serial.baudRate( ) );
pinMode( CS, OUTPUT );
pinMode( LED_OUT, OUTPUT );
pinMode( SW1, INPUT_PULLUP );
pinMode( SW2, INPUT_PULLUP );
SPI.begin( );
//----- SET SPI MODE -----
//SPI_MODE_0 (0,0) CPOL=0 (Clock Idle low level), CPHA=0 (SDO transmit/change edge active to idle)
//SPI_MODE_1 (0,1) CPOL=0 (Clock Idle low level), CPHA=1 (SDO transmit/change edge idle to active)
//SPI_MODE_2 (1,0) CPOL=1 (Clock Idle high level), CPHA=0 (SDO transmit/change edge active to idle)
//SPI_MODE_3 (1,1) CPOL=1 (Clock Idle high level), CPHA=1 (SDO transmit/change edge idle to active)
SPI.setDataMode( SPI_MODE0 ); // データシート 図6.1
SPI.setBitOrder( MSBFIRST ); // most-significant bit first
WiFi.mode( WIFI_STA );
WiFi.disconnect( );
delay( 100 );
reConnect( );
Serial.println( "" );
Serial.print( "WiFi connected." );
Serial.print( "Connected as " );
Serial.println( WiFi.localIP( ) );
HOSTIP[ 0 ] = WiFi.localIP( )[ 0 ];
HOSTIP[ 1 ] = WiFi.localIP( )[ 1 ];
HOSTIP[ 2 ] = WiFi.localIP( )[ 2 ];
HOSTIP[ 3 ] = 1;
UDP.begin( 893 ); //
Serial.println( "UDP.begin!" );
}
int getMCP3204( int iCh )
{
int highByte, lowByte;
// Reading a data from MCP3204
digitalWrite( CS, LOW );
highByte = SPI.transfer( 0x06 | ( iCh >> 2 ) );
highByte = SPI.transfer( iCh << 6 );
lowByte = SPI.transfer( 0x00 );
digitalWrite( CS, HIGH );
// Serial.printf("MCP3204(%d) %02X-%02X ", iCh, highByte, lowByte );
return ( highByte & 0x0F ) * 256 + lowByte;
}
void sendWiFi( char byteData[ ] )
{
if (UDP.beginPacket( HOSTIP, 893 ) ) {
UDP.write( byteData, 3 );
UDP.endPacket( );
// Serial.println( byteData );
}
}
void loop( )
{
static int iLoop = 0;
int iCh0, iCh1, iSw1, iSw2;
char sendData[ 4 ] = { 0, 0, 0, 0, };
if ( WiFi.status( ) != WL_CONNECTED ) {
reConnect( );
}
else {
iLedCnt++;
iLedCnt %= 10;
digitalWrite( LED_OUT, ( iLedCnt == 0 ) );
iCh0 = getMCP3204( 0 ) ;
iCh1 = getMCP3204( 1 ) ;
iSw1 = digitalRead( SW1 );
iSw2 = digitalRead( SW2 );
sendData[ 0 ] = 'A' + ( iCh0 >> 8 );
sendData[ 1 ] = 'A' + ( iCh1 >> 8 );
sendData[ 2 ] = '0' + iSw1 + ( iSw2 * 2 );
Serial.printf("%6d Ch0=%4d, Ch1=%4d, Sw1=%4d, Sw2=%4d (%02X-%02X-%02X) %s\n",
iLoop++, iCh0, iCh1, iSw1, iSw2,
sendData[ 0 ], sendData[ 1 ], sendData[ 2 ], sendData );
sendWiFi( sendData );
}
delay( 50 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment