//******************************************************************// // ESP32 Send IR DATA automatically // 2020.6.7 // ESP32_IRsend06_1.ino // Send data = Sellect Table DATA by tapping accelerometer randomly // ADXL345(I2C) accelerometer //******************************************************************// //******************************************************************// #include // Replace with your network credentials const char* ssid = "fablabkamakura3_guest"; const char* password = "kamakura_1192"; // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; // Decode HTTP GET value String ptmString = String(5); int pos1 = 0; int pos2 = 0; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; const int rmtDataLength = 34; // NEC format data length 34 bit rmt_item32_t rmtData[rmtDataLength]; // data to send const rmt_channel_t channel = RMT_CHANNEL_0; const gpio_num_t irPin = GPIO_NUM_25; //******************************************************************// //****************************************// const int leaderOnUs = 9000; const int leaderOffUs = 4500; const int dataOnUs = 560; const int data1OffUs = 1690; const int data0OffUs = 560; const int stopbitOnUs = 560; const int stopbitOffUs = 0x7fff; //*************************************************************// // LED Brink Pattern Table definition //*************************************************************// /* LED pattern variable */ int LEDpatNo = 0; //LED pattern int LEDpatCnt = 0; //LED data counter int actionmode = 0;//インプットデバイスを使って色を変える int singletap = 0;//setupでもloopの中でも使えるように int Randomcounter = 0;//何回に一回ランダム表示を切り替えるか /* Define 色 Code */ #define K0 0x80 #define K1 0x81 #define K2 0x82 #define K3 0x83 #define K4 0x84 #define K5 0x85 #define K6 0x86 #define K7 0x87 /* Define 明るさ Code */ #define B0 0x00 #define B1 0x40 #define B2 0x80 #define B3 0xC0 /* define LED pattern */ uint8_t LED0[] = { 1, K0 }; // --- uint8_t LED1[] = { 1, K1 }; // --B uint8_t LED2[] = { 1, K2 }; // -G- uint8_t LED3[] = { 1, K3 }; // -GB uint8_t LED4[] = { 1, K4 }; // R-- uint8_t LED5[] = { 1, K5 }; // R-B uint8_t LED6[] = { 1, K6 }; // RG- uint8_t LED7[] = { 1, K7 }; // RGB uint8_t *LEDTBL[] = { LED0, LED1, LED2, LED3, LED4, LED5, LED6, LED7 }; //*** End of LED Brink Pattern Table definition ****************************// #include #include "driver/rmt.h" /* send NEC format remote control data */ void sendData(uint16_t customCode, uint8_t dataCode) { /* leader code 1bit: ON 9000us, OFF 4500us */ rmtData[0].duration0 = leaderOnUs; rmtData[0].level0 = 1; rmtData[0].duration1 = leaderOffUs; rmtData[0].level1 = 0; /* * custom code 16 bit * INPUT series: b15 b14 b13 b12 b11 b10 b09 b08 b07 b06 b05 b04 b03 b02 b01 b00 * SEND series: b08 b09 b10 b11 b12 b13 b14 b15 b00 b01 b02 b03 b04 b05 b06 b07 */ for (int i = 0; i < 2; i++) { for (int j = 0; j < 8; j++) { /* * 1: ON 560us + OFF 1690us * 0: ON 560us + OFF 560us */ rmtData[8 * i + j + 1].duration0 = dataOnUs; rmtData[8 * i + j + 1].level0 = 1; if (customCode & (1 << ((1 - i) * 8 + j))) { rmtData[8 * i + j + 1].duration1 = data1OffUs; } else { rmtData[8 * i + j + 1].duration1 = data0OffUs; } rmtData[8 * i + j + 1].level1 = 0; } } /* * data code 8bit * INPUT series: b7 b6 b5 b4 b3 b2 b1 b0 * SEND series: b0 b1 b2 b3 b4 b5 b6 b7 ~b0 ~b1 ~b2 ~b3 ~b4 ~b5 ~b6 ~b7 */ for (int i = 0; i < 8; i++) { rmtData[i + 17].duration0 = dataOnUs; rmtData[i + 25].duration0 = dataOnUs; rmtData[i + 17].level0 = 1; rmtData[i + 25].level0 = 1; if (dataCode & (1 << i)) { rmtData[i + 17].duration1 = data1OffUs; rmtData[i + 25].duration1 = data0OffUs; } else { rmtData[i + 17].duration1 = data0OffUs; rmtData[i + 25].duration1 = data1OffUs; } rmtData[i + 17].level1 = 0; rmtData[i + 25].level1 = 0; } /* stop bit 1bit: ON 560 */ rmtData[33].duration0 = stopbitOnUs; rmtData[33].level0 = 1; rmtData[33].duration1 = stopbitOffUs; rmtData[33].level1 = 0; rmt_write_items(channel, rmtData, rmtDataLength, true); } //*************************************************// // Print 8bit data in HEX to console //*************************************************// void hexprint(uint8_t data){ char HEXTBL[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; Serial.print(HEXTBL[ (data & 0xF0) >> 4 ]); Serial.print(HEXTBL[ (data & 0x0F)]); } //*************************************************// // MAIN PROCESS //*************************************************// void setup() { rmt_config_t rmtConfig; rmtConfig.rmt_mode = RMT_MODE_TX; // transmit mode rmtConfig.channel = channel; // channel to use 0 - 7 rmtConfig.clk_div = 80; // clock divider 1 - 255. source clock is 80MHz -> 80MHz/80 = 1MHz -> 1 tick = 1 us rmtConfig.gpio_num = irPin; // pin to use rmtConfig.mem_block_num = 1; // memory block size rmtConfig.tx_config.loop_en = 0; // no loop rmtConfig.tx_config.carrier_freq_hz = 38000; // IR remote controller uses 38kHz carrier frequency rmtConfig.tx_config.carrier_duty_percent = 33; // duty rmtConfig.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH; // carrier level rmtConfig.tx_config.carrier_en = 1; // carrier enable rmtConfig.tx_config.idle_level = RMT_IDLE_LEVEL_LOW ; // signal level at idle rmtConfig.tx_config.idle_output_en = 1; // output if idle rmt_config(&rmtConfig); rmt_driver_install(rmtConfig.channel, 0, 0); //******************************************************************// Serial.begin(115200); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); //******************************************************************// } void loop() { //表示 /* Send LED data sequncially in the LED Pattern Table */ uint8_t *p; // Define KeyPatternAddress pointer p = LEDTBL[LEDpatNo]; // Set pointer address int i = *p; // Get pattern length uint8_t kdata = *(p + LEDpatCnt + 1); // get Keydata sendData(0x00ff, kdata); hexprint(kdata); Serial.print(" "); LEDpatCnt++; if ( LEDpatCnt > i-1 ) { LEDpatCnt = 0; Serial.println(); } // delay(30); //******************************************************************// WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, currentTime = millis(); previousTime = currentTime; Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // Display the HTML web page client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); // Web Page client.println("

ESP32 with Neopixel

"); client.println("

Position:

"); client.println(""); client.println(""); client.println(""); //GET /?value=180& HTTP/1.1 if(header.indexOf("GET /?value=")>=0) { pos1 = header.indexOf('='); pos2 = header.indexOf('&'); ptmString = header.substring(pos1+1, pos2); //Rotate the servo Potmode(ptmString.toInt()); Serial.println(ptmString); } // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } //******************************************************************// } void Potmode(int analogdata1){ //ポテンショメータで制御するプログラム if(analogdata1<500){ LEDpatNo = 0; } else if(analogdata1<1000){ LEDpatNo = 1; } else if(analogdata1<1500){ LEDpatNo = 2; } else if(analogdata1<2000){ LEDpatNo = 3; } else if(analogdata1<2500){ LEDpatNo = 4; } else if(analogdata1<3000){ LEDpatNo = 5; } else if(analogdata1<3500){ LEDpatNo = 6; } else{ LEDpatNo = 7; } LEDpatCnt = 0; }