Air quality data from aqicn.org


---------------aqi.sh-----------------------------------
#!/bin/bash

#  to solve the error: sed -e expression #1, char 18: Invalid collation character has to do with the locale setting
export LC_ALL=C

URL1="http://api.waqi.info/feed/chiang mai/?token=a198ff30c2a0be0ba6874bc44dcf1a9c4a40d219"
URL2="https://thethaiger.com/feed"
URL3="https://www.volkskrant.nl/voorpagina/rss.xml"
URL4="https://rss.nytimes.com/services/xml/rss/nyt/World.xml"
OUTPUT1=/usr/local/data/output.aqi
OUTPUT2=/usr/local/data/output.tgr
OUTPUT3=/usr/local/data/output.vlk
OUTPUT4=/usr/local/data/output.rss

while true
do
/usr/bin/curl --silent "$URL1" | /usr/bin/sed  's/,/\n/g' > "$OUTPUT1"
sleep 4
/usr/local/bin/read_aqi
sleep 30
/usr/local/bin/rpi_shut

/usr/bin/curl --silent "$URL2" | /usr/bin/grep "<title>" | /usr/bin/tail -n +3 | /usr/bin/awk -F ">|<"  '{ print $3}' | /usr/bin/sed  's/[\d128-\d255]//g' >  "$OUTPUT2"
sleep 4
/usr/local/bin/read_rss $OUTPUT2
sleep 1
/usr/local/bin/rpi_shut

/usr/local/bin/read_aqi
sleep 30

/usr/bin/curl --silent "$URL3" | /usr/bin/grep "<title>" | /usr/bin/tail -n +3 | /usr/bin/awk -F ">|<"  '{ print $3}' | /usr/bin/sed  's/[\d128-\d255]//g' > "$OUTPUT3"
sleep 4
/usr/local/bin/read_rss $OUTPUT3
sleep 1
/usr/local/bin/rpi_shut

/usr/local/bin/read_aqi
sleep 30
/usr/local/bin/rpi_shut

/usr/bin/curl --silent "$URL4" | /usr/bin/grep "<title>" | /usr/bin/tail -n +3 | /usr/bin/awk -F "<|>"  '{ print $3}' | /usr/bin/sed  's/[\d128-\d255]//g'  >  "$OUTPUT4"
sleep 4

/usr/local/bin/read_rss $OUTPUT4
sleep 1
/usr/local/bin/rpi_shut


done


---------------read_aqi.c--------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <wiringPi.h>
#include <lcd.h>
// Global lcd handle:
static int lcdHandle ;

char line[4][20]={ {"\0"}, {"\0"}, {"\0"}, {"\0"} };
int pol; //pollution

void cutString(char *strptr) {

char *ptr;
char *ptr2;

        if (strstr(strptr,"iaqi") != NULL) {
                if ((ptr = strstr(strptr,"\"v\":")) != NULL) {
                        ptr2 = (strstr(ptr,"}"));
                        *(ptr2) = '\0';
                        //printf("Humid:\t%s\n", ptr+4);
                        strcpy(line[2],"Humid: ");
                        strcat(line[2], ptr+4);

                }
        } else if (strstr(strptr,"pm10") != NULL) {
                if ((ptr = strstr(strptr,"\"v\":")) != NULL) {
                        ptr2 = (strstr(ptr,"}"));
                        *(ptr2) = '\0';
                        //printf("PM10:\t%s\n", ptr+4);
                        strcpy(line[1],"PM10: ");
                        strcat(line[1], ptr+4);

                }
        } else if (strstr(strptr,"pm25") != NULL) {
                if ((ptr = strstr(strptr,"\"v\":")) != NULL) {
                        ptr2 = (strstr(ptr,"}"));
                        *(ptr2) = '\0';
                        //printf("PM2.5:\t%s\n", ptr+4);
                        strcat(line[1]," PM2.5: ");
                        strcat(line[1], ptr+4);
                        pol = atoi(ptr+4);
                }
        } else if (strstr(strptr,"time") != NULL) {
                if ((ptr = strstr(strptr,"\"s\":")) != NULL) {
                        ptr2 = strstr(ptr,":0");
                        *(ptr2+3) = '\0';
                        strcpy(line[3], ptr+5);
                }
        } else if (strstr(strptr,"t") != NULL) {
                if ((ptr = strstr(strptr,"\"v\":")) != NULL) {
                        ptr2 = (strstr(ptr,"}"));
                        *(ptr2) = '\0';
                        //printf("Temp:\t%s\n", ptr+4);
                        strcat(line[2]," Temp: ");
                        strcat(line[2], ptr+4);
                }
        }
}

int main(void) {

FILE *fp;
char myString[100];
  wiringPiSetup () ;

    lcdHandle = lcdInit (4, 20, 8, 11,10, 0,1,2,3,4,5,6,7) ;
    if (lcdHandle < 0)
    {
       fprintf (stderr,"lcdInit failed\n") ;
       return -1 ;
  }
    lcdClear(lcdHandle);

        fp = fopen("/usr/local/data/output.aqi", "r");
        while(fgets(myString, 100, fp)) {
                cutString(myString);
        }
        fclose(fp);
        if (pol < 51) {
                strcpy(line[0], "CNX  Good          ");
        } else if (pol < 101) {
                strcpy(line[0], "CNX  Moderate      ");
        } else if (pol < 151) {
                strcpy(line[0], "CNX  Unhealthy fsg ");
        } else if (pol < 201) {
                strcpy(line[0], "CNX  Unhealthy     ");
        } else if (pol < 301) {
                strcpy(line[0], "CNX  Very Unhealthy");
        } else if (pol > 300)  {
                strcpy(line[0], "CNX  Hazardous     ");
        }

        lcdPosition (lcdHandle, 0, 0) ; lcdPuts (lcdHandle, line[0]) ;
        lcdPosition (lcdHandle, 0, 1) ; lcdPuts (lcdHandle, line[1]) ;
        lcdPosition (lcdHandle, 0, 2) ; lcdPuts (lcdHandle, line[2]) ;
        lcdPosition (lcdHandle, 0, 3) ; lcdPuts (lcdHandle, line[3]) ;


return 0;
}