Auto updating Analog pin Value to ThingSpeak!
Feb 21 2015
Use following code to automatically update analog pin value to ThingSpeak
// Auto Connect to Internet using MangoCube WiFi by Bhargav Mistry #define DEBUG false boolean cmode = false; String response = ""; char c; //------------ Your WiFi settings String sid = "Century"; // SSID : Name of Home WiFi String Auth = "WPA2PSK"; // Authentication : OPEN, SHARED, WPAPSK, WPA2PSK String Encry = "AES"; // Encryption : AES, TKIP, NONE, WEP String Key = "yourpassword"; // key : your WiFi Internet Password String Url = "http://api.thingspeak.com"; // URL: address of the website to connect //String Pph ="/update?key=E7FSORWU63FL9DL5&field1=10"; // Header path String ApiKey = "E7FSORWU63FL9DL5"; // Api key : Thingspeak's key String FValue = "0"; // Field Value : Value to upload String FLink; // Full Link // the setup routine runs once when you press reset: void setup() { pinMode(13, OUTPUT); digitalWrite(13,LOW); Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Serial1.begin(115200); delay(3000); // delay after reboot, enough time to open serial moitor sendData("+++",500,DEBUG); // send +++ if(response == "a") // if response is a { sendData("a",500,DEBUG); // send a if(response == "+ok\r\n\r\n") // if response is a+ok { Serial.println("In Command mode"); // Command Mode cmode = true; } } sendData("AT+E=off\r",500,DEBUG); // Hide Echo if(response =="AT+E=off\n\r+ok\r\n\r\n") // if response is a+ok { Serial.println("Echo Off"); } sendData("AT+WMODE=STA\r",500,DEBUG); // Set STA mode if(response =="+ok\r\n\r\n") // if response is a+ok { Serial.println("STA Mode"); } sendData("AT+HTTPURL="+Url+",80\r",500,DEBUG); // Set Url if(response =="+ok\r\n\r\n") // if response is a+ok { Serial.println("URL is set :"+ Url); } sendData("AT+WSSSID="+sid+"\r",500,DEBUG); // Set SSID if(response =="+ok\r\n\r\n") // if response is a+ok { Serial.println("SSID is set :"+ sid); } sendData("AT+WSKEY="+Auth+","+Encry+","+Key+"\r",500,DEBUG); // Set Security Key if(response =="+ok\r\n\r\n") // if response is a+ok { Serial.println("SKEY is set :"+Auth+","+Encry+", *****"); } delay(100); Serial1.print("AT+Z\r"); // Restart WiFi module delay(7000); // enter command mode again....................................... sendData("+++",500,DEBUG); // send +++ if(response == "a") // if response is a { sendData("a",500,DEBUG); // send a if(response == "+ok\r\n\r\n") // if response is a+ok { Serial.println("In Command mode"); // Command Mode cmode = true; } } sendData("AT+E=off\r",500,DEBUG); // Hide Echo if(response =="AT+E=off\n\r+ok\r\n\r\n") // if response is a+ok { Serial.println("Echo Off"); } sendData("AT+WIFI\r",500,DEBUG); // Set STA mode } void loop() { if(cmode) { sendData("AT+WIFI\r",500,DEBUG); //Check if connected to WiFi if(response =="+ok=UP\r\n\r\n") // if response is a+ok { Serial.println("Connected to Internet!"); } delay(1000); } // Use this code to upload different value of Val. Tip: assign Val to Analog pin value!! for(int Val=0; Val<15; Val=Val+2) { FLink = "AT+HTTPPH=/update?key="+ApiKey+"&field1="+Val; Serial.println(FLink); sendData(FLink+"\r",500,DEBUG); // Set Header path Serial.println(response); if(response =="+ok\r\n\r\n") // if response is a+ok { sendData("AT+HTTPDT\r",5000,DEBUG); // Set Header path Serial.print(response); } delay(11000); } for(int Val=13; Val>0; Val=Val-2) { FLink = "AT+HTTPPH=/update?key="+ApiKey+"&field1="+Val; Serial.println(FLink); sendData(FLink+"\r",500,DEBUG); // Set Header path Serial.println(response); if(response =="+ok\r\n\r\n") // if response is a+ok { sendData("AT+HTTPDT\r",5000,DEBUG); // Set Header path Serial.print(response); } delay(11000); } // Your code here.... // } // Do not change the following...important..!! String sendData(String command, const int timeout, boolean debug) { response = ""; Serial1.print(command); long int time = millis(); while( (time+timeout) > millis()) { while(Serial1.available()) { c = Serial1.read(); // read the next character. response+=c; // combined all the character together } } if(debug) { Serial.print(response); } return response; }