Node red obtains real-time weather data

Time:2022-5-27

Step 1: Download and install “weather” in the node management, as shown in the following figure:

Node red obtains real-time weather data

image.png

Step 2: find “weather” in the sidebar and drag it into the process, as shown in the following figure:

Node red obtains real-time weather data

image.png

Step 3: drag “function” into the process and write the following code, as shown in the following figure:

let code;
code = msg.payload.current.weather;
        switch(code) {
        case '0':
            Status ='sunny '
            break;
        case '1':
            Status ='cloudy '
            break;
        case '2':
            Status ='negative '
            break;
        case '3':
            Status ='shower '
            break;
        case '4':
            Status ='thunderstorm '
            break;
        case '5':
            Status ='thunderstorm with hail '
            break;
        case '6':
            Status ='sleet '
            break;
        case '7':
            Status ='Light rain '
            break;
        case '8':
            Status ='moderate rain '
            break;
        case '9':
            Status ='heavy rain '
            break;
        case '10':
            Status ='rainstorm '
            break;
        case '11':
            Status ='heavy rain '
            break;
        case '12':
            Status ='torrential rain '
            break;
        case '13':
            Status ='snow shower '
            break;
        case '14':
            Status ='Light snow '
            break;
        case '15':
            Status ='moderate snow '
            break;
        case '16':
            Status ='heavy snow '
            break;
        case '17':
            Status ='blizzard '
            break;
        case '18':
            Status ='fog '
            break;
        case '19':
            Status ='freezing rain '
            break;
        case '20':
            Status ='sandstorm '
            break;
        case '21':
            Status ='Light rain to moderate rain '
            break;
        case '22':
            Status = 'moderate rain to heavy rain'
            break;
        case '23':
            Status ='heavy rain to heavy rain '
            break;
        case '24':
            Status = "rainstorm turns to heavy rainstorm"
            break;
        case '25':
            Status ='heavy rain to heavy rain '
            break;
        case '26':
            Status ='Light snow to moderate snow '
            break;
        case '27':
            Status = 'moderate snow to heavy snow'
            break;
        case '28':
            Status ='heavy snow to Blizzard '
            break;
        case '29':
            Status ='floating and sinking '
            break;
        case '30':
            Status ='sand blowing '
            break;
        case '31':
            Status ='strong sandstorm '
            break;
        case '32':
            Status ='squall '
            break;
        case '33':
            Status ='tornado '
            break;
        case '34':
            Status ='If snow blows high '
            break;
        case '35':
            Status ='mist '
            break;
        case '53':
            Status ='haze '
            break;
        case '99':
            Status ='unknown '
            break;
        default:
            Status ='No data '
            break;
        }
return msg;

Step 4: drag “debug” into the process to monitor the debugging window, as shown in the following figure:

Node red obtains real-time weather data

image.png

Step 5: drag “link in” and “Linkout” into the process to link input and output, as shown in the following figure:

Node red obtains real-time weather data

image.png

Step 6: according to the data to be displayed, drag “function” into the process to mark the corresponding functions. I have done a comprehensive job here, and the codes in sequence are as follows:
1. Get weather type:

var result = {
    payload: ""
}

var tq = msg.payload.current.weather;
switch(tq) {
case '0':
    result. Payload ='sunny '
    break;
case '1':
    result. Payload ='cloudy '
    break;
case '2':
    result. Payload ='negative '
    break;
case '3':
    result. Payload ='shower '
    break;
case '4':
    result. Payload ='thunderstorm '
    break;
case '5':
    result. Payload ='thunderstorm with hail '
    break;
case '6':
    result. Payload ='sleet '
    break;
case '7':
    result. Payload ='Light rain '
    break;
case '8':
    result. Payload ='moderate rain '
    break;
case '9':
    result. Payload ='heavy rain '
    break;
case '10':
    result. Payload ='rainstorm '
    break;
case '11':
    result. Payload ='heavy rain '
    break;
case '12':
    result. Payload ='torrential rain '
    break;
case '13':
    result. Payload ='snow shower '
    break;
case '14':
    result. Payload ='Light snow '
    break;
case '15':
    result. Payload ='moderate snow '
    break;
case '16':
    result. Payload ='heavy snow '
    break;
case '17':
    result. Payload ='blizzard '
    break;
case '18':
    result. Payload ='fog '
    break;
case '19':
    result. Payload ='freezing rain '
    break;
case '20':
    result. Payload ='sandstorm '
    break;
case '21':
    result. Payload ='Light rain to moderate rain '
    break;
case '22':
    result. Payload ='moderate rain to heavy rain '
    break;
case '23':
    result. Payload ='heavy rain to heavy rain '
    break;
case '24':
    result. Payload = 'rainstorm to heavy rainstorm'
    break;
case '25':
    result. Payload ='heavy rain to heavy rain '
    break;
case '26':
    result. Payload ='Light snow to moderate snow '
    break;
case '27':
    result. Payload ='moderate snow to heavy snow '
    break;
case '28':
    result. Payload ='heavy snow to Blizzard '
    break;
case '29':
    result. Payload ='floating and sinking '
    break;
case '30':
    result. Payload ='sand blowing '
    break;
case '31':
    result. Payload ='strong sandstorm '
    break;
case '32':
    result. Payload ='squall '
    break;
case '33':
    result. Payload ='tornado '
    break;
case '34':
    result. Payload ='If snow blows high '
    break;
case '35':
    result. Payload ='mist '
    break;
case '53':
    result. Payload ='haze '
    break;
case '99':
    result. Payload ='unknown '
    break;
default:
    result. Payload ='No data '
    break;
}

