WebBrowserで利用するケース

最終更新日:2019年12月27日


Arduinoの場合、EtherNetshieldを接続して、ブラウザから “Lチカ”制御する という記事をよく見かけます。方法論はいくつかあり、記事によって分類できます。ブラウザで表示するHTMLのコードをフラッシュに書くか、SDカードから読み込ませ、要求のあったクライアントに送信しないといけないのですが、UNOに搭載された2KBのメモリではEtherNetスタックすら構築できない場合があります。特にChipがENC28J60だとビルドが通らない可能性が高いです。MEGAだと可成り緩和されますが、BluePillにすることで一気に緩和されるので 今回の課題は 意義があると感じています。

ブラウザから制御する場合、SUBMITボタンを押したときに ? 以降にパラメータを付けて置き、WebServerがこれを受け取って解釈し、IO制御して更に表示更新すると云う方法がオーソドックスです。ただ、これは、都度HTMLをページ更新となります。負荷が可成り有るという認識です。データ更新をそれなりに密に行う場合、この方法は良くないです。
実際のところAjaxを使う例が増えてきました。Ajaxで実現すると、応答性が改善されますし、LAN内のトラフィックも改善されると信じています。
ここではAjaxを採用し、8pinを出力設定し、LED接続してON/OFF制御出来るモノを作っておきます。

http://ipアドレス でアクセスすると、制御画面がでてLEDのON/OFF制御が出来ます。


※I2C未接続だと数値表示されません。そのように記述しています。

http://ipアドレス/setup でアクセスすると MACアドレス等の情報を確認/変更出来ます。これによって、一つのバイナリーコードがあればMACアドレスの異なるモジュールをいくつも構築可能となります。また、IPアドレスも自由に設定出来ることになります。

ソースコードはこんな感じになります。
このコードを実行するに当たり追加が必要なライブラリは、

/*
 * 20191219 T.Wanibe STM32MINIShieldEvaluation
 * STM32MINIShieldEvaluationPin
 * MACWiznet
 * MAC
 * IP192.168.0.xZone192.168.0.200
 * W5500DHCPFIX
 * DO4PB12,PB13,PB14,PB15
 * DI4PB11,PB10,PB1,PB0
 * AI4PA0,PA1,PA2,PA3
 * I2CGY21(HTU21DF)GY2561(TSL2561)
 * HWPB6/PB710KPullUpI2C
 * R8,R9
 * I2CSTM32duino
 * Arduino
 * SPImodule
 * Build
 * 1310725734443%
 * 20480RAM459222%15888
 */
