/* ===== pfod Command for Menu_3 ==== pfodApp msg {.} --> {,~Menu with Dwg`0~V1|+A~z} */ // Using ESP32 based board programmed via Arduino IDE // follow the steps given on http://www.forward.com.au/pfod/ESP32/index.html to install ESP32 support for Arduino IDE // NOTE: The latest versions of the ESP32 V2.0.0 - V2.0.5 Arduino board support have problems with WiFi connections // for the ESP32 chip (e.g. Sparkfun ESP32 Thing, tec). // It may take 3 or more re-connections to get a stable connection after reboot or reprogramming. // ESP32 BLE and Bluetoot are OK on V2.0.4 as is ESP32C3 WiFi (different chip). // For ESP8266 V3.0.2 works. // You need to modify the WLAN_SSID, WLAN_PASS settings below // to match your network settings /* Code generated by pfodDesignerV3 V3.0.4181 */ /* * (c)2014-2021 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 #include #define DEBUG // Download pfodESP32BufferedClient library from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodESP32BufferedClient.zip contains pfodESP32BufferedClient and pfodESP32Utils #include #include // download the libraries from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodDwgControls.zip V1.0+ contains pfodDwgControls.h includes DesignerSwitch.h #include #include int swap01(int); // method prototype for slider end swaps pfodSecurity parser("V2"); // create a parser with menu version string to handle the pfod messages pfodESP32BufferedClient 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 "b0Ux9akSiwKkwCtcnjTnpWp" 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 pfodDwgs dwgs(&parser); // drawing support DesignerSwitch switch_z('a',&dwgs); // an example switch for drawing z // Commands for drawing controls only need to unique per drawing, you can re-use cmd 'a' in another drawing. // the setup routine runs once on reset: void setup() { // Explicitly set the ESP32 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) #ifdef DEBUG Serial.begin(115200); Serial.println(); #endif /* Initialise wifi module */ if (*staticIP != '\0') { IPAddress ip(pfodESP32Utils::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); #ifdef DEBUG Serial.print("."); #endif } #ifdef DEBUG Serial.println(); Serial.println("WiFi connected"); #endif // Start the server server.begin(); #ifdef DEBUG Serial.println("Server started"); #endif // Print the IP address #ifdef DEBUG Serial.println(WiFi.localIP()); #endif // <<<<<<<<< Your extra setup code goes here switch_z.setLabel(F("Label")); } // 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 pressed menu item that loaded drawing with load cmd 'z' // in the main Menu of Menu_3 byte dwgCmd = parser.parseDwgCmd(); // parse rest of dwgCmd, return first char of active cmd // can be \0 if no dwg or no active areas defined // << Modify this code for your own controls if (switch_z.getCmd() == dwgCmd) { // check drawing control cmd to see what the user pressed in this drawing switch_z.setValue(!switch_z.getValue()); // modify this to take action based on this control sendDrawingUpdates_z(); // update the drawing } else { // no change parser.print(F("{}")); // always send back a pfod msg otherwise pfodApp will disconnect. } } else if ('z' == cmd) { // pfodApp is asking to load dwg 'z' if (!parser.isRefresh()) { // not refresh, send whole dwg parser.parseLong(pfodFirstArg,&pfodLongRtn); // parse first arg as a long sendDrawing_z(pfodLongRtn); } else { // refresh just update drawing state sendDrawingUpdates_z(); } } 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 menu background, format, prompt, refresh and version parser.print(F("~Menu with Dwg`0")); parser.sendVersion(); // send the menu version // send menu items parser.print(F("|+A~z")); parser.print(F("}")); // close pfod message } void sendMainMenuUpdate() { parser.print(F("{;")); // start an Update Menu pfod message // send menu items parser.print(F("|+A")); parser.print(F("}")); // close pfod message // ============ end of menu =========== } void sendDrawing_z(long dwgIdx) { if (dwgIdx == 0) { sendDrawing_z0(); } else if (dwgIdx == 1) { sendDrawing_z1(); // add more parts of the dwg here if needed } else { parser.print(F("{}")); // always return somthing } } void sendDrawing_z0() { dwgs.start(50, 30, dwgs.WHITE,true); //true means more to come, background defaults to WHITE if omitted i.e. dwgs.start(50,30); parser.sendRefreshAndVersion(0); // send the parser version to cache this image, no auto-refresh dwgs.pushZero(10, 15, 1.0f); // scale by 1.0f, scale defaults to 1.0f if omitted, i.e. pushZero(10,15); switch_z.draw(); // button drawn dwgs.popZero(); dwgs.end(); } void sendDrawing_z1() { dwgs.startUpdate(true); //true means more to come, // empty update so pfodApp will not ask for more dwgs.end(); } void sendDrawingUpdates_z() { dwgs.startUpdate(); // dwg refresh updates must be <1024 bytes, i.e. can only be 1 msg switch_z.update(); // update existing button with current state dwgs.end(); } int swap01(int in) { return (in==0)?1:0; } // ============= end generated code =========