/* ===== pfod Command for Garage Door Angle ==== */ // Using OLIMEX_ESP8266_EVB board // You need to modify the WLAN_SSID, WLAN_PASS settings below // to match your network settings /* Code generated by pfodDesigner V1.2.781 * (c)2014-2015 Forward Computing and Control Pty. Ltd. * NSW Australia, www.forward.com.au * This generated code may be freely used for both private and commerical use */ #include // Download library from http://www.forward.com.au/pfod/pfodParserLibraries/index.html #include #include pfodSecurity parser; // create a parser to handle the pfod messages #define WLAN_SSID "networkSSID" // cannot be longer than 32 characters! #define WLAN_PASS "networkPassword" const int portNo = 4989; // What TCP port to listen on for connections. // add you 32 char Hex pfod Password here for 128bit security // eg "173057F7A706AF9BBE65D51122A14CEE" but generate your own key, "" means no pfod password #define pfodSecurityCode "" // no security for this testing // see http://www.forward.com.au/pfod/ArduinoWiFi_simple_pfodDevice/index.html for more information and an example // and QR image key generator. WiFiServer server(portNo); WiFiClient client; boolean alreadyConnected = false; // whether or not the client was connected previously unsigned long currentTime = 0; const unsigned long ADC_INTERVAL = 10; // 10mS unsigned long ADCtimerStart = 0; const unsigned long PLOT_INTERVAL = 100; // 100mS unsigned long plotTimerStart = 0; const int ADC_PIN = 17; // PIN for adc const int FILTER_SIZE_POWER = 5; const size_t FILTER_SIZE = (1< 0; i--) { Serial.print(i); Serial.print(' '); delay(500); } Serial.println(); /* Initialise wifi module */ WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); // if (*staticIP != '\0') { // config static IP IPAddress ip(10,1,1,110); IPAddress gateway(ip[0], ip[1], ip[2], 1); // set gatway to ... 1 #ifdef DEBUG Serial.print(F("Setting gateway to: ")); Serial.println(gateway); #endif IPAddress subnet(255, 255, 255, 0); WiFi.config(ip, gateway, subnet); // } // else leave as DHCP // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); // start listening for clients // server.begin(); // initialize client client = server.available(); // evaluates to false if no connection // <<<<<<<<< Your extra setup code goes here // initializeFilter(); currentTime = millis(); ADCtimerStart = currentTime; plotTimerStart = currentTime; } void initializeFilter() { for (size_t i=0; i<=FILTER_SIZE; i++) { filter[i] = 0; } filterSum = 0; filterIdx = 0; filteredValue = 0; } int filterReading(int adcReading) { filteredValue = adcReading; // get current reading in this idx int idxReading = filter[filterIdx]; // update sum filterSum += adcReading; filterSum -= idxReading; filter[filterIdx] = adcReading; filterIdx++; filterIdx &= FILTER_SIZE; // loop if needed filteredValue = filterSum >> FILTER_SIZE_POWER; // divide by filter size a power of 2 // limit to 1023 filteredValue &= 1023; return filteredValue; } // the loop routine runs over and over again forever: void loop() { if (!client) { // see if a client is available client = server.available(); // evaluates to false if no connection } else { // have client if (!client.connected()) { if (alreadyConnected) { // client closed so clean up closeConnection(parser.getPfodAppStream()); } } else { // have connected client if (!alreadyConnected) { parser.connect(&client, F(pfodSecurityCode)); // sets new io stream to read from and write to EEPROM.commit(); // does nothing if nothing to do alreadyConnected = true; } byte cmd = parser.parse(); // pass it to the parser // parser returns non-zero when a pfod command is fully parsed if (cmd != 0) { // have parsed a complete msg { to } byte* pfodFirstArg = parser.getFirstArg(); // may point to \0 if no arguments in this msg. long pfodLongRtn; // used for parsing long return arguments, if any if ('.' == cmd) { // pfodApp has connected and sent {.} , it is asking for the main menu // send back the menu designed sendMainMenu(); // now handle commands returned from button/sliders } else if ('A' == cmd) { // user pressed -- 'Button' // in the main Menu of Garage Door Angle // << add your action code here for this button parser.print("{=Garage Door Angle|time(mS)|Angle ADC measurement}"); // always send back a pfod msg otherwise pfodApp will disconnect. } else if ('!' == cmd) { // CloseConnection command closeConnection(parser.getPfodAppStream()); } else { // unknown command parser.print(F("{}")); // always send back a pfod msg otherwise pfodApp will disconnect. } } } } // <<<<<<<<<<< Your other loop() code goes here currentTime = millis(); if ((currentTime - ADCtimerStart) > ADC_INTERVAL) { ADCtimerStart = currentTime; int i = analogRead(17); delay(0); int filteredADC = filterReading(i); } if ((currentTime - plotTimerStart) > PLOT_INTERVAL) { plotTimerStart = currentTime; if (alreadyConnected) { parser.print(currentTime); parser.print(","); parser.println(filteredValue); } } } void closeConnection(Stream *io) { // add any special code here to force connection to be dropped parser.closeConnection(); // nulls io stream alreadyConnected = false; if (!client) { return; } // else client.stop(); client = server.available(); // evaluates to false if no connection } void sendMainMenu() { parser.print(F("{.")); // start a Menu screen pfod message send_menuContents(); // send the menu contents for Garage Door Angle parser.print(F("}")); // close pfod message } void sendMainMenuUpdate() { parser.print(F("{:")); // start an Update Menu pfod message send_menuContents(); // send the menu contents for Garage Door Angle parser.print(F("}")); // close pfod message } // modify this method if you need to update the menu to reflect state changes void send_menuContents() { // send menu prompt // ")); // send menu items parser.print(F(" | A~Plot of Angle")); // ============ end of menu item =========== } // ============= end generated code =========