String writeAPIKey = "D0ZXL9NY51Z6NZPT"; //Put your ThingSpeak Write API key here. //String writeAPIKey = "INSERT API KEY HERE"; //Put your ThingSpeak Write API key here. unsigned int interval = 60000; //Update every Minute unsigned long int now=0, previous=0; //Variables used to hold time variables. #define DST_IP "api.thingspeak.com" //Destination Server void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect. Needed for Leonardo only Serial1.begin(115200); //Serial1.println("AT+RST"); Serial.println("ThingSpeak Demo"); Serial.println("Has your wifi already been setup? \nIf not run the Debug program"); //usb port Serial1.println("AT+CWMODE=1"); //Setup single user mode while (Serial1.available()) { Serial.write(Serial1.read()); } } } void loop() { now=millis(); //Get the current time. if(now - previous >= interval){ //Check to see if it's time to run. previous = now; Serial.println("Sending Datat to Thingspeak"); updateThingSpeak(); } } void updateThingSpeak(){ //Get current temperature int temp = 5.0 * analogRead(A2) * 100.0 / 1024; //Builds the web connection string String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += DST_IP; cmd += "\",80"; Serial1.println(cmd); //Run the command Serial.println(cmd); //Print this to the debug window delay(1000); //DEBUG LOOP- display ESP output to serial Monitor. while (Serial1.available()) { Serial.write(Serial1.read()); } //Build web connection string String httpcmd="GET /update?api_key="; httpcmd += writeAPIKey+"&field1="; httpcmd += temp; httpcmd += " HTTP/1.1\r\n"; httpcmd += "Host: api.thingspeak.com\n"; httpcmd += "Connection: close\r\n\r\n"; //I NEED TO UPDATE THIS IF STATEMENT TO MAKE SURE CONNECTION WORKED Serial1.print("AT+CIPSEND="); Serial1.println(httpcmd.length()); Serial.print("AT+CIPSEND="); Serial.println(httpcmd.length()); delay(1000); Serial.print(">"); Serial1.println(httpcmd); Serial.println(httpcmd); delay(3000); //DEBUG LOOP- display ESP output to serial Monitor. while (Serial1.available()) { Serial.write(Serial1.read()); } Serial.println("AT+CIPCLOSE"); Serial1.println("AT+CIPCLOSE"); //Close the Web Connection }