Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
simonhyde88
Published © GPL3+

Get Data and Display It Using a Children's Toy

This project uses a WeMos D1 to grab data from a website and display it using coloured LEDs.

IntermediateFull instructions provided900
Get Data and Display It Using a Children's Toy

Things used in this project

Hardware components

Pushbutton Switch, Momentary
Pushbutton Switch, Momentary
×1
Wemos D1 Mini
Espressif Wemos D1 Mini
×1
WeMos D1 battery module
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

I couldnt work out Fritzing

Here are the connections for the reset button

Code

Air Visual sketch

Arduino
Use this if you are using the Air Visual free API
/*
  Data grab and deep sleep with button using Air Visual API

  Created on: 10/10/2019

*/
#include <NeoPixelBrightnessBus.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <ESP8266HTTPClient.h>
#include <Ticker.h>
#include <jsonlib.h>
Ticker ticker;
const uint16_t PixelCount = 2; //
const uint8_t PixelPin = 3;  //
#define colorSaturation 128
String mostSignificantDigit;
// Declare our NeoPixel strip object:
NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount);
//Declare the colours
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor white(colorSaturation);
RgbColor black(0);
RgbColor purple(246, 22, 246);
RgbColor orange(255, 137, 0);
RgbColor yellow(255, 239, 0);
RgbColor darkpurple(137, 36, 142);
RgbColor darkmag(158, 6, 2);


ESP8266WiFiMulti WiFiMulti;
int number_airindex ;
int numLedsToLight;
//sleep time
const int sleepSeconds_show = 1000 * 10; // 10 seconds
void tick()
{
  int state = digitalRead(BUILTIN_LED);  // get the current state of GPIO1 pin
  digitalWrite(BUILTIN_LED, !state);     // set pin to the opposite state
}
void setup() {
  Serial.begin(115200);
  strip.Begin();
  strip.SetBrightness(120);

  strip.Show(); // Initialize all pixels to 'off'
  strip.ClearTo(white ); strip.Show();
  pinMode(BUILTIN_LED, OUTPUT);
  ticker.attach(0.5, tick);
  //WiFiManager wifiManager;
  //wifiManager.autoConnect();
  // Serial.setDebugOutput(true);
  Serial.setTimeout(2000);
  while (!Serial) { }
  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...", t);
    Serial.flush();

    delay(1000);
  }

  //WiFiMulti.addAP("SSID", "password");
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    HTTPClient http;

    Serial.print("[HTTP] begin...");
    // configure traged server and url
    // get the entry_id from AirVisual
    http://api.airvisual.com/v2/nearest_city?key={Your secret key}//
    http.begin("http://api.airvisual.com/v2/nearest_city?key={Your secret key}"); //HTTP
    Serial.print("[HTTP] begin...");
    Serial.println("[HTTP] GET...");
    // start connection and send HTTP header
    Serial.print("http://api.airvisual.com/v2/nearest_city?key={Your secret key}");
    int httpCode = http.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d", httpCode);

      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        String pollution_list = jsonExtract(payload, "data");
        String air_pollution_list = jsonExtract(payload, "pollution");
        int number_airindex = jsonExtract(air_pollution_list, "aqius").toFloat();
        

        Serial.print("AQI index is..");
        Serial.println(number_airindex); //air pollution idex
        Serial.println();
        //Light the LEDs
        Serial.println("Switch on LED");
        //Light the LEDs
        if (number_airindex >= 300) {
          // do Thing A
          strip.ClearTo(darkmag); strip.Show();
        }
        else if (number_airindex >= 200) {
          // do Thing B
          strip.ClearTo(darkpurple); strip.Show();
        }
        else if (number_airindex >= 150) {
          // do Thing B
          strip.ClearTo(red); strip.Show();
        }
        else if (number_airindex >= 100) {
          // do Thing B
          strip.ClearTo(orange); strip.Show();
        }
        else if (number_airindex >= 50) {
          // do Thing B
          strip.ClearTo(yellow); strip.Show();
        }
        else {
          // do Thing C
          strip.ClearTo(green); strip.Show();
        }
      }

    } else {
      Serial.printf("[HTTP] GET... failed, error: %s", http.errorToString(httpCode).c_str());
      strip.ClearTo(purple); strip.Show();
    }
    http.end();
  }

  Serial.println("Here");
  delay(sleepSeconds_show);
  //Turn the LEDs to black
  strip.ClearTo(black ); strip.Show();
  Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
  ESP.deepSleep(0);

}

