Stunts1337 0 Posted November 20, 2014 Share Posted November 20, 2014 Hi everyone, I first want to say this isn't a huge priority, but if you have time to look over my code and point out any obvious errors I would appreciate it. The code is based off of the getWeather sketch provided by Energia, it searches a server api that I control for 4 integer values: 3 that are used to program a digital potentiometer and one that controls 3 different GPIO pins which ultimately controls some relays. The basic idea is the msp430 will test the circuit I select by turning on the correct relay, and the digital potentiometer allows for an user adjustable gain in the circuit being tested. The problem I am having is the cc3200 launchpad frequently becomes unable to connect to server when I run the code below. The msp430 gets into a state where it keeps trying to connect to server but is never able to. It typically takes the data from the server about 10 times before becoming unable to connect, and seems to fail more when the data in the server has changed. The code searches the server every 5 seconds. If anyone has any suggestions to potentially help the problem I am having with my code, any feedback would be appreciated. #include <SPI.h> #include <WiFi.h> // network login info char ssid[] = "(my SSID)"; char password[] = "(my Pass)"; IPAddress hostIp(184,106,153,149); //IP address for ThingSpeak WiFiClient client; const unsigned long requestInterval = 5000; // delay between requests (5 sec) unsigned long lastAttemptTime = 0; // variable to hold the last time you connected to the server String currentLine = ""; // string to hold the text from server String circuitString = ""; // string to hold circuit selection boolean readingCircuit = false; // if you're currently reading the circuit selection String invertingString = ""; // string to hold inverting resistance selection boolean readingInverting = false; // if you're currently reading the inverting resistance selection String nonInvertingString = ""; // string to hold non-inverting resistance selection boolean readingNonInverting = false; // if you're currently reading the non-inverting resistance selection String weightedString = ""; // string to hold weighted summer resistance selection boolean readingWeighted = false; // if you're currently reading the weighted summer resistance selection int circuit = 1; // holds circuit selection (inverting = defalt) int inverting = 33; // holds inverting resistance selection (33 = min value) int nonInverting = 4; // holds non-inverting resistance selection (4 = min value) int weighted = 17; // holds weighted summer resistance selection (17 = min value) int temp = 0; // temporary integer value used in string to int function boolean flag = false; // used to update values (I/O pins and digital pot) void setup() { currentLine.reserve(100); // reserve space for strings circuitString.reserve(10); invertingString.reserve(10); nonInvertingString.reserve(10); weightedString.reserve(10); Serial.begin(115200); // set baud rate for serial communication //DIGITAL I/O PINS FOR RELAY CONTROL SETUP pinMode (8, OUTPUT); pinMode (9, OUTPUT); pinMode (10, OUTPUT); digitalWrite(8,LOW); digitalWrite(9,LOW); digitalWrite(10,LOW); //DIGITAL POTENTIOMETER SETUP pinMode (18, OUTPUT); // SS pin for SPI data transfer SPI.begin(); // SPI for digital potentiometer programming digitalPotWrite(0,inverting); // initalize digital potentiometer values to base values (gain of ~1.2 V/V, or ~6V output) delay(10); //short delay digitalPotWrite(2,nonInverting); delay(10); //short delay digitalPotWrite(1,weighted); //WIFI CONNECTION SETUP Serial.print("Attempting to connect to Network named: "); Serial.println(ssid); WiFi.begin(ssid, password); while ( WiFi.status() != WL_CONNECTED) { Serial.print("."); // print dots while we wait to connect delay(300); } Serial.println("\nYou're connected to the network"); Serial.println("Waiting for an ip address"); while (WiFi.localIP() == INADDR_NONE) { Serial.print("."); // print dots while we wait for an ip addresss delay(300); } Serial.println("\nIP Address obtained"); printWifiStatus(); } void loop() { if (client.available()) { char inChar = client.read(); // read incoming bytes: currentLine += inChar; // add incoming byte to end of line: if (inChar == '\n') { // if a newline is found, clear the line: currentLine = ""; } // LOOKING FOR CIRCUIT SELECTION if ( currentLine.endsWith("<field1>")) { readingCircuit = true; // Circuit selection data is beginning, clear circuit string circuitString = ""; } // READING CIRCUIT SELECTION DATA if (readingCircuit) { if (inChar != 'f') { // if you see 'f', you're done reading circuit selection circuitString += inChar; } else { readingCircuit = false; //termination character was found Serial.print("- Circuit Selection: "); circuit = getInt(circuitString); Serial.print(circuit); Serial.println(" (1 = non-inverting, 2 = inverting, 3 = weighted summer)"); } } // LOOKING FOR INVERTING RESISTANCE if ( currentLine.endsWith("<field2>")) { readingInverting = true; // Circuit selection data is beginning, clear inverting string invertingString = ""; } // READING INVERTING RESISTANCE DATA if (readingInverting) { if (inChar != 'f') { // if you see 'f', you're done reading inverting selection invertingString += inChar; } else { readingInverting = false; //termination character was found Serial.print("- Inverting Resistance Selection: "); inverting = getInt(invertingString); Serial.println(inverting); } } // LOOKING FOR NON-INVERTING RESISTANCE if ( currentLine.endsWith("<field3>")) { readingNonInverting = true; // Circuit selection data is beginning, clear non-inverting string nonInvertingString = ""; } // READING NON-INVERTING RESISTANCE DATA if (readingNonInverting) { if (inChar != 'f') { // if you see 'f', you're done reading non-inverting selection nonInvertingString += inChar; } else { readingNonInverting = false; //termination character was found Serial.print("- Non-Inverting Resistance Selection: "); nonInverting = getInt(nonInvertingString); Serial.println(nonInverting); } } // LOOKING FOR WEIGHTED SUMMER RESISTANCE if ( currentLine.endsWith("<field4>")) { readingWeighted = true; // Circuit selection data is beginning, clear weighted summer string weightedString = ""; } // READING WEIGHTED SUMMER RESISTANCE DATA if (readingWeighted) { if (inChar != 'f') { // if you see 'f', you're done reading weighted summer selection weightedString += inChar; } else { readingWeighted = false; //termination character was found Serial.print("- Weighted Summer Resistance Selection: "); weighted = getInt(weightedString); Serial.println(weighted); flag = true; // Done reading data, so set flag } } if (flag) { //done reading server data, now update digital potentiometer and GPIO pins Serial.println("Done reading values!"); digitalPotWrite(0,nonInverting); //write to resistor 1 digitalPotWrite(1,weighted); //write to resistor 2 digitalPotWrite(2,inverting); //write to resistor 3 if (circuit == 2) { // Non-Inverting selected Serial.println("Non-Inverting resistance updated!"); digitalWrite(8,HIGH); digitalWrite(9,LOW); digitalWrite(10,LOW); } else if (circuit == 1) { // Inverting selected Serial.println("Inverting resistance updated!"); digitalWrite(8,LOW); digitalWrite(9,HIGH); digitalWrite(10,LOW); } else { // Weighted summer selected, default case Serial.println("Weighted summer resistance updated!"); digitalWrite(8,LOW); digitalWrite(9,LOW); digitalWrite(10,HIGH); } Serial.println(""); flag = false; //reset flag } } else if (millis() - lastAttemptTime > requestInterval) { connectToServer(); } } void connectToServer(){ Serial.println("connecting to server..."); if (client.connect(hostIp, 80)) { Serial.println("Connected! Reading web server data..."); client.println("GET /channels/(my channel id)/feed/last.xml"); // make HTTP GET request to ThingSpeak client.print("HOST: http://api.thingspeak.com\n"); // declare correct server client.println("Connection: close"); // close connection client.println(); } lastAttemptTime = millis(); // timestamp for this connection } void printWifiStatus() { Serial.print("SSID: "); Serial.println(WiFi.SSID()); //print SSID IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); //print IP address Serial.println(""); } int digitalPotWrite(int address, int value) { Serial.println("Writing resistance values"); digitalWrite(18,LOW); // take the SS pin low to select the digital pot SPI.transfer(address); // send in the address (resistor selection) SPI.transfer(value); // send in the value (resistor value) digitalWrite(18,HIGH); // take the SS pin high to de-select the digital pot } int getInt(String input){ //converts string to integer int i = 1; while(input[i] != '<'){ i++; } input = input.substring(1,i); char carray[20]; input.toCharArray(carray, sizeof(carray)); temp = atoi(carray); //atoi() converts an array of char to a string return temp; } Quote Link to post Share on other sites
jeffchen622 0 Posted November 22, 2014 Share Posted November 22, 2014 we have same problem when use mqtt library( PubSubClient). we review the code and it used client.h to connect our server. when we init our code.it start great no problem . but after few minutes or an hour random. _client can't connect to our server anymore even we restart wifi use our check code below .we have press reset button on the board then it will start connect correct again. we are not sure what the problem. because we don't have any debug tool to know. it seems hardware\cc3200\cores\cc3200\Client.h have someting wrong or SRAM overflow problem. check connect if lost and restart connect code at loop function if ((millis() - keepalivetime)>10000) { if(mqttclient.loop()) { if( SETDEBUG ) { Serial.println("(MQTTClient Loop)->True"); } } else { // check WIFI status if(WiFi.status() != WL_CONNECTED) { if( SETDEBUG ) { Serial.println("WiFi status LOST! init WiFi again."); initWiFi(); } } printCurrentNet(); if( SETDEBUG ) { Serial.println("(MQTTClient Loop)->False and connect again."); } if (mqttclient.connect(macaddressArry)) { <----- always return false here . our server no problem mqttclient.publish(publishtopics,macaddress,maclen); mqttclient.subscribe(subscribetopics); keepalivetime=millis(); } else { Serial.println( "(MQTTClient connect)->False" ); } } keepalivetime = millis(); } //---------------------------------------------------------------------------------------------------------- PubSubClient.cpp boolean PubSubClient::connected() { <---- always return false when our server lost connection.but our server still running no problem boolean rc; if (_client == NULL ) { rc = false; } else { rc = (int)_client->connected(); if (!rc) _client->stop(); } return rc;} Quote Link to post Share on other sites
Stunts1337 0 Posted November 24, 2014 Author Share Posted November 24, 2014 @jeffchen622,Thanks for the helpful insight, luckily for me the grader for my project was understanding since he added the wifi aspect to our project at short notice. This project is now complete for me, but the problem still technically persists. I will likely come back to this in the future after the semester when I play around with my cc3200 when I have more free time. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.