Created
November 18, 2025 12:58
-
-
Save Robotto/c4fdbbb9cda7d3881a434fc1e2bdc9b1 to your computer and use it in GitHub Desktop.
IOTplotter example for ESP8266
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ESP8266WiFi.h> | |
| #include <ESP8266HTTPClient.h> | |
| #include <ArduinoJson.h> | |
| const uint16_t json_doc_size = 1024; | |
| #include <WiFiClient.h> | |
| #include <WiFiClientSecure.h> | |
| #include <SparkFun_Si7021_Breakout_Library.h> //Uses a simple temperature/humidity sensor. Link: https://www.sparkfun.com/sparkfun-humidity-and-temperature-sensor-breakout-si7021.html | |
| #include <Wire.h> | |
| #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager | |
| //GET YOUR OWN API KEY AND ENDPOINT FEED ADDRESS FROM: https://iotplotter.com/ | |
| uint32_t delayMS=60000; | |
| Weather sensor; | |
| void configModeCallback (WiFiManager *myWiFiManager) { | |
| Serial.println("Entered config mode"); | |
| Serial.println("AP: " + myWiFiManager->getConfigPortalSSID()); | |
| Serial.println("IP: " + WiFi.softAPIP().toString()); | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| sensor.begin(); | |
| WiFi.hostname("8266_IOTdevice"); | |
| //WiFiManager | |
| //Local intialization. Once its business is done, there is no need to keep it around | |
| WiFiManager wifiManager; | |
| Serial.println("Connecting to wifi.."); | |
| wifiManager.setAPCallback(configModeCallback); //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode | |
| wifiManager.setConnectTimeout(180); //try to connect to known wifis for a long time before defaulting to AP mode | |
| //fetches ssid and pass and tries to connect | |
| //if it does not connect it starts an access point with the specified name | |
| //and goes into a blocking loop awaiting configuration | |
| if (!wifiManager.autoConnect("iot_wifi_config")) { | |
| Serial.println("failed to connect and hit timeout"); | |
| ESP.restart(); //reset and try again, or maybe put it to deep sleep | |
| } | |
| Serial.println(); | |
| Serial.print("WiFi up!"); | |
| Serial.print(" IPv4: "); | |
| Serial.println(WiFi.localIP()); | |
| } | |
| float celsius, RH, vBatt; | |
| unsigned long lastEpoch; | |
| void getWeather() | |
| { | |
| RH = sensor.getRH(); | |
| celsius = sensor.getTemp(); | |
| } | |
| void loop() { | |
| getWeather(); | |
| readBatt(); | |
| iotplotter_call("GET_YOUR_OWN_API_PATH","GET_YOUR_OWN_API_KEY"); | |
| delay(delayMS); | |
| } | |
| /* | |
| * Small helper functions that creates a json from the data | |
| */ | |
| double round2(double value) { | |
| return (int)(value * 100 + 0.5) / 100.0; | |
| } | |
| void create_json(JsonObject &doc) { | |
| doc["Temperatur"][0]["value"] = round2(celsius); | |
| doc["Luftfugtighed"][0]["value"] = round2(RH); | |
| doc["Batteri"][0]["value"] = round2(vBatt); | |
| } | |
| // rounds a number to 2 decimal places | |
| // example: round(3.14159) -> 3.14 | |
| /* | |
| * This function implements the communication with the IOTPlotter service | |
| */ | |
| bool iotplotter_call(char* api_path, char* api_key) { | |
| WiFiClient client; | |
| HTTPClient httpClient; | |
| // Create the json needed for the service | |
| StaticJsonDocument<json_doc_size> doc; | |
| JsonObject object = doc.createNestedObject("data"); | |
| create_json(object); | |
| char json[json_doc_size]; | |
| serializeJson(doc, json); | |
| // Serial.println("IOTPlotter JSON"); | |
| Serial.println(json); | |
| // Send the data with the needed header settings | |
| String serviceURL = "http://iotplotter.com/api/v2/feed/" + String(api_path); | |
| httpClient.begin(client,serviceURL); | |
| httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded"); | |
| httpClient.addHeader("api-key", String(api_key)); | |
| int http_response_code = httpClient.POST(json); | |
| Serial.print(http_response_code); | |
| Serial.print(" "); | |
| Serial.println(httpClient.getString()); | |
| // Disconnect | |
| httpClient.end(); | |
| return http_response_code == 200; | |
| } | |
| void readBatt(){ | |
| //Do a bunch of ADC measurements and convert them to average Vbatt, append Vbatt to report | |
| unsigned long ADCSum = 0; | |
| for (int i=0; i<64; i++) ADCSum+=analogRead(A0); | |
| unsigned int ADCavg = ADCSum/64; | |
| vBatt = ((float)ADCavg*14.0/1023.0)-1.02; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment