×

We will be closed on November 25, 2021 and November 26, 2021 in observance of the Thanksgiving holiday. We will resume normal operations on November 29, 2021. Wishing you a safe and happy holiday from all your friends at SparkFun!

×

Receive a free SparkFun USB Thumb Drive with every order this weekend. Details.

Member #665360

Member Since: March 25, 2015

Country: United States

  • Hello, i have adjusted this code to meet my requirements but once i plug it all up, the only thing on the serial monitor is: Testing connection it stays there and nothing else happens... I have configured my xbee wifi with X-CTU and it show to be connected with wifi but as soon as i plug it on my arduino xbee sheild, it doesnt connect and i get the Testing Connection messdage only once i upload the code.. when i am programming i put the jumper on the USB and once it has uploaded, i change it to XBEE but it has no effect. any help on this would be helpfull please! heres the code:

    #include <SoftwareSerial.h>
    

    // The Phant library makes creating POSTs super-easy

    include <Phant.h>

    // Time in ms, where we stop waiting for serial data to come in // 2s is usually pretty good. Don't go under 1000ms (entering // command mode takes about 1s).

    define COMMAND_TIMEOUT 2000 // ms

    // WiFi Network Stuff //

    // Your WiFi network's SSID (name): String WIFI_SSID = "saqib"; // Your WiFi network's encryption setting // Set the "encrypt" variable to one of these four characters: // OPEN = 0, WPA_TKIP = 1, WPA2_AES = 2, WEP = 3 enum encryption{NO_SECURITY, WPA_TKIP, WPA2_AES, WEP}; encryption WIFI_EE = WPA2_AES; // Your WiFi network's passphrase (if necessary). If your network // is open, make this a blank string (passphrase = "";) String WIFI_PSK = "saqibwifipassword";

    // Phant Stuff //

    String destIP = "54.86.132.254"; // data.sparkfun.com's IP address

    // Phant phant(server, publicKey, privateKey); Phant phant("data.sparkfun.com", "o85jyKNrXEIK32Gw4byN", "yzo87X59jEU2DZbEV5N8"); // Phant field string defintions. Make sure these match the // fields you've defined in your data stream: const String AirTempField = "AirTemp"; const String CamErrField = "CamErr"; const String CrankErrField = "CrankErr"; const String FuelConField = "FuelCon"; const String FuelPressField = "FuelPress"; const String GearField = "Gear"; const String KPHField = "KPH"; const String LambdaField = "Lambda"; const String MAPKpaField = "MAPKpa"; const String OilPressField = "OilPress"; const String OilTempField = "OilTemp"; const String RPMField = "RPM"; const String TPSField = "TPS"; const String VoltsField = "Volts"; const String WaterTempField = "WaterTemp";

    //////////////// // XBee Stuff // //////////////// const byte XB_RX = 2; // XBee's RX (Din) pin const byte XB_TX = 3; // XBee's TX (Dout) pin // We'll use "xB" from here-on to send and receive data to it: SoftwareSerial xB(XB_RX, XB_TX); const int XBEE_BAUD = 9600; // Your XBee's baud (9600 is default)

    //set the baud rate up to match the one on the can bus

    ///////////////////////////// // Input Pin Stuff // ///////////////////////////// int a; int b; int c; int d; int e; int f; int g; int h; int i; int j; int k; int l; int m; int n; int o; //think about adding the CAN bus variables here

    ///////////////////////// // Update Rate Control // ///////////////////////// // Phant limits you to 10 seconds between posts. Use this variable // to limit the update rate (in milliseconds): const unsigned long UPDATE_RATE = 10000; // 10000ms = 10 seconds unsigned long lastUpdate = 0; // Keep track of last update time

    /////////// // Setup // /////////// // In setup() we configure our INPUT PINS, start the XBee and // SERIAL ports, and CONNECT TO THE WIFI NETWORK. void setup() { // Set up sensor pins: // a=0; // b=0; // c=0; // d=0; // e=0; // f=0; // g=0; // h=0; // i=0; // j=0; // k=0; // l=0; // m=0; // n=0; // o=0;

    // Set up serial ports: Serial.begin(9600); // Make sure the XBEE BAUD RATE matches its pre-set value // (defaults to 9600). xB.begin(XBEE_BAUD);

    // Set up WiFi network Serial.println("Testing network"); // connectWiFi will attempt to connect to the given SSID, using // encryption mode "encrypt", and the passphrase string given. connectWiFi(WIFI_SSID, WIFI_EE, WIFI_PSK); // Once connected, print out our IP address for a sanity check: Serial.println("Connected!"); Serial.print("IP Address: "); printIP(); Serial.println();

    // setupHTTP() will set up the destination address, port, and // make sure we're in TCP mode: setupHTTP(destIP);

    // Once everything's set up, send a data stream to make sure // everything check's out: Serial.print("Sending update..."); if (sendData()) Serial.println("SUCCESS!"); else Serial.println("Failed :("); }

    ////////// // Loop // ////////// // loop() constantly checks to see if enough time has lapsed // (controlled by UPDATE_RATE) to allow a new stream of data // to be posted. // Otherwise, to kill time, it'll print out the sensor values // over the serial port. void loop() { // If current time is UPDATE_RATE milliseconds greater than // the last update rate, send new data. if (millis() > (lastUpdate + UPDATE_RATE)) { Serial.print("Sending update..."); if (sendData()) Serial.println("SUCCESS!"); else Serial.println("Failed :("); lastUpdate = millis(); } // In the meanwhile, we'll print data to the serial monitor, // just to let the world know our Arduino is still operational: readSensors(); // Get updated values from sensors Serial.print(millis()); // Timestamp Serial.print(": "); Serial.print(a); Serial.print('\t'); Serial.print(b); Serial.print('\t'); Serial.print(c); Serial.print('\t'); Serial.print(d); Serial.print('\t'); Serial.print(e); Serial.print('\t'); Serial.print(f); Serial.print('\t'); Serial.print(g); Serial.print('\t'); Serial.print(h); Serial.print('\t'); Serial.print(i); Serial.print('\t'); Serial.print(j); Serial.print('\t'); Serial.print(k); Serial.print('\t'); Serial.print(l); Serial.print('\t'); Serial.print(m); Serial.print('\t'); Serial.print(n); Serial.print('\t'); Serial.println(o); delay(1000);

    //add the messgae bytes here to display on the serial monitor }

    //////////////// // sendData() // //////////////// // sendData() makes use of the PHANT LIBRARY to send data to the // data.sparkfun.com server. We'll use phant.add() to add specific // parameter and their values to the param list. Then use // phant.post() to send that data up to the server. int sendData() { xB.flush(); // Flush data so we get fresh stuff in // IMPORTANT PHANT STUFF!!! // First we need to add fields and values to send as parameters // Since we just need to read values from the analog pins, this // can be automized with a for loop: //readSensors(); // Get updated values from sensors. //phant.add(tempField, tempVal); //follow this as shown

    phant.add(AirTempField, a); phant.add(CamErrField, b); phant.add(CrankErrField, c); phant.add(FuelConField, d); phant.add(FuelPressField, e); phant.add(GearField, f); phant.add(KPHField, g); phant.add(LambdaField, h); phant.add(MAPKpaField, i); phant.add(OilPressField, j); phant.add(OilTempField, k); phant.add(RPMField, l); phant.add(TPSField, m); phant.add(VoltsField, n); phant.add(WaterTempField, o);

    // After our PHANT.ADD's we need to PHANT.POST(). The post needs // to be sent out the XBee. A simple "print" of that post will // take care of it. xB.print(phant.post());

    // Check the response to make sure we receive a "200 OK". If // we were good little programmers we'd check the content of // the OK response. If we were good little programmers... char response[12]; if (waitForAvailable(12) > 0) { for (int i=0; i<12; i++) { response[i] = xB.read(); } if (memcmp(response, "HTTP/1.1 200", 12) == 0) return 1; else { Serial.println(response); return 0; // Non-200 response } } else // Otherwise timeout, no response from server return -1; }

    // readSensors() will simply update a handful of global variables // It updates tempVal, lightVal, coVal, and methaneVal void readSensors() { a = a + 1; b = b + 2; c = c + 3; d = d + 4; e = e + 5; f = f + 6; g = g + 7; h = h + 8; i = i + 9; j = j + 10; k = k + 11; l = l + 12; m = m + 13; n = n + 14; o = o + 15;

    }

    /////////////////////////// // XBee WiFi Setup Stuff // /////////////////////////// // setupHTTP() sets three important parameters on the XBee: // 1. Destination IP -- This is the IP address of the server // we want to send data to. // 2. Destination Port -- We'll be sending data over port 80. // The standard HTTP port a server listens to. // 3. IP protocol -- We'll be using TCP (instead of default UDP). void setupHTTP(String address) { // Enter command mode, wait till we get there. while (!commandMode(1)) ;

    // Set IP (1 - TCP) command("ATIP1", 2); // RESP: OK // Set DL (destination IP address) command("ATDL" + address, 2); // RESP: OK // Set DE (0x50 - port 80) command("ATDE50", 2); // RESP: OK

    commandMode(0); // Exit command mode when done }

    /////////////// // printIP() // /////////////// // Simple function that enters command mode, reads the IP and // prints it to a serial terminal. Then exits command mode. void printIP() { // Wait till we get into command Mode. while (!commandMode(1)) ; // Get rid of any data that may have already been in the // serial receive buffer: xB.flush(); // Send the ATMY command. Should at least respond with // "0.0.0.0\r" (7 characters): command("ATMY", 7); // While there are characters to be read, read them and throw // them out to the serial monitor. while (xB.available() > 0) { Serial.write(xB.read()); } // Exit command mode: commandMode(0); }

    // connectWiFi(id, ee, psk) //

No public wish lists :(