// 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 TWITTERAPIKEY[] = "????????????????"; //thingtweet API key char status[256] = ""; char IPADDRESS[] = "api.thingspeak.com"; char URL[] = "/update"; char twitterURL[] = "/apps/thingtweet/1/statuses/update"; int dry = 2150, damp = 2100, moist = 2000, wet = 1800, soaking = 1200; // Figure out your own values for these readings 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 int lastStatus = -1; HttpClient http; HttpClient http2; 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 will NULL }; http_header_t twitterHeaders[] = { // { "Content-Type", "application/json" }, // { "Accept" , "application/json" }, { "Content-Type", "application/x-www-form-urlencoded" }, { NULL, NULL } // NOTE: Always terminate headers will NULL }; void setup() { pinMode(sensorPin, INPUT); pinMode(powerPin, OUTPUT); Serial.begin(9600); } void loop() { int val, status = 72; if (nextTime > millis()) { return; } request.hostname = IPADDRESS; request.port = 80; request.path = URL; digitalWrite(powerPin, HIGH); val = analogRead(sensorPin); digitalWrite(powerPin, LOW); if(val >= dry) status = 0; if(val >= damp && val < dry) status = 1; if(val >= moist && val < damp) status = 2; if(val >= wet && val < moist) status = 3; if(val >= soaking && val < wet) status = 4; if(val <= soaking) status = 5; request.body = "field1=" + String(val, DEC) + "&" + "field2=" + String(status, 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) + "&field3=" + String(val3, DEC) }; */ http.post(request, response, headers); lastStatus = status; request.path = twitterURL; request.body = "api_key=" + String(TWITTERAPIKEY) + "&status=Current status: " + (status == 0 ? "dry" : (status == 1 ? "damp" : (status == 2 ? "moist" : (status == 3 ? "wet" : "soaking")))); http2.post(request, response, twitterHeaders); nextTime = millis() + updateThingSpeakInterval; }