Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <Adafruit_NeoPixel.h>
- #define PIN 6
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
- const char WiFiSSID[] = "NSA";
- const int LED_PIN = 5; // Thing's onboard, green LED
- const int ANALOG_PIN = A0; // The only analog pin on the Thing
- const int DIGITAL_PIN = 12; // Digital pin to be read
- WiFiClient client;
- String responseString;
- String currentCondition;
- boolean startCapture;
- void connect()
- {
- byte ledStatus = LOW;
- Serial.println();
- Serial.println("Connecting to: " + String(WiFiSSID));
- // Set WiFi mode to station (as opposed to AP or AP_STA)
- WiFi.mode(WIFI_STA);
- // WiFI.begin([ssid], [passkey]) initiates a WiFI connection
- // to the stated [ssid], using the [passkey] as a WPA, WPA2,
- // or WEP passphrase.
- WiFi.begin(WiFiSSID);
- // Use the WiFi.status() function to check if the ESP8266
- // is connected to a WiFi network.
- while (WiFi.status() != WL_CONNECTED){
- // Blink the LED
- digitalWrite(LED_PIN, ledStatus); // Write LED high/low
- ledStatus = (ledStatus == HIGH) ? LOW : HIGH;
- // Delays allow the ESP8266 to perform critical tasks
- // defined outside of the sketch. These tasks include
- // setting up, and maintaining, a WiFi connection.
- delay(100);
- // Potentially infinite loops are generally dangerous.
- // Add delays -- allowing the processor to perform other
- // tasks -- wherever possible.
- }
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void initHardware()
- {
- Serial.begin(9600);
- pinMode(DIGITAL_PIN, INPUT_PULLUP); // Setup an input to read
- pinMode(LED_PIN, OUTPUT); // Set LED as output
- digitalWrite(LED_PIN, HIGH); // LED off
- // Don't need to set ANALOG_PIN as input,
- // that's all it can be.
- }
- // Perform an HTTP GET request to a remote page
- bool getPage() {
- const char http_site[] = "api.openweathermap.org";
- const int http_port = 80;
- Serial.println("About to connect");
- // Attempt to make a connection to the remote server
- if ( !client.connect(http_site, http_port) ) {
- Serial.println("Connect failed");
- return false;
- }
- Serial.println("Connect succeeded");
- // Make an HTTP GET request
- client.println("GET /data/2.5/weather?q=SanFrancisco,usa&appid=e798a6b30f92812133a91c06a33dbe41 HTTP/1.1");
- client.print("Host: ");
- client.println(http_site);
- client.println("Connection: close");
- client.println();
- Serial.println("Data sent");
- return true;
- }
- String getValuesFromKey(const String response, const String sKey){
- String quotedKey;
- quotedKey = "\"" + sKey + "\":";
- char key[quotedKey.length()];
- quotedKey.toCharArray(key, sizeof(key));
- int keySize = sizeof(key)-1;
- String result = "";
- int responseLength = response.length();
- for(int i=0; i < (responseLength-keySize-1); i++){
- char c[keySize];
- for(int k=0; k<keySize; k++){
- c[k] = response.charAt(i+k);
- }
- boolean isEqual = true;
- for(int k=0; k<keySize; k++){
- if(!(c[k] == key[k])){
- isEqual = false;
- break;
- }
- }
- if(isEqual){
- int j= i + keySize + 1;
- while(!(response.charAt(j) == ',')){
- result += response.charAt(j);
- j++;
- }
- //Remove char '"'
- result.replace("\"","");
- break;
- }
- }
- return result;
- }
- void colorWipe(uint32_t c, uint8_t wait) {
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- strip.show();
- delay(10);
- }
- }
- void setColor(int R, int G, int B){
- colorWipe(strip.Color(R, G, B), 50);
- }
- void updateColor(String condition){
- if (condition == "clear sky"){
- setColor(100, 100, 100);
- }
- if (condition == "few clouds"){
- setColor(100, 100, 100);
- }
- if (condition == "scattered clouds"){
- setColor(100, 100, 100);
- }
- if (condition == "broken clouds"){
- setColor(100, 100, 100);
- }
- if (condition == "shower rain"){
- setColor(100, 100, 100);
- }
- if (condition == "broken clouds"){
- setColor(100, 100, 100);
- }
- if (condition == "rain"){
- setColor(100, 100, 100);
- }
- if (condition == "thunderstorm"){
- setColor(100, 100, 100);
- }
- if (condition == "snow"){
- setColor(100, 100, 100);
- }
- if (condition == "mist"){
- setColor(100, 100, 100);
- }
- }
- void setup()
- {
- initHardware(); // Setup input/output I/O pins
- connect(); // Connect to WiFi
- digitalWrite(LED_PIN, LOW); // LED on to indicate connect success
- delay(500);
- if ( !getPage() ) {
- Serial.println("GET request failed");
- }
- }
- void loop()
- {
- if (client.available())
- {
- char c = client.read();
- if(c == '{'){
- startCapture=true;
- }
- if(startCapture){
- responseString += c;
- }
- }
- // If the server has disconnected, stop the client and WiFi
- if ( !client.connected() ) {
- Serial.println();
- // Close socket and wait for disconnect from WiFi
- client.stop();
- if ( WiFi.status() != WL_DISCONNECTED ) {
- WiFi.disconnect();
- }
- // Turn off LED
- digitalWrite(LED_PIN, LOW);
- // Do nothing
- Serial.println("Received " + (String)responseString.length() + " bytes");
- Serial.println("Disconnecting.");
- client.stop();
- client.flush();
- Serial.println();
- currentCondition = getValuesFromKey(responseString,"description");
- Serial.print("description:");
- Serial.println(currentCondition);
- Serial.println();
- updateColor(currentCondition);
- delay(1800000);
- connect();
- // Serial.println("reconnected");
- if( !getPage() ) {
- Serial.println("GET request failed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement