code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#include <Wire.h> #include <SPI.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #include <PubSubClient.h> #include <ESP8266WiFi.h> #define WIFI_AP "" #define WIFI_PASSWORD "" #define TOKEN "" char thingsboardServer[] = ""; WiFiClient wifiClient; Adafruit_BME280 bme; PubSubClient client(wifiClient); int status = WL_IDLE_STATUS; unsigned long lastSend; void setup() { Serial.begin(115200); bme.begin(); delay(10); InitWiFi(); client.setServer( thingsboardServer, 1883 ); lastSend = 0; } void loop() { if ( !client.connected() ) { reconnect(); } if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds getAndSendData(); lastSend = millis(); } client.loop(); } void getAndSendData() { Serial.print("getAndSendData: "); float h = bme.readHumidity(); float t = bme.readTemperature(); float p = bme.readPressure()*0.007500617; String temperature = String(t); String humidity = String(h); String pressure = String(p); // Just debug messages Serial.print( temperature ); Serial.print( ", " ); Serial.print( pressure ); Serial.print( ", " ); Serial.println( humidity ); // Prepare a JSON payload string String payload = "{"; payload += "\"temperature\":"; payload += temperature; payload += ","; payload += "\"pressure\":"; payload += pressure; payload += ","; payload += "\"humidity\":"; payload += humidity; payload += "}"; char attributes[100]; payload.toCharArray( attributes, 100 ); client.publish( "v1/devices/me/telemetry", attributes ); Serial.println( attributes ); } void InitWiFi() { Serial.println("Connecting to AP ..."); // attempt to connect to WiFi network WiFi.begin(WIFI_AP, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected to AP"); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { status = WiFi.status(); if ( status != WL_CONNECTED) { WiFi.begin(WIFI_AP, WIFI_PASSWORD); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("Connected to AP"); } Serial.print("Connecting to ThingsBoard node ..."); // Attempt to connect (clientId, username, password) if ( client.connect("ESP8266 Device", TOKEN, NULL) ) { Serial.println( "[DONE]" ); } else { Serial.print( "[FAILED] [ rc = " ); Serial.print( client.state() ); Serial.println( " : retrying in 5 seconds]" ); // Wait 5 seconds before retrying delay( 5000 ); } } } |
[collapse]