/* ===== pfod Command for ESP8266 OLIMEX ==== pfodApp msg {.} --> {.<+6>OLIMEX Relay Control|A~<+6>Relay is `0~~Off\On} */ // Using ESP8266 based board programmed via Arduino IDE // follow the steps given on https://github.com/esp8266/arduino under Installing With Boards Manager // You need to modify the WLAN_SSID, WLAN_PASS settings below // to match your network settings /* Code generated by pfodDesignerV3 V3.0.3861 */ /* (c)2014-2020 Forward Computing and Control Pty. Ltd. NSW Australia, www.forward.com.au This code is not warranted to be fit for any purpose. You may only use it at your own risk. This generated code may be freely used for both private and commercial use provided this copyright is maintained. */ #include // Download pfodESP8266BufferedClient library from http://www.forward.com.au/pfod/pfodParserLibraries/index.html #include #include #include // Download pfodParser library from http://www.forward.com.au/pfod/pfodParserLibraries/index.html #include // V2.31 or higher // download the libraries from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodParser.zip V3.48+ contains pfodParser, pfodSecurity, pfodDelay, pfodBLEBufferedSerial, pfodSMS and pfodRadio int swap01(int); // method prototype for slider end swaps pfodSecurity parser("V4"); // create a parser with menu version string to handle the pfod messages pfodESP8266BufferedClient bufferedClient; #define WLAN_SSID "myNetwork" // cannot be longer than 32 characters! #define WLAN_PASS "myPassword" const int portNo = 4989; // What TCP port to listen on for connections. const char staticIP[] = ""; // set this the static IP you want, e.g. "10.1.1.200" or leave it as "" for DHCP. DHCP is not recommended. // add your pfod Password here for 128bit security // eg "173057F7A706AF9BBE65D51122A14CEE" but generate your own key, "" means no pfod password #define pfodSecurityCode "" // 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; WiFiClient* clientPtr; // non-null if have connected client unsigned long plot_mSOffset = 0; // set by {@} response bool clearPlot = false; // set by the {@} response code // give the board pins names, if you change the pin number here you will change the pin controlled int cmd_A_var; // name the variable for 'Relay is ' const int cmd_A_pin = 5; // name the output pin for 'Relay is ' // the setup routine runs once on reset: void setup() { // Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, // would try to act as both a client and an access-point and could cause // network-issues with your other WiFi-devices on your WiFi-network. WiFi.mode(WIFI_STA); EEPROM.begin(512); // only use 20bytes for pfodSecurity but reserve 512 (pfodWifiConfig uses more) cmd_A_var = 0; //pinMode(cmd_A_pin, INPUT_PULLUP); pinMode(cmd_A_pin, OUTPUT); // output for 'Relay is ' is initially LOW, //uncomment INPUT_PULLUP line above and set variable to 1, if you want it initially HIGH digitalWrite(cmd_A_pin, cmd_A_var); // set output Serial.begin(115200); for (int i = 10; i > 0; i--) { Serial.print(i); Serial.print(' '); delay(500); } Serial.println(); /* Initialise wifi module */ if (*staticIP != '\0') { IPAddress ip(pfodESP8266Utils::ipStrToNum(staticIP)); 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); } WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); // <<<<<<<<< Your extra setup code goes here } // the loop routine runs over and over again forever: void loop() { if (clientPtr && (!clientPtr->connected())) { closeConnection(parser.getPfodAppStream()); } if (server.hasClient()) { // new connection if (!clientPtr) { // no existing connection client = server.available(); // get any new client clientPtr = &client; // have connected client parser.connect(bufferedClient.connect(clientPtr), F(pfodSecurityCode)); // sets new io stream to read from and write to EEPROM.commit(); // does nothing if nothing to do } else { // already have client so just stop the new one WiFiClient newClient = server.available(); // get any new client newClient.stop(); } } if (clientPtr) { uint8_t cmd = parser.parse(); // parse incoming data from connection // parser returns non-zero when a pfod command is fully parsed if (cmd != 0) { // have parsed a complete msg { to } uint8_t* pfodFirstArg = parser.getFirstArg(); // may point to \0 if no arguments in this msg. pfod_MAYBE_UNUSED(pfodFirstArg); // may not be used, just suppress warning long pfodLongRtn; // used for parsing long return arguments, if any pfod_MAYBE_UNUSED(pfodLongRtn); // may not be used, just suppress warning if ('.' == cmd) { // pfodApp has connected and sent {.} , it is asking for the main menu if (!parser.isRefresh()) { sendMainMenu(); // send back the menu designed } else { sendMainMenuUpdate(); // menu is cached just send update } // handle {@} request } else if ('@' == cmd) { // pfodApp requested 'current' time plot_mSOffset = millis(); // capture current millis as offset rawdata timestamps clearPlot = true; // clear plot on reconnect as have new plot_mSOffset parser.print(F("{@`0}")); // return `0 as 'current' raw data milliseconds // now handle commands returned from button/sliders } else if ('A' == cmd) { // user moved slider -- 'Relay is ' // in the main Menu of ESP8266 OLIMEX // set output based on slider 0 == LOW, 1 == HIGH parser.parseLong(pfodFirstArg, &pfodLongRtn); // parse first arg as a long cmd_A_var = (int)pfodLongRtn; // set variable digitalWrite(cmd_A_pin, cmd_A_var); // set output sendMainMenuUpdate(); // 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 } void closeConnection(Stream *io) { (void)(io); // unused // add any special code here to force connection to be dropped parser.closeConnection(); // nulls io stream bufferedClient.stop(); // clears client reference clientPtr->stop(); clientPtr = NULL; } void sendMainMenu() { // !! Remember to change the parser version string // every time you edit this method parser.print(F("{.")); // start a Menu screen pfod message send_menuContents(); // send the menu contents for ESP8266 OLIMEX parser.print(F("}")); // close pfod message } void sendMainMenuUpdate() { parser.print(F("{:")); // start an Update Menu pfod message send_menuContents(); // send the menu contents for ESP8266 OLIMEX 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 parser.print(F("<+6>OLIMEX\nRelay Control")); // send menu items parser.print(F("|A~<+6>Relay is `")); parser.print(cmd_A_var); // output the current state 0 Low or 1 High parser.print(F("~~Off\\On")); // Note the \\ inside the "'s to send \ // ============ end of menu =========== } int swap01(int in) { return (in == 0) ? 1 : 0; } // ============= end generated code =========