· 6 years ago · Sep 09, 2019, 05:12 PM
1#include <SPI.h>
2#include <Wire.h>
3#include <Adafruit_GFX.h>
4#include <Adafruit_SSD1306.h>
5#include "DHTesp.h"
6#include<SoftwareSerial.h>
7
8#define SCREEN_WIDTH 128
9#define SCREEN_HEIGHT 64
10
11
12#define OLED_MOSI 9
13#define OLED_CLK 10
14#define OLED_DC 11
15#define OLED_CS 12
16#define OLED_RESET 13
17Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
18 OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
19
20#define RX 2
21#define TX 3
22
23
24#define DEBUG true
25String mySSID = "justpixit"; // WiFi SSID
26String myPWD = "Pixit123"; // WiFi Password
27String myAPI = "72WX3N4E9E2D1N9T"; // API Key
28String myHOST = "api.thingspeak.com";
29String myPORT = "80";
30String myFIELD_1 = "field1";
31String myFIELD_2 = "field2";
32String myFIELD_3 = "field3";
33
34unsigned long previousMillis = 0;
35
36const long interval = 15; // 15 sec interval at which to send data to thingspeak
37
38SoftwareSerial esp8266(3,2);
39
40int moisture;
41int percentage;
42int analogPin = A0;
43int map_low = 1000;
44int map_high = 250;
45int screen_type = 1;
46
47 float h,t;
48
49/***********************************************/
50
51#define LOGO_HEIGHT 64
52#define LOGO_WIDTH 128
53#define DHT11_PIN 7 //DHT connected to pin 7
54
55#define DHTTYPE DHT11 // DHT 11
56DHT dht(DHT11_PIN, DHTTYPE);
57
58static const unsigned char PROGMEM logo_bmp[] =
59{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x07, 0x00, 0x40, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x0D, 0x80, 0x40, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x08, 0x00, 0x40, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x0C, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x10, 0x00, 0x06, 0x00, 0x1C, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x0F, 0x00, 0x04, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x07, 0x00, 0x09, 0x00, 0x04, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0D, 0x80, 0x0F, 0x00, 0x04, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x08, 0x9C, 0x86, 0x00, 0x04, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0D, 0x84, 0x80, 0x00, 0x04, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x07, 0x04, 0x80, 0x00, 0x0C, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x04, 0x80, 0x00, 0x08, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x06, 0x80, 0x00, 0x38, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x03, 0x80, 0x00, 0x60, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x01, 0x80, 0x00, 0x40, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x60, 0x01, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0xC0, 0x03, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00, 0x3F, 0x00, 0x00, 0x00,
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x61, 0x80, 0x00, 0x00,
101 0x08, 0x42, 0x11, 0xFF, 0xFB, 0xE7, 0xC3, 0xC2, 0x10, 0xC0, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x00,
102 0x08, 0x42, 0x28, 0x20, 0x42, 0x04, 0x04, 0x22, 0x20, 0x60, 0x00, 0x01, 0x80, 0x70, 0x00, 0x00,
103 0x08, 0xA2, 0x28, 0x20, 0x42, 0x04, 0x08, 0x12, 0x40, 0x18, 0x00, 0x03, 0x00, 0x18, 0x00, 0x00,
104 0x04, 0xA4, 0x44, 0x20, 0x42, 0x04, 0x08, 0x12, 0x80, 0x0C, 0x00, 0x06, 0x00, 0x08, 0x00, 0x00,
105 0x04, 0xA4, 0x44, 0x20, 0x43, 0xC7, 0xC8, 0x13, 0x00, 0x07, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x00,
106 0x05, 0x14, 0x82, 0x20, 0x42, 0x04, 0x08, 0x12, 0x80, 0x01, 0xC0, 0x08, 0x00, 0x06, 0x00, 0x00,
107 0x03, 0x18, 0xFE, 0x20, 0x42, 0x04, 0x08, 0x12, 0x40, 0x00, 0x7F, 0xFC, 0x00, 0x02, 0x00, 0x00,
108 0x02, 0x09, 0x01, 0x20, 0x42, 0x04, 0x04, 0x22, 0x20, 0x00, 0x00, 0x0C, 0x00, 0x03, 0x00, 0x00,
109 0x02, 0x09, 0x01, 0x20, 0x43, 0xE4, 0x03, 0xC2, 0x10, 0x00, 0x00, 0x18, 0x00, 0x01, 0x80, 0x00,
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x10, 0xC0, 0x00,
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC8, 0x00, 0x10, 0x40, 0x00,
112 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x88, 0x00, 0x10, 0x60, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x10, 0x00, 0x18, 0x20, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x30, 0x00, 0x0C, 0x20, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x20, 0x00, 0x04, 0x20, 0x00,
116 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x60, 0x00, 0x04, 0x20, 0x00,
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x40, 0x00, 0x04, 0x30, 0x00,
118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x40, 0x00, 0x0C, 0x10, 0x00,
119 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x40, 0x00, 0x18, 0x10, 0x00,
120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x40, 0x00, 0x10, 0x10, 0x00,
121 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x40, 0x00, 0x10, 0x10, 0x00,
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, 0x00, 0x10, 0x10, 0x00
123};
124
125void setup() {
126 Serial.begin(9600);
127
128 esp8266.begin(115200);
129
130 espData("AT+RST", 1000, DEBUG); //Reset the ESP8266 module
131 espData("AT+CWMODE=1", 1000, DEBUG); //Set the ESP mode as station mode
132 espData("AT+CWJAP=\""+ mySSID +"\",\""+ myPWD +"\"", 1000, DEBUG); //Connect to WiFi network
133 /*while(!esp.find("OK"))
134 {
135 //Wait for connection
136 }*/
137 delay(1000);
138
139
140 if (!display.begin(SSD1306_SWITCHCAPVCC)) {
141 Serial.println(F("SSD1306 allocation failed"));
142 for (;;);
143 }
144
145 display.display();
146 delay(2000);
147
148
149 display.clearDisplay();
150}
151
152void loop() {
153
154 h = dht.readHumidity();
155 // Read temperature as Celsius (the default)
156 t = dht.readTemperature();
157
158 moisture = analogRead(analogPin);
159 percentage = map(moisture, map_low, map_high, 0, 100);
160
161 // printen van de waardes in serieele monitor
162 Serial.print("Temperature = ");
163 Serial.println(t, 0);
164 Serial.print("Humidity = ");
165 Serial.println(h, 0);
166 Serial.print("Moisture = ");
167 Serial.println(percentage);
168
169 switch (screen_type) {
170 case 1: testscrolltext();
171 case 2: testdrawbitmap();
172 }
173
174 if (screen_type == 1) {
175 screen_type = 2;
176 } else {
177 screen_type = 1;
178 }
179 delay(500);
180
181
182 unsigned long currentMillis = millis();
183
184 if (currentMillis - previousMillis >= (interval*1000)) {
185 previousMillis = currentMillis;
186
187 String sendData = "GET /update?api_key="+ myAPI +"&"+ myFIELD_1 +"="+String(t)+"&"+ myFIELD_2 +"="+String(h)+"&"+ myFIELD_3 +"="+String(percentage);
188 espData("AT+CIPMUX=1", 1000, DEBUG); //Allow multiple connections
189 espData("AT+CIPSTART=0,\"TCP\",\""+ myHOST +"\","+ myPORT, 1000, DEBUG);
190 espData("AT+CIPSEND=0," +String(sendData.length()+4),1000,DEBUG);
191 esp8266.find(">");
192 esp8266.println(sendData);
193 Serial.print("Value to be sent: ");
194 Serial.println(t);
195 Serial.println(h);
196 Serial.println(" ");
197
198
199 espData("AT+CIPCLOSE=0",1000,DEBUG);
200 }
201
202
203
204}
205
206void testscrolltext(void) {
207 display.clearDisplay();
208
209 display.setTextSize(2); // Draw 2X-scale text
210 display.setTextColor(WHITE);
211 display.setCursor(5, 0);
212 //display.println(F("scroll"));
213 display.print("TEMP ");
214 display.print(t, 0);
215 display.println(" C");
216 display.setCursor(5, 20);
217 display.print("HUMI ");
218 display.print(h, 0);
219 display.println(" %");
220 display.setCursor(5, 40);
221 display.print("MOIS ");
222 display.print(percentage);
223 display.println(" %");
224 display.display(); // Show initial text
225 delay(5000);
226}
227
228void testdrawbitmap(void) {
229 display.clearDisplay();
230
231 display.drawBitmap(
232 (display.width() - LOGO_WIDTH ) / 2,
233 (display.height() - LOGO_HEIGHT) / 2,
234 logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
235 display.display();
236 delay(1000);
237}
238
239
240 String espData(String command, const int timeout, boolean debug)
241{
242 Serial.print("AT Command ==> ");
243 Serial.print(command);
244 Serial.println(" ");
245
246 String response = "";
247 esp8266.println(command);
248 long int time = millis();
249 while ( (time + timeout) > millis())
250 {
251 while (esp8266.available())
252 {
253 char c = esp8266.read();
254 response += c;
255 }
256 }
257 if (debug)
258 {
259 //Serial.print(response);
260 }
261 return response;
262}