EasyManua.ls Logo

sparkfun ESP8266 - Page 20

sparkfun ESP8266
33 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd) to "Thing-":
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.macAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
String postedID = "Thing-" + macID;
// Add the four field/value pairs defined by our stream:
phant.add("id", postedID);
phant.add("analog", analogRead(ANALOG_PIN));
phant.add("digital", digitalRead(DIGITAL_PIN));
phant.add("time", millis());
// Now connect to data.sparkfun.com, and post our data:
WiFiClient client;
const int httpPort = 80;
if (!client.connect(PhantHost, httpPort))
{
// If we fail to connect, return 0.
return 0;
}
// If we successfully connected, print our Phant post:
client.print(phant.post());
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
//Serial.print(line); // Trying to avoid using serial
}
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
return 1; // Return success
}
After loading the code onto your Thing, it will begin to run. The status LED connected to pin 5
will initially blink at about 2 Hz. After the Thing connects to your network, the green LED will
turn solid and post to the test stream. At that point the LED will go dark, only blinking every
30s-or-so as the Thing posts to Phant again.
If the LED never stops blinking, your Thing is probably having trouble connecting to the WiFi
network. Make sure the SSID and PSK variables are set correctly.
Four values are posted to the Phant stream: the reading from the ADC pin, a digital reading
from pin 12, the Thing’s ID (“Thing” appendd with the last two MAC bytes), and a time variable
loaded from the millis() function. Load up the test stream to check for your Thing’s signature
there!

Other manuals for sparkfun ESP8266