Hi All,
I am attempting to control a switch using the pfodApp developer and application. The goal of my device is to trigger pump using a android app with all devices on my local network supplied by an internet hub. I have created my arduino code using the pfodDesigner tool which has been successfully uploaded onto my Yun. The issue I am having is when I try to connect to my Yun over the network using my android I have connection issues.
My local IP for the Yun is 192.168.1.6 however I am unsure how to port forward this local IP to allow for incoming connections from my app. From what I can gather I need to allow my app to connect to the arduino over the local network using the local IP 192.168.1.6. The following is my code uploaded onto my Yun. I am very new to arduinos so I do apologize for my limited knowledge. Any assistance would be greatly appreciated.
/* ===== pfod Command for Menu_3 ====
pfodApp msg {.} --> {.<+7>Sample On|A~<+2>Sample is `0~~Off\On}
/
// Using Serial1 and 115200 for send and receive
// Serial1 D0 (RX) and D1 (TX) on Arduino Leonardo, Yun and SparkFun FioV3 boards and D19 (RX) and D18 (TX) on Arduino Due and Mega boards
/ Code generated by pfodDesigner V1.2.782
- (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
/
// ======================
// this is the pfodParser.h file with the class renamed pfodParser_codeGenerated and with comments, constants and un-used methods removed
class pfodParser_codeGenerated: public Print {
public:
pfodParser_codeGenerated();
void connect(Stream ioPtr); void closeConnection(); byte parse(); byte* getCmd(); byte* getFirstArg();
byte getArgsCount(); byte* parseLong(byte* idxPtr, long result); size_t write(uint8_t c); void flush();
void init(); byte parse(byte in); Stream getPfodAppStream();
private:
Stream* io; byte argsCount; byte argsIdx; byte parserState; byte args[255+1];
};
//============= end of pfodParser_codeGenerated.h
pfodParser_codeGenerated parser; // create a parser to handle the pfod messages
// 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 'Sample is '
const int cmd_A_pin = 4; // name the output pin for 'Sample is '
// the setup routine runs once on reset:
void setup() {
Serial1.begin(115200);
for (int i=3; i>0; i--) {
// wait a few secs to see if we are being programmed
delay(1000);
}
parser.connect(&Serial1); // connect the parser to the i/o stream
cmd_A_var = 0;
//pinMode(cmd_A_pin, INPUT_PULLUP);
pinMode(cmd_A_pin, OUTPUT); // output for 'Sample 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
// <<<<<<<<< Your extra setup code goes here
}
// the loop routine runs over and over again forever:
void loop() {
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 moved slider -- 'Sample is '
// in the main Menu of Menu_3
// 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) {
// add any special code here to force connection to be dropped
}
void sendMainMenu() {
parser.print(F("{.")); // start a Menu screen pfod message
send_menuContents(); // send the menu contents for Menu_3
parser.print(F("}")); // close pfod message
}
void sendMainMenuUpdate() {
parser.print(F("{:")); // start an Update Menu pfod message
send_menuContents(); // send the menu contents for Menu_3
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("<+7>Sample On"));
// send menu items
parser.print(F("|A~<+2>Sample 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 item ===========
}
//=========================================================================
/* You can remove from here on if you have the pfodParser library installed from
http://www.forward.com.au/pfod/pfodParserLibraries/index.html
- and add
#include <EEPROM.h>
#include <pfodParser.h> - at the top of this file
- and replace the line
pfodParser_codeGenerated parser; // create a parser to handle the pfod messages - with
pfodParser parser;
/
// this is the pfodParser.cpp file with the class renamed pfodParser_codeGenerated and with comments, constants and un-used methods removed
pfodParser_codeGenerated::pfodParser_codeGenerated() {
io = NULL; init();
}
void pfodParser_codeGenerated::init() {
argsCount = 0; argsIdx = 0; args[0] = 0; args[1] = 0; parserState = ((byte)0xff);
}
void pfodParser_codeGenerated::connect(Stream ioPtr) {
init(); io = ioPtr;
}
void pfodParser_codeGenerated::closeConnection() {
init();
}
Stream* pfodParser_codeGenerated::getPfodAppStream() {
return io;
}
size_t pfodParser_codeGenerated::write(uint8_t c) {
if (!io) {
return 1; // cannot write if io null but just pretend to
}
return io->write(c);
}
void pfodParser_codeGenerated::flush() {
if (!io) {
return ; // cannot write if io null but just pretend to
}
io->flush();
}
byte* pfodParser_codeGenerated::getCmd() {
return args;
}
byte* pfodParser_codeGenerated::getFirstArg() {
byte* idxPtr = args;
while( idxPtr != 0) {
++idxPtr;
}
if (argsCount > 0) {
++idxPtr;
}
return idxPtr;
}
byte pfodParser_codeGenerated::getArgsCount() {
return argsCount;
}
byte pfodParser_codeGenerated::parse() {
byte rtn = 0;
if (!io) {
return rtn;
}
while (io->available()) {
int in = io->read();
rtn = parse((byte)in);
if (rtn != 0) {
// found msg
if (rtn == '!') {
closeConnection();
}
return rtn;
}
}
return rtn;
}
byte pfodParser_codeGenerated::parse(byte in) {
if ((parserState == ((byte)0xff)) || (parserState == ((byte)'}'))) {
parserState = ((byte)0xff);
if (in == ((byte)'{')) {
init();
parserState = ((byte)'{');
}
return 0;
}
if ((argsIdx >= (255-2)) &&
(in != ((byte)'}'))) {
init();
return 0;
}
if (parserState == ((byte)'{')) {
parserState = ((byte)0);
}
if ((in == ((byte)'}')) || (in == ((byte)'|')) || (in == ((byte)'~')) || (in == ((byte)'`'))) {
args[argsIdx++] = 0;
if (parserState == ((byte)0xfe)) {
argsCount++;
}
if (in == ((byte)'}')) {
parserState = ((byte)'}'); // reset state
args[argsIdx++] = 0;
return args[0];
} else {
parserState = ((byte)0xfe);
}
return 0;
}
args[argsIdx++] = in;
return 0;
}
byte pfodParser_codeGenerated::parseLong(byte* idxPtr, long *result) {
long rtn = 0;
boolean neg = false;
while ( *idxPtr != 0) {
if (*idxPtr == '-') {
neg = true;
} else {
rtn = (rtn<<3) + (rtn<<1);
rtn = rtn + (*idxPtr-'0');
}
++idxPtr;
}
if (neg) {
rtn = -rtn;
}
*result = rtn;
return ++idxPtr;
}
// ============= end generated code =========