スマホでコントロールできるロボットを作ろう!
from
https://pages.rohm.co.jp/ardiuno-smartphone-control-robot.html
code
https://pages.rohm.co.jp/ardiuno-smartphone-control-robot.html
code
#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> extern "C" { #include <user_interface.h> } #include <Ticker.h> //人感センサー用 #include <Adafruit_NeoPixel.h> //NeoPixel用 #ifdef __AVR__ #include <avr/power.h> #endif #include <Wire.h> //I2Cライブラリのインクルード #define ADDR 0x64 #include <DHT.h> //温湿度センサー・ライブラリ #define DHTTYPE DHT11 // 温湿度センサーのタイプ (DHT11, DHT22) #define DHTPIN 2 //温湿度センサー用のピン // 自宅のWifi設定 const char *ssid = "elecom2g-0507d0"; const char *password = "4858634471451"; ESP8266WebServer server(80); const int G0 = 0; //テスト用のピン const int pixel = 4; //Neo Pixel用のピン DHT dht(DHTPIN, DHTTYPE); float t = 0 ; //気温計測用 float h = 0 ; //湿度計測用 String led_status = "Rainbow"; //Neo Pixelの初期カラー #define command_start 0 //モーター用コマンド(スタート) #define command_stop 1 //モーター用コマンド(ストップ) #define command_back 2 //モーター用コマンド(バック) char state = command_stop; //モーター・コマンドの初期値(ストップ) Ticker ticker; // ロボット操作用ウェブページの作成 String getPage(){ String page = "<html lang=ja><head><meta http-equiv='refresh' content='30' charset='UTF-8' content='width=200' />"; page += "<title>ESProbot</title>"; page += "<style> body { background-color: #fffff; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>"; page += "</head><body><h1>ESProbot</h1>"; page += "<h3>ロボット操作</h3>"; page += "<form action=start><input type=submit value='前進' target=tif></form>"; page += "<form action=stop><input type=submit value='ストップ' target=tif></form>"; page += "<form action=back><input type=submit value='後退' target=tif></form>"; page += "<h3>LED色変更</h3>"; //page += "<form action='/' method='POST'>"; page += "<ul><li>LED色: "; page += led_status; page += "<form action='/'><input name='LED' type=submit value='Rainbow' target=tif></form>"; page += "<form action='/'><input name='LED' type=submit value='Red' target=tif></form>"; page += "<form action='/'><input name='LED' type=submit value='Green' target=tif></form>"; page += "<form action='/'><input name='LED' type=submit value='Blue' target=tif></form>"; page += "</li></ul>"; /*page += "<br><INPUT type='radio' name='LED' value='red'>Red "; page += "<INPUT type='radio' name='LED' value='green'>Green "; page += "<INPUT type='radio' name='LED' value='blue'>Blue</li></ul>"; page += "<INPUT type='submit' value='色変更'>";*/ page += "<h3>室内計測</h3>"; page += "<ul><li>気温: "; page += String(t,0); page += " ℃</li>"; page += "<li>湿度: "; page += String(h,0); page += " %</li></ul>"; page += "<iframe src='javascript:false' style='display:none' name='tif' id='tif'></iframe>"; page += "</body></html>"; return page; } Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, pixel, NEO_GRB + NEO_KHZ800); // Fill the dots one after the other with a color void colorWipe(uint32_t c, uint8_t wait) { for(uint16_t i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(wait); } } uint32_t Wheel(byte WheelPos) { WheelPos = 255 - WheelPos; if(WheelPos < 85) { return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if(WheelPos < 170) { WheelPos -= 85; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } // Slightly different, this makes the rainbow equally distributed throughout void rainbowCycle(uint8_t wait) { uint16_t i, j; for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel for(i=0; i< strip.numPixels(); i++) { strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); } strip.show(); delay(wait); } } void rainbow(uint8_t wait) { uint16_t i, j; for(j=0; j<256; j++) { for(i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, Wheel((i+j) & 255)); } strip.show(); delay(wait); } } //Theatre-style crawling lights. void theaterChase(uint32_t c, uint8_t wait) { for (int j=0; j<10; j++) { //do 10 cycles of chasing for (int q=0; q < 3; q++) { for (uint16_t i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, c); //turn every third pixel on } strip.show(); delay(wait); for (uint16_t i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, 0); //turn every third pixel off } } } } //Theatre-style crawling lights with rainbow effect void theaterChaseRainbow(uint8_t wait) { for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel for (int q=0; q < 3; q++) { for (uint16_t i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on } strip.show(); delay(wait); for (uint16_t i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, 0); //turn every third pixel off } } } } void handleSubmit() { String LEDColor; LEDColor = server.arg("LED"); Serial.println("Set Color "); Serial.print(LEDColor); if ( LEDColor == "Red" ) { colorWipe(strip.Color(255, 0, 0), 50); // Red led_status = "Red"; server.send ( 200, "text/html", getPage() ); } else if ( LEDColor == "Green" ) { colorWipe(strip.Color(0, 255, 0), 50); // Green led_status = "Green"; server.send ( 200, "text/html", getPage() ); } else if ( LEDColor == "Blue" ) { colorWipe(strip.Color(0, 0, 255), 50); // Blue led_status = "Blue"; server.send ( 200, "text/html", getPage() ); } else if ( LEDColor == "Rainbow" ) { rainbow(20); // Rainbow led_status = "Rainbow"; server.send ( 200, "text/html", getPage() ); } else { Serial.println("Error"); } } void handleRoot(){ if ( server.hasArg("LED") ) { handleSubmit(); } else { server.send ( 200, "text/html", getPage() ); } } void stop_motor(){ Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x18); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x1B); Wire.endTransmission(); } void start_motor(){ Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x19); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x35); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x65); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0xC9); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0xFD); Wire.endTransmission(); } void reverse_motor(){ Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x1A); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x36); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0x66); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0xCA); Wire.endTransmission(); delay(10); Wire.beginTransmission(ADDR); Wire.write(0x00); Wire.write(0xFE); Wire.endTransmission(); } void handle_stop() { Serial.print("stop"); if((state == command_start)||(state == command_back)) { stop_motor(); state = command_stop; } server.send(200, "text/html", getPage() ); } void handle_start() { Serial.print("start"); if(state == command_back){ stop_motor(); delay(10); start_motor(); }else if(state == command_stop){ start_motor(); delay(3000); stop_motor(); } state = command_start; server.send(200, "text/html", getPage() ); } void handle_back() { Serial.print("back"); if(state == command_start){ stop_motor(); delay(10); reverse_motor(); }else if(state == command_stop){ reverse_motor(); delay(3000); stop_motor(); } state = command_back; server.send(200, "text/html", getPage() ); } void LED_ON() { Serial.print("LED ON"); digitalWrite(G0, 1); server.send(200, "text/html", getPage() ); } void LED_OFF() { Serial.print("LED OFF"); digitalWrite(G0, 0); server.send(200, "text/html", getPage() ); } //人感センサー制御 void timer() { // A/Dセンサー取得 int tout = system_adc_read(); Serial.println(tout); /* if (tout < 500) { colorWipe(strip.Color(0, 255, 0), 50); // Green } */ digitalWrite(G0, tout < 500 ? LOW : HIGH); } void setup() { delay(1000); Serial.begin(115200); Serial.println(); Serial.print("Configuring access point..."); pinMode(G0, OUTPUT); //テスト用 pinMode(pixel, OUTPUT); //NeoPixel用 ticker.attach_ms(500, timer); //人感センサー用 digitalWrite(G0, 0); // Connect to WiFi network WiFi.begin(ssid, password); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Wire.begin(5, 14); delay(40); server.on("/", handleRoot); // And as regular external functions: server.on("/stop", handle_stop); server.on("/start", handle_start); server.on("/back", handle_back); /*server.on("/led_on", LED_ON); server.on("/led_off", LED_OFF);*/ server.begin(); Serial.println("HTTP server started"); strip.begin(); strip.show(); // Initialize all pixels to 'off' } void loop() { server.handleClient(); t = dht.readTemperature(); h = dht.readHumidity(); Serial.print("Temp: "); Serial.print(t); Serial.println(" C"); Serial.print("Humid: "); Serial.print(h); Serial.println(" %"); delay(100); if (led_status == "Rainbow") { rainbow(20); } else if ( digitalRead(G0) == 1) { theaterChase(strip.Color(127, 127, 0), 50); // Blink Yellow delay(100); } else if (t > 24) { theaterChase(strip.Color(127, 0, 0), 50); // Blink Red delay(100); } else if (h < 30) { theaterChase(strip.Color(0, 0, 127), 50); // Blink Blue delay(100); } else { colorWipe(strip.Color(0, 0, 0), 50); delay(100); } }
留言
張貼留言