· 6 years ago · Feb 15, 2020, 11:14 AM
1#include <FS.h> //this needs to be first, or it all crashes and burns...
2#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
3#include <Wire.h>
4#include <DNSServer.h>
5#include <ESP8266WebServer.h>
6#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
7#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
8
9#define TRIGGER_PIN 0
10
11const char* server = "api.thingspeak.com";
12char api_key[17] = "your api TOKEN";
13long t, h;
14String str;
15bool shouldSaveConfig = false;
16
17void saveConfigCallback () {
18 Serial.println("Should save config");
19 shouldSaveConfig = true;
20}
21
22client.print
23
24void setup() {
25 Serial.begin(115200);
26 str = (const char*) api_key;
27 Serial.println();
28 pinMode(TRIGGER_PIN, INPUT);
29
30 if (SPIFFS.begin()) {
31 Serial.println("mounted file system");
32 if (SPIFFS.exists("/config.json")) {
33 //file exists, reading and loading
34 Serial.println("reading config file");
35 File configFile = SPIFFS.open("/config.json", "r");
36 if (configFile) {
37 Serial.println("opened config file");
38 size_t size = configFile.size();
39 // Allocate a buffer to store contents of the file.
40 std::unique_ptr<char[]> buf(new char[size]);
41 configFile.readBytes(buf.get(), size);
42 DynamicJsonBuffer jsonBuffer;
43 JsonObject& json = jsonBuffer.parseObject(buf.get());
44 json.printTo(Serial);
45 if (json.success()) {
46 Serial.println("\nparsed json");
47 strcpy(api_key, json["api_key"]);
48 } else {
49 Serial.println("failed to load json config");
50 }
51 configFile.close();
52 }
53 }
54 } else {
55 Serial.println("failed to mount FS");
56 }
57
58 WiFi.mode(WIFI_STA);
59
60 WiFiManagerParameter custom_api_key("key", "api key", api_key, 17);
61
62 WiFiManager wifiManager;
63 wifiManager.setSaveConfigCallback(saveConfigCallback);
64 wifiManager.addParameter(&custom_api_key);
65
66 if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
67 Serial.println("failed to connect and hit timeout");
68 delay(3000);
69 ESP.reset();
70 delay(5000);
71 }
72
73 Serial.println("connected...yeey :)");
74 strcpy(api_key, custom_api_key.getValue());
75
76
77 if (shouldSaveConfig) {
78 Serial.println("saving config");
79 DynamicJsonBuffer jsonBuffer;
80 JsonObject& json = jsonBuffer.createObject();
81
82 json["api_key"] = api_key;
83
84 File configFile = SPIFFS.open("/config.json", "w");
85 if (!configFile) {
86 Serial.println("failed to open config file for writing");
87 }
88
89 json.printTo(Serial);
90 json.printTo(configFile);
91 configFile.close();
92 //end save
93 }
94
95 Serial.println(WiFi.localIP());
96
97}
98
99
100
101void loop() {
102 if ( digitalRead(TRIGGER_PIN) == LOW ) {
103 WiFi.mode(WIFI_AP_STA);
104 WiFi.softAP("Rsenso");
105 WiFiManager wifiManager;
106
107 if (!wifiManager.startConfigPortal()) {
108 Serial.println("failed to connect and hit timeout");
109 delay(3000);
110 ESP.reset();
111 delay(5000);
112 }
113 }
114
115
116 //--------------------------thingspeak-------------------------
117
118 Serial.println("thingspeak");
119
120
121 if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
122 String postStr = api_key;
123 postStr +="&field1=";
124 postStr += String(10);
125 postStr +="&field2=";
126 postStr += String(20);
127 Serial.println(postStr);
128 Serial.println("thingspeak post");
129
130 client.print("GET /update HTTP/1.1\n");
131 client.print("Host: api.thingspeak.com\n");
132 client.print("Connection: close\n");
133 client.print("X-THINGSPEAKAPIKEY: "+str+"\n");
134 client.print("Content-Type: application/x-www-form-urlencoded\n");
135 client.print("Content-Length: ");
136 client.print(postStr.length());
137 client.print("\n\n\n\n\n\n\n\n");
138 client.print(postStr);
139
140
141
142 }
143 client.stop();
144
145
146 // thingspeak needs minimum 15 sec delay between updates
147 delay(20000);
148}