return result;

2. Obtain body temperature:

var result = {
    payload: msg.payload.current.feelsLike.value
}
return result; 

3. Obtain the current temperature:

var result = {
    payload: msg.payload.current.temperature.value
}
return result; 

4. Obtain the current humidity:

var result = {
    payload: msg.payload.current.humidity.value
}
return result;

5. Obtain the current air pressure:

var result = {
    payload: msg.payload.current.pressure.value
}
return result;

6. Obtain the current wind speed:

var result = {
    payload: msg.payload.current.wind.speed.value
}
return result;

7. Obtain the current air quality:

var result = {
    payload: msg.payload.aqi.aqi
}
return result;

8. Obtain the current PM2.5:

var result = {
    payload: msg.payload.aqi.pm25
}
return result;

9. Obtain the temperature status in the next 14 days:

var payload = [{
    "Series": ["minimum temperature", "maximum temperature"],
    "data": [
//        [{ "x": 1504029632890, "y": 50 },
//         { "x": 1504029636001, "y": 40 },
//         { "x": 1504029638656, "y": 20 },
//         { "x": 1504029640666, "y": 20 }
//        ],
//        [{ "x": 1504029633514, "y": 6 },
//         { "x": 1504029636622, "y": 7 },
//         { "x": 1504029639539, "y": 6 }
//        ]
    ],
    "labels": []
}];
var lTemp = [];
var hTemp = [];

var values = msg.payload.forecastDaily.temperature.value;
var date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
var x = date.getTime();
for(var i in values) {
    lTemp.push({
        "x": x,
        "y": values[i]['to']
    });
    hTemp.push({
        "x": x,
        "y": values[i]['from']
    });
    x = new Date(x + 24 * 60 * 60 * 1000).getTime();
}
payload[0].data.push(lTemp);
payload[0].data.push(hTemp);

return {
    'payload': payload
};

10. Obtain PM10:

var result = {
    payload: msg.payload.aqi.pm10
}
return result;

11. Obtain carbon monoxide CO:

var result = {
    payload: msg.payload.aqi.co
}
return result;

12: Obtain nitrogen dioxide NO2:

var result = {
    payload: msg.payload.aqi.co
}
return result;

13: Obtain ozone O3:

var result = {
    payload: msg.payload.aqi.o3
}
return result;

14: Capture SO2:

var result = {
    payload: msg.payload.aqi.so2
}
return result;

15: Data source:

var result = {
    payload: msg.payload.aqi.src
}
return result;

Step 7: if you want to further explain some obtained data, you can refer to some information in the debugging window, as shown in the following figure:

Node red obtains real-time weather data

image.png

Step 8: link the corresponding modules in the process, as shown in the following figure:

Node red obtains real-time weather data

image.png

Step 9: enter the UI interface to see the effect. Some specific presentation methods can also be adjusted according to your actual needs. I have made the following figure for reference:

Node red obtains real-time weather data

image.png

The above is only some implementation operations of many materials on the personal reference network.

Recommended Today

(C#) Listening to the clipboard

public partial class Form1 : Form { public Form1() { InitializeComponent(); AddClipboardFormatListener(this.Handle); } protected override void WndProc(ref Message m) { if (m.Msg == WM_CLIPBOARDUPDATE) { Console.WriteLine("Clipboard content changed"); } base.WndProc(ref m); } //——————— public const int WM_CLIPBOARDUPDATE = 0x031D; [DllImport(“user32.dll”, SetLastError = true)] public static extern bool AddClipboardFormatListener(IntPtr hWnd); [DllImport(“user32.dll”, SetLastError = true)] public static […]