#ifndef __CC3200R1M1RGC__ // Do not include SPI for CC3200 LaunchPad #include #endif #include #include #include // ThingSpeak Settings char thingSpeakAddress[] = "api.thingspeak.com"; String writeAPIKey = "O6**********M4"; //your Thing speak write API key const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval) //buffer for float to string char buffer[25]; // your network name also called SSID char ssid[] = "YOUR SSID"; // your network password char password[] = "YOUR PASSWORD"; // initialize the library instance: WiFiClient client; unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds boolean lastConnected = false; // state of the connection last time through the main loop const unsigned long postingInterval = 10*1000; int failedCounter = 0; void setup() { Serial.begin(9600); Serial.print("Attempting to connect to Network named: "); Serial.println(ssid); WiFi.begin(ssid, password); while ( WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(300); } Serial.println("\nYou're connected to the network"); Serial.println("Waiting for an ip address"); while (WiFi.localIP() == INADDR_NONE) { // print dots while we wait for an ip addresss Serial.print("."); delay(300); } Serial.println("\nIP Address obtained"); //printWifiStatus(); } void loop() { while (client.available()) { char c = client.read(); Serial.print(c); } if (!client.connected() && lastConnected) { Serial.println(); Serial.println("disconnecting."); client.stop(); } Serial.println("----------------------------------------------------------------------------------------------------------------------------------------------------"); Serial.print("Level Sensor: "); String sobjt = String(analogRead(6), DEC); Serial.print(sobjt); Serial.println("----------------------------------------------------------------------------------------------------------------------------------------------------"); updateThingSpeak("field1=" + sobjt); lastConnected = client.connected(); } void updateThingSpeak(String tsData) { if (client.connect(thingSpeakAddress, 80)) { client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(tsData.length()); Serial.println(">>TSDATALength=" + tsData.length()); client.print("\n\n"); client.print(tsData); Serial.println(">>TSDATA=" + tsData); lastConnectionTime = millis(); if (client.connected()) { Serial.println("Connecting to ThingSpeak..."); Serial.println(); failedCounter = 0; } else { failedCounter++; Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")"); Serial.println(); } } else { failedCounter++; Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")"); Serial.println(); lastConnectionTime = millis(); } } int getLength(int someValue) { int digits = 1; int dividend = someValue / 10; while (dividend > 0) { dividend = dividend / 10; digits++; } return digits; }