void loop() {
}

Thingspeak code

Arduino
Use this sketch for Thingspeak
/*
  Data grab and deep sleep with button

  Created on: 10/10/2019

*/
#include <NeoPixelBus.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <ESP8266HTTPClient.h>
#include <Ticker.h>
Ticker ticker;
const uint16_t PixelCount = 2; //
const uint8_t PixelPin = 3;  //
#define colorSaturation 128
String mostSignificantDigit;
// Declare our NeoPixel strip object:
NeoPixelBus<NeoGrbFeature, NeoEsp8266Dma800KbpsMethod> strip(PixelCount);
//Declare he colours
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor white(colorSaturation);
RgbColor black(0);
RgbColor purple(246, 22, 246);
RgbColor orange(255, 137, 0);
RgbColor yellow(255, 239, 0);
RgbColor darkpurple(137,36,142);
RgbColor darkmag(158,6,2);

ESP8266WiFiMulti WiFiMulti;
int number_airindex ;
int numLedsToLight;



void tick()
{
  int state = digitalRead(BUILTIN_LED);  // get the current state of GPIO1 pin
  digitalWrite(BUILTIN_LED, !state);     // set pin to the opposite state
}

//sleep time
const int sleepSeconds_show = 1000 * 10; // 10 seconds


void setup() {
  Serial.begin(115200);
  strip.Begin();
  //  strip.setBrightness(40);
  strip.Show(); // Initialize all pixels to 'off'
  (red ); strip.Show();
  pinMode(BUILTIN_LED, OUTPUT);
  ticker.attach(0.5, tick);
  WiFiManager wifiManager;
  wifiManager.autoConnect();
  // Serial.setDebugOutput(true);
   Serial.setTimeout(2000);
  while (!Serial) { }
  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {

    Serial.printf("[SETUP] WAIT %d...", t);
    Serial.flush();
    strip.ClearTo(red ); strip.Show();
    delay(500);
    strip.ClearTo(black ); strip.Show();
    (red ); strip.Show();
    
  }
  // WiFiMulti.addAP("Learning_Lounge", "Z@q12wsx");
  //WiFiMulti.addAP("simon", "binh12345");
  // wait for WiFi connection
  strip.ClearTo(red); strip.Show();
  delay(1000);
  if ((WiFiMulti.run() == WL_CONNECTED)) {
    strip.ClearTo(white ); strip.Show();
    delay(1000);
    HTTPClient http;

    Serial.print("[HTTP] begin...");
    // configure traged server and url
    // get the AQI value
    //http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey
    http.begin("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey"); //HTTP
    Serial.println("[HTTP] begin...");
    Serial.println("[HTTP] GET...");
    // start connection and send HTTP header
    //http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey
    Serial.print("http://api.thingspeak.com/apps/thinghttp/send_request?api_key=yourKey");
    int httpCode = http.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d", httpCode);
      strip.ClearTo(purple); strip.Show();
      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        // Print the string value from Air visual
        Serial.println("Payload  ");
        Serial.println(payload);
        // Convert string to integer so that we can utilise the number
        number_airindex = payload.toInt();
        Serial.println(number_airindex);
        Serial.println();

        //Light the LEDs
        if (number_airindex >= 300) {
          // do Thing A
          strip.ClearTo(darkmag); strip.Show();
        }
        else if (number_airindex >= 200) {
          // do Thing B
          strip.ClearTo(darkpurple); strip.Show();
        }
        else if (number_airindex >= 150) {
          // do Thing B
          strip.ClearTo(red); strip.Show();
        }
        else if (number_airindex >= 100) {
          // do Thing B
          strip.ClearTo(orange); strip.Show();
        }
        else if (number_airindex >= 50) {
          // do Thing B
          strip.ClearTo(yellow); strip.Show();
        }
        else {
          // do Thing C
          strip.ClearTo(green); strip.Show();
        }
      }

    } else {
      Serial.printf("[HTTP] GET... failed, error: %s", http.errorToString(httpCode).c_str());
      strip.ClearTo(purple); strip.Show();
    }

    http.end();



    Serial.println("Here");
    delay(sleepSeconds_show);
    strip.ClearTo(black); strip.Show();
    number_airindex = 0;
    Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
    //WiFi.disconnect(true);
    ESP.deepSleep(0);
  }
}
void loop() {
}

Credits

simonhyde88
3 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.