[{"data":1,"prerenderedAt":44},["ShallowReactive",2],{"example-page-picow-mqtt":3},{"id":4,"info":5,"parts":6,"wired":14,"refs":15,"nets":16,"moreNets":38,"code":39},"picow-mqtt","The Pico W reads a DHT22 on GP15 and sends temperature and humidity to the virtual network's MQTT broker every 2 s (PubSubClient). The messages appear in the \"Network\" panel; a message \"on\" or \"off\" to the topic pico\u002Fled switches the onboard LED. Change the sensor's temperature and humidity in the inspector, even while the simulation runs.",[7,10,12],[8,9],"dht22",1,[11,9],"rpi_pico_w",[13,9],"wifi_router",false,{"A1":11,"U2":8},[17,25,31],{"name":18,"pins":19},"0",[20,23],[21,22],"A1","GND",[24,22],"U2",{"name":26,"pins":27},"3V3",[28,29],[21,26],[24,30],"VCC",{"name":32,"pins":33},"DHT",[34,36],[21,35],"GP15",[24,37],"DATA",0,{"file":40,"lang":41,"text":42,"lines":43,"more":14},"sketch.ino","cpp","\u002F\u002F Pico W + DHT22 (GP15): sends temperature and humidity to the MQTT broker every 2 s,\n\u002F\u002F the topic \"pico\u002Fled\" (on\u002Foff) switches the onboard LED. Messages are in the \"Network\" panel.\n#include \u003CWiFi.h>\n#include \u003CPubSubClient.h>\n#include \u003CDHT.h>\n\nconst char* SSID = \"CircuitLab\";\nconst char* PASS = \"secret123\";\nWiFiClient net;\nPubSubClient mqtt(net);\nDHT dht(15, DHT22);\n\nvoid onMessage(char* topic, byte* payload, unsigned int len) {\n  String msg; for (unsigned int i = 0; i \u003C len; i++) msg += (char)payload[i];\n  Serial.print(\"MQTT \"); Serial.print(topic); Serial.print(\" = \"); Serial.println(msg);\n  if (String(topic) == \"pico\u002Fled\") digitalWrite(LED_BUILTIN, msg == \"on\" ? HIGH : LOW);\n}\n\nvoid connectMqtt() {\n  while (!mqtt.connected()) {\n    if (mqtt.connect(\"pico-w-climate\")) { mqtt.subscribe(\"pico\u002Fled\"); Serial.println(\"MQTT connected\"); }\n    else delay(500);\n  }\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(LED_BUILTIN, OUTPUT);\n  dht.begin();\n  WiFi.begin(SSID, PASS);\n  while (WiFi.status() != WL_CONNECTED) delay(100);\n  Serial.print(\"IP: \"); Serial.println(WiFi.localIP());\n  mqtt.setServer(\"broker.lab\", 1883);\n  mqtt.setCallback(onMessage);\n}\n\nuint32_t last = 0;\nvoid loop() {\n  connectMqtt();\n  mqtt.loop();\n  if (millis() - last >= 2000) {\n    last = millis();\n    float t = dht.readTemperature(), h = dht.readHumidity();\n    if (!isnan(t)) {\n      mqtt.publish(\"pico\u002Fclimate\u002Ftemperature\", String(t, 1).c_str());\n      mqtt.publish(\"pico\u002Fclimate\u002Fhumidity\", String(h, 0).c_str());\n      Serial.print(t, 1); Serial.print(\" C  \"); Serial.print(h, 0); Serial.println(\" %\");\n    }\n  }\n}",50,1796304401012]