/* This sketch is a Movuino firmware. It allows the Movuino to send data on a specific Wifi through an OSC protocol. (Open Sound Control) */ #include #include #include #include "Wire.h" #include "I2Cdev.h" #include "MPU6050.h" #include #include #include #include #include // Set your wifi network configuration here const char * ssid = "magique"; // your network SSID (name of the wifi network) const char * pass = "idbeholdl"; // your network password const char * hostIP = "192.168.43.117"; // IP address of the host computer const char * hisIP = "192.168.43.117"; String hisIp = "test"; const unsigned int port = 7400; // port on which data are sent (send OSC message) MPU6050 accelgyro; ESP8266WiFiMulti WiFiMulti; //WiFiClient client; WiFiServer server(80); int packetNumber = 0; int16_t ax, ay, az; // store accelerometre values int16_t gx, gy, gz; // store gyroscope values int16_t mx, my, mz; // store magneto values // Button variables const int pinBtn = 13; // the number of the pushbutton pin boolean isBtn = 0; // variable for reading the pushbutton status float pressTime = 1000; // pressure time needed to switch Movuino state float lastButtonTime; boolean lockPress = false; // avoid several activation on same button pressure boolean isWifi; // LEDs const int pinLedWifi = 2; // wifi led indicator const int pinLedBat = 0; // battery led indicator WiFiUDP Udp; void setup() { // pin setup pinMode(pinBtn, INPUT_PULLUP); // pin for the button pinMode(pinLedWifi, OUTPUT); // pin for the wifi led pinMode(pinLedBat, OUTPUT); // pin for the battery led Wire.begin(); Serial.begin(115200); delay(10); // initialize device Serial.println("Initializing I2C devices..."); accelgyro.initialize(); // We start by connecting to a WiFi network WiFiMulti.addAP(ssid, pass); Serial.println(); Serial.println(); Serial.print("Wait for WiFi... "); // wait while connecting to wifi ... while (WiFiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(500); } // Movuino is now connected to Wifi Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println("IP address: "); Serial.println(WiFi.localIP()); delay(50); Udp.begin(port); Udp.parsePacket(); delay(50); IPAddress myIp = WiFi.localIP(); hisIp = String(Udp.remoteIP()); char hisIP[hisIp.length()+1]; // tableau de char de la taille du String param+1 (caractère de fin de ligne) hisIp.toCharArray(hisIP,hisIp.length()+1); // récupère le param dans le tableau de char isWifi = true; } void loop() { // BUTTON CHECK /* isBtn = digitalRead(pinBtn); // check if button is pressed if (isBtn) { lastButtonTime = millis(); lockPress = false; } else { if (millis() - lastButtonTime > pressTime && !lockPress) { lockPress = true; // avoid several activation with same pressure isWifi = !isWifi; // switch state lastButtonTime = millis(); if (!isWifi) { shutDownWifi(); } else { awakeWifi(); } } }*/ // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } // Read the first line of the request String req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request int val; if (req.indexOf("/sensor/0") != -1) val = 0; else if (req.indexOf("/sensor/1") != -1) val = 1; else { Serial.println("invalid request"); client.stop(); return; } // Set GPIO2 according to the request digitalWrite(1, val); client.flush(); // Prepare the response if (val == 1) { String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nAccelero and Gyro now
\r\n"; s += (val)?String(ax / float(32768))+";"+String(ay / float(32768))+";"+String(az / float(32768))+"
\r\n"+String(gx / float(32768))+";"+String(gy / float(32768))+";"+String(gz / float(32768))+"
\r\n":99999; s += "\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Client disconnected"); } if (val == 0) { // MOVUINO DATA if (WiFi.status() == WL_CONNECTED) { IPAddress myIp = WiFi.localIP(); // GET MOVUINO DATA //accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz); // Get all 9 axis data (acc + gyro + magneto) //---- OR -----// accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Get only axis from acc & gyr delay(5); printMovuinoData(); // optionnal // SEND MOVUINO DATA OSCMessage msg("/movuinOSC"); // create an OSC message on address "/movuinOSC" msg.add(ax / float(32768)); // add acceleration X data as message msg.add(ay / float(32768)); // add acceleration Y data msg.add(az / float(32768)); // add ... msg.add(gx / float(32768)); msg.add(gy / float(32768)); msg.add(gz / float(32768)); // you can add as many data as you want Udp.beginPacket(hisIP, port); // send message to computer target with "client Ip" on "port" msg.send(Udp); Udp.endPacket(); msg.empty(); delay(5); } else { delay(200); // wait more if Movuino is sleeping } } } void shutDownWifi() { WiFi.mode(WIFI_OFF); WiFi.forceSleepBegin(); delay(1); // needed digitalWrite(pinLedWifi, HIGH); // turn OFF wifi led digitalWrite(pinLedBat, HIGH); // turn OFF battery led Serial.println("OFF"); } void awakeWifi() { // Awake wifi and re-connect Movuino WiFi.forceSleepWake(); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); digitalWrite(pinLedBat, LOW); // turn ON wifi led //Blink wifi led while wifi is connecting while (WiFiMulti.run() != WL_CONNECTED) { digitalWrite(pinLedWifi, LOW); delay(200); digitalWrite(pinLedWifi, HIGH); delay(200); } digitalWrite(pinLedWifi, LOW); Serial.println("ON"); } void printMovuinoData() { Serial.print(ax / float(32768)); Serial.print("\t "); Serial.print(ay / float(32768)); Serial.print("\t "); Serial.print(az / float(32768)); Serial.print("\t "); Serial.print(gx / float(32768)); Serial.print("\t "); Serial.print(gy / float(32768)); Serial.print("\t "); Serial.print(gz / float(32768)); Serial.println(""); } void WebMovuinoData() { Serial.print(ax / float(32768)); Serial.print("\t "); Serial.print(ay / float(32768)); Serial.print("\t "); Serial.print(az / float(32768)); Serial.print("\t "); Serial.print(gx / float(32768)); Serial.print("\t "); Serial.print(gy / float(32768)); Serial.print("\t "); Serial.print(gz / float(32768)); Serial.println(""); }