#include <SPI.h>                        // for Ethernet.h
#include <Ethernet3.h>                  // for Ethernet
#include <EEPROM.h>
#include <avr/pgmspace.h>
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
#include <SparkFunTSL2561.h>
#define HttpPort        80
#define LED             PC13            //BluePillLED
//I2C
#define GY21ADRS        0x48
#define GY2561ADRS      0x39            //TSL2561_ADDR
//PIN
#define W550io_Rst      PA8             //NIC_Reset
#define SPI1_NSS_PIN    PA4             //NIC_ChipSelect
#define AI1             PA0             //1
#define AI2             PA1             //2
#define AI3             PA2             //3
#define AI4             PA3             //4
#define LED1            PB12            //1
#define LED2            PB13            //2
#define LED3            PB14            //3
#define LED4            PB15            //4
#define SW1             PB11            //1
#define SW2             PB10            //2
#define SW3             PB1             //3
#define SW4             PB0             //4
//----------------------- VARIABLES LED ------------------------//
byte mac[]              = {0x00,0x08,0xDC,0x54,0x4D,0xD0};      //Wiznet
byte ip[]               = {192, 168, 0, 199};
byte dns_server[]       = {192, 168, 0, 1};
byte gateway[]          = {0, 0, 0, 0};
byte subnet[]           = {255, 255, 255, 0};
String HTTP_req;                                        // stores the HTTP request
int dout[5]             = {1,1,1,1,1};                  // 1 0
int din[4]              = {0,0,0,0};                    // 1 =High
int ai[4]               = {0,0,0,0};                    // STM3212bit0-4095
boolean gain;                                           // HTU21DFGain setting, 0 = X1, 1 = X16;
unsigned int ms;                                        // HTU21DFIntegration ("shutter") time in milliseconds
unsigned int data0, data1;                              // HTU21DF
const byte EID          = 0x92;                         //KEYID
const char titleStr[]   = "STM32MINIShieldEvaluation";
byte    ErrorHTU,ErrorLight;
EthernetServer server(HttpPort);
EthernetClient client;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
SFE_TSL2561 light;
//----------------------------------
void LANSetup(){
        int idcheck = EEPROM.read(0);                   //KEYID
        if (idcheck != EID){
                //idID
                //
                //
        }
        if (idcheck == EID){
                //idID
                //
                //EERPOM
                for (int i = 0; i < 6; i++){
                        mac[i]          = EEPROM.read(i+1);
                }
                for (int i = 0; i < 4; i++){
                        ip[i]           = EEPROM.read(i+7);
                }
                for (int i = 0; i < 4; i++){
                        subnet[i]       = EEPROM.read(i+11);
                }
                for (int i = 0; i < 4; i++){
                        gateway[i]      = EEPROM.read(i+15);
                }
        }
        Ethernet.begin(mac, ip);                        //
//      Ethernet.begin(mac);                            //DHCP
//      Ethernet.begin(mac, ip, subnet);                //SubnetMask
//      Ethernet.begin(mac, ip, subnet, gateway);       //gateway
}
//--------------------------HTML
//      HTMLHTMLclient.println(F(""))
void PrintNormalPage( EthernetClient client,String parm)
{
        PrintResponse200(client);
        
        client.println(F("<!doctype html>"));        
        client.print(F("<html lang='ja'>\n\t<head>\n\t\t<title>"));
        client.print(titleStr);
        client.println(F("</title>\n"));
        client.println(F("\t\t<meta charset='UTF-8' name='viewport' content='width=240px' />\n"));
        client.println(F("\t\t<script type='text/javascript'>\n"));
        
        client.println(F("\t\tfunction createXMLHttpRequest(cbFunc){\n"));
        client.println(F("\t\t\tvar XObj = new XMLHttpRequest();\n"));
        client.println(F("\t\t\tif(XObj) XObj.onreadystatechange = cbFunc;return XObj;\n\t\t}\n"));
        client.println(F("\t\tfunction setData(val){\n\t\t\thtObj = createXMLHttpRequest(displayData);\n"));
        client.println(F("\t\t\tif(htObj){\n\t\t\t\thtObj.open('GET','/?btn='+val,true);htObj.send(null);\n\t\t\t}\n\t\t}\n"));
        client.println(F("\t\tfunction getData(){\n\t\t\thtObj = createXMLHttpRequest(displayData);\n"));
        client.println(F("\t\t\tif(htObj){\n\t\t\t\thtObj.open('GET','/?=',true);htObj.send(null);\n\t\t\t}\n\t\t}\n"));
        client.println(F("\t\tfunction displayData(){\n\t\t\tif((htObj.readyState == 4) && (htObj.status == 200)){\n"));
        client.println(F("\t\t\tdocument.getElementById('result').innerHTML =  htObj.responseText;\n\t\t\t}\n\t\t}\n"));
        client.println(F("\t\tfunction strT(){\n\t\t\tgetData(); timerID=setTimeout('strT()',document.getElementById('tf1').value);\n")); 
        client.println(F("\t\t\tdocument.getElementById('btn100').value = 'STARTING';\n\t\t}\n"));
        client.println(F("\t\tfunction clrT(){\n\t\t\tclearTimeout(timerID);\n"));
        client.println(F("\t\t\tdocument.getElementById('btn100').value = 'START';\n\t\t}\n"));
        client.println(F("\t\twindow.onload = function() {"));
        client.println(F("\t\t\tif (navigator.onLine === true) {"));
        client.println(F("\t\t\t\talert('current network status is online');"));
        client.println(F("\t\t\t} else if (navigator.onLine === false) {"));
        client.println(F("\t\t\t\talert('current network status is offline');"));
        client.println(F("\t\t\t} else {"));
        client.println(F("\t\t\t\talert('current network status is unknown');"));
        client.println(F("\t\t\t}"));
        client.println(F("\t\t}"));
        client.println(F("\t\tfunction setInputBackGroundColor(idname,flg){"));
        client.println(F("\t\t\tvar obj = document.getElementByName(idname);"));                        //  
        client.println(F("\t\t\tif(flg){\n\t\t\t\tobj[0].stle.backgroundColor='#ff0000';\n"));          //
        client.println(F("\t\t\t}else{\n\t\t\t\tobj[0].stle.backgroundColor='#ffffff';\n"));            //  
        client.println(F("\t\t\t}\n\t\t}\n"));
        client.println(F("\t\t</script>\n\t</head>\n\t<body onLoad='getData()'>\n"));
        client.println(F("\t<form action='/' method='GET'>\n"));
        client.print(F("\t<h2 class='text-center'><P><CENTER>"));
        client.print(titleStr);
        client.println(F("</CENTER></h2>\n"));
        client.println(F("\t<BLOCKQUOTE>\n\t\t<CENTER><BR / CLEAR='ALL'><TABLE WIDTH='280' BORDER='0' CELLSPACING='0' CELLPADDING='0'>\n"));
        client.println(F("\t\t\t<TR>\n\t\t\t\t<TD WIDTH='100%'>\n"));
        client.println(F("\t\t\t\t<input id='btn0' type='button' value=' PA0 ' onClick='setData(1)'>\n"));
        client.println(F("\t\t\t\t<input id='btn1' type='button' value=' PA1 ' onClick='setData(2)'>\n"));
        client.println(F("\t\t\t\t<input id='btn2' type='button' value=' PA2  ' onClick='setData(3)'>\n"));
        client.println(F("\t\t\t\t<input id='btn3' type='button' value=' PA3  ' onClick='setData(4)'><br / CLEAR='ALL'>\n"));
        client.println(F("\t\t\t\t<input id='btn100' type='button' value=' START ' onClick='strT()'>\n"));
        client.println(F("\t\t\t\t<input id='btn200' type='button' value=' STOP ' onClick='clrT()'>\n"));
        client.println(F("\t\t\t\t<input id='tf1' type='text' size='5' value='2000'>[msec]\n"));
        client.println(F("\t\t\t\t<br / CLEAR='ALL'></TD>\n\t\t\t</TR>\n\t\t</TABLE>\n\t\t<P><HR></P>\n\t\t<div id='result'></div></CENTER><br />\n"));
        client.println(F("\t</BLOCKQUOTE>\n\t</form>\n\t</body>\n</html>"));
        
        HTTP_req = "";
}
//--------------------------HTML(SetUp)
void PrintSetUpPage( EthernetClient client,String parm)
{
        PrintResponse200(client);
        client.println(F("<!DOCTYPE HTML PUBLIC \"\"><html><HEAD>"));
        client.println(F("<META http-equiv=\"Content-Type\" charset=UTF-8\">"));
        client.println(F("<META http-equiv=\"Content-Style-Type\">"));
        client.print(F("<TITLE>"));
        client.print(titleStr);
        client.println(F(" Setup Page</TITLE></HEAD>"));
        client.println(F("<BODY marginwidth=\"0\" marginheight=\"0\" "));
        client.println(F("leftmargin=\"0\" style=\"margin: 0; padding: 0;\">"));
        client.println(F("<BLOCKQUOTE><BLOCKQUOTE><table bgcolor=\"#17A1A5\" "));
        client.println(F("border=\"0\" width=\"100%\" cellpadding=\"1\" "));
        client.println(F("style=\"font-family:Verdana;color:#ffffff;font-size:12px;\">"));
        client.println(F("<tr><td>&nbsp;"));
        client.print(titleStr);
        client.println(F(" Setup Page</td></tr></table><br>"));
        //
        client.println(F("<script>function hex2num (s_hex) {eval(\"var n_num=0X\" + "));
        client.println(F("s_hex);return n_num;}</script><FORM><input type=\"hidden\" "));
        client.println(F("name=\"SBM\" value=\"1\"><table><tr><td>MAC:</td><td>"));
        client.println(F("<input id=\"T1\" type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"2\" name=\"DT1\" value=\""));
        client.print(mac[0],HEX);
        client.println(F("\">.<input id=\"T3\" type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"2\" name=\"DT2\" value=\""));
        client.print(mac[1],HEX);
        client.println(F("\">.<input id=\"T5\" type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"2\" name=\"DT3\" value=\""));
        client.print(mac[2],HEX);
        client.println(F("\">.<input id=\"T7\" type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"2\" name=\"DT4\" value=\""));
        client.print(mac[3],HEX);
        client.println(F("\">.<input id=\"T9\" type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"2\" name=\"DT5\" value=\""));
        client.print(mac[4],HEX);
        client.println(F("\">.<input id=\"T11\" type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"2\" name=\"DT6\" value=\""));
        client.print(mac[5],HEX);
        //
        client.println(F("\"><input id=\"T2\" type=\"hidden\" name=\"DT1\">"));
        client.println(F("<input id=\"T4\" type=\"hidden\" name=\"DT2\">"));
        client.println(F("<input id=\"T6\" type=\"hidden\" name=\"DT3\">"));
        client.println(F("<input id=\"T8\" type=\"hidden\" name=\"DT4\">"));
        client.println(F("<input id=\"T10\" type=\"hidden\" name=\"DT5\">"));
        client.println(F("<input id=\"T12\" type=\"hidden\" name=\"DT6\">"));
        client.println(F("</td></tr><tr><td>IP:</td><td><input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT7\" value=\""));
        client.print(ip[0],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT8\" value=\""));
        client.print(ip[1],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT9\" value=\""));
        client.print(ip[2],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT10\" value=\""));
        client.print(ip[3],DEC);
        //
        client.println(F("\"></td></tr><tr><td>MASK:</td><td><input type="));
        client.println(F("\"text\" size=\"3\" maxlength=\"3\" name=\"DT11\" value=\""));
        client.print(subnet[0],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT12\" value=\""));
        client.print(subnet[1],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT13\" value=\""));
        client.print(subnet[2],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT14\" value=\""));
        client.print(subnet[3],DEC);
        //
        client.println(F("\"></td></tr><tr><td>GW:</td><td><input type=\"text\" "));
        client.println(F("size=\"3\" maxlength=\"3\" name=\"DT15\" value=\""));
        client.print(gateway[0],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT16\" value=\""));
        client.print(gateway[1],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT17\" value=\""));
        client.print(gateway[2],DEC);
        client.println(F("\">.<input type=\"text\" size=\"3\" "));
        client.println(F("maxlength=\"3\" name=\"DT18\" value=\""));
        client.print(gateway[3],DEC);
        client.println(F("\"></td></tr><tr><td><br></td></tr><tr><td>"));
        client.println(F("<input id=\"button1\"type=\"submit\" value=\"SUBMIT\" "));
        client.println(F("Onclick =\"document.getElementById('T2').value "));
        client.println(F("= hex2num(document.getElementById('T1').value);"));
        client.println(F("document.getElementById('T4').value "));
        client.println(F("= hex2num(document.getElementById('T3').value);"));
        client.println(F("document.getElementById('T6').value "));
        client.println(F("= hex2num(document.getElementById('T5').value);"));
        client.println(F("document.getElementById('T8').value "));
        client.println(F("= hex2num(document.getElementById('T7').value);"));
        client.println(F("document.getElementById('T10').value "));
        client.println(F("= hex2num(document.getElementById('T9').value);"));
        client.println(F("document.getElementById('T12').value "));
        client.println(F("= hex2num(document.getElementById('T11').value);\""));
        
        client.println(F("></td><td></td></tr></form></table>"));
        client.println(F("</BLOCKQUOTE></BLOCKQUOTE></BODY></html>"));
        HTTP_req = "";
}
//--------------------------
void PrintResponse200( EthernetClient client)
{
        client.println(F("HTTP/1.1 200 OK"));
        client.println(F("Content-Type: text/html"));
        client.println(F("Connnection: close")); 
        client.println();
}
//--------------------------
void PrintResponse404( EthernetClient client )
{
        client.println(F("HTTP/1.1 404 Not Found"));
        client.println(F("Content-Type: text/html"));
        client.println(F("Connnection: close")); 
        client.println();
}
//--------------------------
void XMLhttpObjectAccess(EthernetClient client,String parm)
{
        int check = parm.indexOf('=');
        if(check != -1){
                //Set Digital Port
                int check2 = parm.indexOf('btn');
                if(check2 != -1){
                        int port = (parm.substring(check+1)).toInt();
                        dout[port] = !dout[port];
                        switch(port){
                                case 0:
                                        //digitalWrite(LED, dout[port]);
                                        break;
                                case 1:
                                        digitalWrite(LED1, dout[port]);
                                        break;
                                case 2:
                                        digitalWrite(LED2, dout[port]);
                                        break;
                                case 3:
                                        digitalWrite(LED3, dout[port]);
                                        break;
                                case 4:
                                        digitalWrite(LED4, dout[port]);
                                        break;
                                default:
                                        break;
                                      
                        }
                }
                //
                double lux;             // Resulting lux value
                boolean good;           // True if neither sensor is saturated
                if(!ErrorLight){
                        if (light.getData(data0,data1)){
                                good = light.getLux(gain,ms,data0,data1,lux);
                        }else{
                                byte error = light.getError();
                                printError(error);
                        }
                }         
                client.println(F("\n\t\t<input id='cBox0'  type='text' maxlength='5' size='3' value='PA0=")); if(dout[1])               client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));
                client.println(F("  \t\t<input id='cBox1'  type='text' maxlength='5' size='3' value='PA1=")); if(dout[2])               client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));
                client.println(F("  \t\t<input id='cBox2'  type='text' maxlength='5' size='3' value='PA2=")); if(dout[3])               client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));
                client.println(F("  \t\t<input id='cBox3'  type='text' maxlength='5' size='3' value='PA3=")); if(dout[4])               client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));client.println(F("<br />\n"));
                client.println(F("  \t\t<input id='cBox4'  type='text' maxlength='5' size='3' value='PB11="));if(digitalRead(SW1))      client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));
                client.println(F("  \t\t<input id='cBox5'  type='text' maxlength='5' size='3' value='PB10="));if(digitalRead(SW2))      client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));
                client.println(F("  \t\t<input id='cBox6'  type='text' maxlength='5' size='3' value='PB1=")); if(digitalRead(SW3))      client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));
                client.println(F("  \t\t<input id='cBox7'  type='text' maxlength='5' size='3' value='PB0=")); if(digitalRead(SW4))      client.print(F("F"));else client.print(F("T"));client.println(F("'></input>"));client.println(F("<br />\n"));
                client.print  (F("\t\t<div class='range'><input id='A0' max='4095' min='0' type='range' value="));client.print(ai[0]=analogRead(AI1));
                client.println(F(" /> <span id='value'>"));client.print(ai[0]);client.println(F("</span></div>\n"));
                client.print  (F("\t\t<div class='range'><input id='A1' max='4095' min='0' type='range' value="));client.print(ai[1]=analogRead(AI2));
                client.println(F(" /> <span id='value'>"));client.print(ai[1]);client.println(F("</span></div>\n"));
                client.print  (F("\t\t<div class='range'><input id='A2' max='4095' min='0' type='range' value="));client.print(ai[2]=analogRead(AI3));
                client.println(F(" /> <span id='value'>"));client.print(ai[2]);client.println(F("</span></div>\n"));
                client.print  (F("\t\t<div class='range'><input id='A3' max='4095' min='0' type='range' value="));client.print(ai[3]=analogRead(AI4));
                client.println(F(" /> <span id='value'>"));client.print(ai[3]);client.println(F("</span></div>\n"));
                if(!ErrorLight){
                        client.print  (F("\n\t\t<input id='tf2' type='text' size='5' value='"));client.print(htu.readTemperature());client.print(F("'>[deg]<BR>\n"));
                        client.print  (F("\n\t\t<input id='tf3' type='text' size='5' value='"));client.print(htu.readHumidity());client.print(F("'>[%]\t\t<BR>\n"));
                }
                if(!ErrorHTU){
                        client.print  (F("\n\t\t<input id='tf4' type='text' size='5' value='"));if(good) client.print(lux);else client.print(F("BAD"));client.print(F("'>[lux]\t\t<BR>\n"));
                }
        }
        HTTP_req = "";
}
//--------------------------
void printError(byte error){
        // If there's an I2C error, this function will
        // print out an explanation.
        Serial.print(F("I2C error: "));
        Serial.print(error,DEC);
        Serial.print(F(", "));
  
        switch(error){
                case 0:
                        Serial.println(F("success"));
                        break;
                case 1:
                        Serial.println(F("data too long for transmit buffer"));
                        break;
                case 2:
                        Serial.println(F("received NACK on address (disconnected?)"));
                        break;
                case 3:
                        Serial.println(F("received NACK on data"));
                        break;
                case 4:
                        Serial.println(F("other error"));
                        break;
                default:
                        Serial.println(F("unknown error"));
        }
}
//--------------------------
int LoopCount = 0;
void setup()
{
        Serial.begin(115200);                           // for diagnostics
        delay(1000);                                    //Start
        Serial.print(F("Start "));
        Serial.println(titleStr);
        SPI.begin();                                    //Initialize the SPI_1 port.
        SPI.setBitOrder(MSBFIRST);                      // Set the SPI_1 bit order
        SPI.setClockDivider(SPI_CLOCK_DIV4);            //SPI_CLOCK_DIV16 (72 / 16 = 4.5 MHz SPI_1 speed)
                                                        //SPI_CLOCK_DIV4 (72 / 4 = 18 MHz SPI_1 speed)
        Serial.print(F("SPI_Start\n"));
        pinMode(SPI1_NSS_PIN, OUTPUT);
        //Ethernet3API
        Ethernet.setCsPin(SPI1_NSS_PIN);
        Ethernet.setRstPin(W550io_Rst);
        //
        pinMode(W550io_Rst, OUTPUT);
        digitalWrite(W550io_Rst, LOW);
        delay(10);
        digitalWrite(W550io_Rst, HIGH);
        Serial.print(F("NIC_Reset\n"));
        
        LANSetup();
        Serial.print(F("LAN_setuped\n"));
        
        server.begin();
        Serial.print(F("WebServer_Start\n"));
        //I2CSlave
        Wire.begin();
        Serial.print(F("I2C_Start\n"));
        //ErrorHTUcheck
        Wire.beginTransmission(GY21ADRS);
        Serial.print(F("trans_Start\n"));
        ErrorHTU = Wire.endTransmission();
        Serial.println(ErrorHTU);
        if(!ErrorHTU){
                Serial.print(F("HTU_Start\n"));
                htu.begin();
        }
        Serial.print(F("htu_CheckEnd\n"));
        delay(100); 
        //ErrorLight
        Wire.beginTransmission(GY2561ADRS);
        Serial.print(F("trans_Start\n"));
        ErrorLight = Wire.endTransmission();
        Serial.println(ErrorLight);
        if(!ErrorLight){
                Serial.print(F("Light_Start\n"));
                light.begin();
        }
        Serial.print(F("light_CheckEnd\n"));
        
        unsigned char ID;
  
        if(!ErrorLight){
                if (light.getID(ID)){
                        //Serial.print(F("Got factory ID: 0X"));
                        //Serial.print(ID,HEX);
                        //Serial.println(F(", should be 0X5X"));
                }else{
                        // Most library commands will return true if communications was successful,
                        // and false if there was a problem. You can ignore this returned value,
                        // or check whether a command worked correctly and retrieve an error code:
                        byte error = light.getError();
                        printError(error);
                }
                gain = 0;
                unsigned char time = 2;
                light.setTiming(gain,time,ms);
                light.setPowerUp();
        }
        pinMode(LED,  OUTPUT);  //digitalWrite(LED, dout[0]);
        pinMode(LED1, OUTPUT);  digitalWrite(LED1,dout[1]);
        pinMode(LED2, OUTPUT);  digitalWrite(LED2,dout[2]);
        pinMode(LED3, OUTPUT);  digitalWrite(LED3,dout[3]);
        pinMode(LED4, OUTPUT);  digitalWrite(LED4,dout[4]);
        pinMode(SW1, INPUT_PULLUP);     din[0]  = digitalRead(SW1);
        pinMode(SW2, INPUT_PULLUP);     din[1]  = digitalRead(SW2);
        pinMode(SW3, INPUT_PULLUP);     din[2]  = digitalRead(SW3);
        pinMode(SW4, INPUT_PULLUP);     din[3]  = digitalRead(SW4);        
        ai[0]  = analogRead(AI1);
        ai[1]  = analogRead(AI2);
        ai[2]  = analogRead(AI3);
        ai[3]  = analogRead(AI4);
        Serial.print(F("IO_Setuped\n"));
        
        Serial.print(F("server is at "));Serial.println(Ethernet.localIP());
}
//--------------------------
void loop()
{
        if(!(LoopCount++ % 1000)){
        }        
        client = server.available();
        if( !client ){
                delay(1);
                return;
        }
        // HTTP\r\n\r\n
        boolean currentLineIsBlank = true;
        while( client.connected() ) {
                if( !client.available() ){
                        Serial.print(F("."));
                        continue;
                }
                char c = client.read();
                HTTP_req += c;
                //Serial.print(c);
                if( c == '\n'  && currentLineIsBlank ){  // HTTP
                        //Serial.print("RecieveLength=");Serial.println(HTTP_req.length());
                        //PrintResponse200(client);
                        if (HTTP_req.indexOf("/?")>0) {         //setup?/?
                                //Serial.print(F("CASE1:"));Serial.print(LoopCount);
                                if(LoopCount++ % 2){
                                        digitalWrite(PC13,HIGH);        //Serial.println(F(" H"));
                                }else{
                                        digitalWrite(PC13,LOW);         //Serial.println(F(" L"));
                                }
                                delay(100);
                                XMLhttpObjectAccess(client,HTTP_req);
                                break;
                        }else if(HTTP_req.indexOf("setup")>0){
                                //setup SBMEEPROM
                                //setup?SBM=1&DT1=0&DT2=8&DT3=DC&DT4=54&DT5=4D&DT6=D0&DT1=0&DT2=8&DT3=220&DT4=84&DT5=77&DT6=208&DT7=192&DT8=168&DT9=0&DT10=200&DT11=255&DT12=255&DT13=255&DT14=0&DT15=192&DT16=168&DT17=0&DT18=1
                                //TextFinderHTMLSTRING
                                if (HTTP_req.indexOf("SBM")>0){
                                        //Serial.println(F("Found_SBM"));
                                        int Code;
                                        //byte SET = finder.getValue();Serial.print(F("SET="));Serial.println(SET);
                                        int len = HTTP_req.length();
                                        char* HTTP_c = (char*)malloc(len+1);
                                        HTTP_req.toCharArray(HTTP_c, len);
                                        char bufff[10];
                                        //DT1 mac[0]            HEX
                                        int indexS = HTTP_req.indexOf("DT1=");
                                        int indexE = HTTP_req.indexOf("&",indexS);
                                        int k = 0;
                                        char c;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';//Serial.println(bufff);
                                                                        mac[0]=int(strtol(bufff, NULL, 16));//Serial.print(bufff);Serial.println(mac[0]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }
                                        }
                                        //DT2 mac[1]            HEX
                                        indexS = HTTP_req.indexOf("DT2=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                 if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';//Serial.println(bufff);
                                                                        mac[1]=int(strtol(bufff, NULL, 16));//Serial.print(bufff);Serial.println(mac[1]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                         }
                                        //DT3 mac[2]            HEX
                                        indexS = HTTP_req.indexOf("DT3=");      //Serial.print("Pos=");Serial.println(indexS);
                                        indexE = HTTP_req.indexOf("&",indexS);  //Serial.print("Pos=");Serial.println(indexE);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);//Serial.println(c);
                                                if (c == '='){
                                                        int m = indexE - indexS;        //Serial.print("m=");Serial.println(m);
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        mac[2]=int(strtol(bufff, NULL, 16));//Serial.print(bufff);Serial.println(mac[2]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                 }
                                        }
                                        //DT4 mac[3]            HEX
                                        indexS = HTTP_req.indexOf("DT4=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                 if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        mac[3]=int(strtol(bufff, NULL, 16));//Serial.print(bufff);Serial.println(mac[3]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }
                                        }
                                        //DT5 mac[4]            HEX
                                        indexS = HTTP_req.indexOf("DT5=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        mac[4]=int(strtol(bufff, NULL, 16));//Serial.print(bufff);Serial.println(mac[4]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT6 mac[5]            HEX
                                        indexS = HTTP_req.indexOf("DT6=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        mac[5]=int(strtol(bufff, NULL, 16));//Serial.print(bufff);Serial.println(mac[5]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT7 ip[0]             DEC
                                        indexS = HTTP_req.indexOf("DT7=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        ip[0]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(ip[0]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }
                                        }
                                        //DT8 ip[1]             DEC
                                        indexS = HTTP_req.indexOf("DT8=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        ip[1]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(ip[1]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }
                                        }
                                        //DT9 ip[2]             DEC
                                        indexS = HTTP_req.indexOf("DT9=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        ip[2]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(ip[2]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT10 ip[3]            DEC
                                        indexS = HTTP_req.indexOf("DT10=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        ip[3]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(ip[3]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }
                                        }
                                        //DT11 subnet[0]        DEC
                                        indexS = HTTP_req.indexOf("DT11=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                 if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        subnet[0]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(subnet[0]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT12 subnet[1]        DEC
                                        indexS = HTTP_req.indexOf("DT12=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        subnet[1]=int(strtol(bufff, NULL, 10)); //Serial.print(bufff);Serial.println(subnet[1]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT13 subnet[2]        DEC
                                        indexS = HTTP_req.indexOf("DT13=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j); //Serial.println(bufff);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        subnet[2]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(subnet[2]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT14 subnet[3]        DEC
                                        indexS = HTTP_req.indexOf("DT14=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                 if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';  //Serial.println(bufff);
                                                                        subnet[3]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(subnet[3]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT15 gateway[0]       DEC
                                        indexS = HTTP_req.indexOf("DT15=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';//Serial.println(bufff);
                                                                        gateway[0]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(gateway[0]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT16 gateway[1]       DEC
                                        indexS = HTTP_req.indexOf("DT16=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';//Serial.println(bufff);
                                                                        gateway[1]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(gateway[1]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT17 gateway[2]       DEC
                                        indexS = HTTP_req.indexOf("DT17=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';//Serial.println(bufff);
                                                                        gateway[2]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(gateway[2]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }        
                                        }
                                        //DT18 gateway[3]       DEC
                                        indexS = HTTP_req.indexOf("DT18=");
                                        indexE = HTTP_req.indexOf("&",indexS);
                                        k = 0;
                                        for(int j = indexS;j<=indexE;j++){
                                                c = HTTP_req.charAt(j);
                                                if (c == '='){
                                                        int m = indexE - indexS;
                                                        for (int l = 1; l < m;l++){
                                                                c = HTTP_req.charAt(j+l);
                                                                if(c == '&'){
                                                                        bufff[k]='\0';//Serial.println(bufff);
                                                                        gateway[3]=int(strtol(bufff, NULL, 10));//Serial.print(bufff);Serial.println(gateway[3]);
                                                                        break; 
                                                                }else{
                                                                        bufff[k++] = c;
                                                                }
                                                        }
                                                        break;
                                                }
                                        }

                                        // EEPROM
                                        for (int i = 0 ; i < 6; i++){
                                                EEPROM.write(i + 1,mac[i]);
                                        }
                                        for (int i = 0 ; i < 4; i++){
                                                EEPROM.write(i + 7, ip[i]);
                                        }
                                        for (int i = 0 ; i < 4; i++){
                                                EEPROM.write(i + 11, subnet[i]);
                                        }
                                        for (int i = 0 ; i < 4; i++){
                                                EEPROM.write(i + 15, gateway[i]);
                                        }
                                        // IDArduinoEEPROM
                                        EEPROM.write(0, EID); 
                                        //EEPROMarduino
                                        //
                                        int idcheck = EEPROM.read(0);
                                        if (idcheck == EID){
                                                //idID
                                                //
                                                //EERPOM
                                                for (int i = 0; i < 6; i++){
                                                        mac[i]          = EEPROM.read(i+1);
                                                }
                                                for (int i = 0; i < 4; i++){
                                                        ip[i]           = EEPROM.read(i+7);
                                                }
                                                for (int i = 0; i < 4; i++){
                                                        subnet[i]       = EEPROM.read(i+11);
                                                }
                                                for (int i = 0; i < 4; i++){
                                                        gateway[i]      = EEPROM.read(i+15);
                                                }
                                        }
                                        //
                                        HTTP_req = "";                     
                                }else{       
                                        Serial.println(F("SBMnotFound"));
                                }
                                //EEPROMarduino
                                //
                                PrintSetUpPage(client,HTTP_req);
                                break;
                        }else{
                                Serial.println(F("CASE4"));
                                PrintNormalPage(client,HTTP_req);
                                break;
                        }
                        break;                          // 
                }
                if(c ==  '\n'){                         // 
                        currentLineIsBlank = true;
                } else if(c !=  '\r' ){                 // 
                        currentLineIsBlank = false;
                }
        }
        // Web
        delay(1);
        
        // 
        client.stop();
}
//--------------------------EOF

setup画面のHTMLコードですが、参考としたコードは PROGMEM =”” を採用していました。この場合レタッチがとても大変です。
obj.println(F("")); で書いた方が制限が少なくて扱いやすいです。必ずF("")を遣う必要があります。そうしないとSRAMが足りなくなります。

Setupを実行したら必ずBluePillをリセットしてください。起動した時点で内容が更新されます。


オブジェクト自体はベクター殿のストレージをお借りしています。

https://www.vector.co.jp/vpack/browse/person/an051501.html



免責事項

本ソフトウエアは、あなたに対して何も保証しません。本ソフトウエアの関係者(他の利用者も含む)は、あなたに対して一切責任を負いません。
あなたが、本ソフトウエアを利用(コンパイル後の再利用など全てを含む)する場合は、自己責任で行う必要があります。

本ソフトウエアの著作権はToolsBoxに帰属します。
本ソフトウエアをご利用の結果生じた損害について、ToolsBoxは一切責任を負いません。
ToolsBoxはコンテンツとして提供する全ての文章、画像等について、内容の合法性・正確性・安全性等、において最善の注意をし、作成していますが、保証するものではありません。
ToolsBoxはリンクをしている外部サイトについては、何ら保証しません。
ToolsBoxは事前の予告無く、本ソフトウエアの開発・提供を中止する可能性があります。

商標・登録商標

Microsoft、Windows、WindowsNTは米国Microsoft Corporationの米国およびその他の国における登録商標です。
Windows Vista、Windows XPは、米国Microsoft Corporation.の商品名称です。
LabVIEW、National Instruments、NI、ni.comはNational Instrumentsの登録商標です。
I2Cは、NXP Semiconductors社の登録商標です。
その他の企業名ならびに製品名は、それぞれの会社の商標もしくは登録商標です。
すべての商標および登録商標は、それぞれの所有者に帰属します。