// This #include statement was automatically added by the Spark IDE. #include "HttpClient/HttpClient.h" #include "application.h" int sensorPin = A0, powerPin = D6; char APIKEY[] = "????????????????"; //thingspeak API key char status[256] = ""; char IPADDRESS[] = "api.thingspeak.com"; char URL[] = "/update"; const int updateThingSpeakInterval = 60 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval) unsigned int nextTime = 0; // Next time to contact the server HttpClient http; http_request_t request; http_response_t response; http_header_t headers[] = { // { "Content-Type", "application/json" }, // { "Accept" , "application/json" }, { "X-THINGSPEAKAPIKEY" , APIKEY }, { "Content-Type", "application/x-www-form-urlencoded" }, { NULL, NULL } // NOTE: Always terminate headers with NULL }; void setup() { pinMode(sensorPin, INPUT); pinMode(powerPin, OUTPUT); } void loop() { int val; if (nextTime > millis()) { return; } request.hostname = IPADDRESS; request.port = 80; request.path = URL; digitalWrite(powerPin, HIGH); val = analogRead(sensorPin); digitalWrite(powerPin, LOW); request.body = "field1=" + String(val, DEC); /* if you want to send more than one field at the same time (multiple sensors etc) you can do the following: request.body = { "field1=" + String(val, DEC) + "&" + "field2=" + String(val2, DEC) // Do not omit + "&" + "field3=" + String(val3, DEC) // the & }; */ http.post(request, response, headers); nextTime = millis() + updateThingSpeakInterval; }