|
| 1 | +/* |
| 2 | +Version 1.0 supports OLED display's with either SDD1306 or with SH1106 controller |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <ESP8266WiFi.h> |
| 6 | +#include <ESP8266WebServer.h> |
| 7 | +#include <Wire.h> |
| 8 | +#include "font.h" |
| 9 | +//#define offset 0x00 // SDD1306 // offset=0 for SSD1306 controller |
| 10 | +#define offset 0x02 // SH1106 // offset=2 for SH1106 controller |
| 11 | +#define OLED_address 0x3c // all the OLED's I have seen have this address |
| 12 | +#define SSID "........" // insert your SSID |
| 13 | +#define PASS "........" // insert your password |
| 14 | +// ******************* String form to sent to the client-browser ************************************ |
| 15 | +String form = |
| 16 | + "<p>" |
| 17 | + "<center>" |
| 18 | + "<h1>Talk to me :-)</h1>" |
| 19 | + "<img src='http://i.imgur.com/qu8lDEu.jpg'>" |
| 20 | + "<form action='msg'><p>Wassup? <input type='text' name='msg' size=50 autofocus> <input type='submit' value='Submit'></form>" |
| 21 | + "</center>"; |
| 22 | + |
| 23 | +ESP8266WebServer server(80); // HTTP server will listen at port 80 |
| 24 | + long period; |
| 25 | + |
| 26 | +/* |
| 27 | + handles the messages coming from the webbrowser, restores a few special characters and |
| 28 | + constructs the strings that can be sent to the oled display |
| 29 | +*/ |
| 30 | +void handle_msg() { |
| 31 | + clear_display(); // clears oled |
| 32 | + |
| 33 | + server.send(200, "text/html", form); // Send same page so they can send another msg |
| 34 | + |
| 35 | + // Display msg on Oled |
| 36 | + String msg = server.arg("msg"); |
| 37 | + Serial.println(msg); |
| 38 | + String decodedMsg = msg; |
| 39 | + // Restore special characters that are misformed to %char by the client browser |
| 40 | + decodedMsg.replace("+", " "); |
| 41 | + decodedMsg.replace("%21", "!"); |
| 42 | + decodedMsg.replace("%22", ""); |
| 43 | + decodedMsg.replace("%23", "#"); |
| 44 | + decodedMsg.replace("%24", "$"); |
| 45 | + decodedMsg.replace("%25", "%"); |
| 46 | + decodedMsg.replace("%26", "&"); |
| 47 | + decodedMsg.replace("%27", "'"); |
| 48 | + decodedMsg.replace("%28", "("); |
| 49 | + decodedMsg.replace("%29", ")"); |
| 50 | + decodedMsg.replace("%2A", "*"); |
| 51 | + decodedMsg.replace("%2B", "+"); |
| 52 | + decodedMsg.replace("%2C", ","); |
| 53 | + decodedMsg.replace("%2F", "/"); |
| 54 | + decodedMsg.replace("%3A", ":"); |
| 55 | + decodedMsg.replace("%3B", ";"); |
| 56 | + decodedMsg.replace("%3C", "<"); |
| 57 | + decodedMsg.replace("%3D", "="); |
| 58 | + decodedMsg.replace("%3E", ">"); |
| 59 | + decodedMsg.replace("%3F", "?"); |
| 60 | + decodedMsg.replace("%40", "@"); |
| 61 | + //Serial.println(decodedMsg); // print original string to monitor |
| 62 | + unsigned int lengte = decodedMsg.length(); // length of received message |
| 63 | + for (int i=0;i<lengte;i++) // prints up to 8 rows of 16 characters. |
| 64 | + { |
| 65 | + char c = decodedMsg[i]; |
| 66 | + Serial.print(c); //decodedMsg[i]); |
| 67 | + if (i<16) {sendCharXY(c,0,i);} |
| 68 | + else if (i<32) {sendCharXY(c,1,i-16);} |
| 69 | + else if (i<48) {sendCharXY(c,2,i-32);} |
| 70 | + else if (i<64) {sendCharXY(c,3,i-48);} |
| 71 | + else if (i<80) {sendCharXY(c,4,i-64);} |
| 72 | + else if (i<96) {sendCharXY(c,5,i-80);} |
| 73 | + else if (i<112) {sendCharXY(c,6,i-96);} |
| 74 | + else if (i<128) {sendCharXY(c,7,i-112);} |
| 75 | + |
| 76 | + } |
| 77 | + //Serial.println(' '); // new line in monitor |
| 78 | +} |
| 79 | + |
| 80 | +void setup(void) { |
| 81 | +//ESP.wdtDisable(); // used to debug, disable wachdog timer, |
| 82 | + Serial.begin(115200); // full speed to monitor |
| 83 | + Wire.begin(0,2); // Initialize I2C and OLED Display |
| 84 | + init_OLED(); // |
| 85 | + reset_display(); |
| 86 | + WiFi.begin(SSID, PASS); // Connect to WiFi network |
| 87 | + while (WiFi.status() != WL_CONNECTED) { // Wait for connection |
| 88 | + delay(500); |
| 89 | + Serial.print("."); |
| 90 | + } |
| 91 | + // Set up the endpoints for HTTP server, Endpoints can be written as inline functions: |
| 92 | + server.on("/", []() { |
| 93 | + server.send(200, "text/html", form); |
| 94 | + }); |
| 95 | + server.on("/msg", handle_msg); // And as regular external functions: |
| 96 | + server.begin(); // Start the server |
| 97 | + clear_display(); |
| 98 | + |
| 99 | + Serial.print("SSID : "); // prints SSID in monitor |
| 100 | + Serial.println(SSID); // to monitor |
| 101 | + sendStrXY("SSID :" ,0,0); sendStrXY(SSID,0,7); // prints SSID on OLED |
| 102 | + |
| 103 | + char result[16]; |
| 104 | + sprintf(result, "%3d.%3d.%3d.%3d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]); |
| 105 | + Serial.println();Serial.println(result); |
| 106 | + sendStrXY(result,2,0); |
| 107 | + |
| 108 | + Serial.println("WebServer ready! "); |
| 109 | + sendStrXY("WebServer ready!",4,0); // OLED first message |
| 110 | + Serial.println(WiFi.localIP()); // Serial monitor prints localIP |
| 111 | + Serial.print(analogRead(A0)); |
| 112 | + int test = 13; |
| 113 | + pinMode(test,OUTPUT); |
| 114 | + digitalWrite(test,HIGH); |
| 115 | + delay(1000); |
| 116 | + digitalWrite(test,LOW); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +void loop(void) { |
| 121 | + server.handleClient(); // checks for incoming messages |
| 122 | +} |
| 123 | + |
| 124 | +//==========================================================// |
| 125 | +// Resets display depending on the actual mode. |
| 126 | +static void reset_display(void) |
| 127 | +{ |
| 128 | + displayOff(); |
| 129 | + clear_display(); |
| 130 | + displayOn(); |
| 131 | +} |
| 132 | + |
| 133 | +//==========================================================// |
| 134 | +// Turns display on. |
| 135 | +void displayOn(void) |
| 136 | +{ |
| 137 | + sendcommand(0xaf); //display on |
| 138 | +} |
| 139 | + |
| 140 | +//==========================================================// |
| 141 | +// Turns display off. |
| 142 | +void displayOff(void) |
| 143 | +{ |
| 144 | + sendcommand(0xae); //display off |
| 145 | +} |
| 146 | + |
| 147 | +//==========================================================// |
| 148 | +// Clears the display by sendind 0 to all the screen map. |
| 149 | +static void clear_display(void) |
| 150 | +{ |
| 151 | + unsigned char i,k; |
| 152 | + for(k=0;k<8;k++) |
| 153 | + { |
| 154 | + setXY(k,0); |
| 155 | + { |
| 156 | + for(i=0;i<(128 + 2 * offset);i++) //locate all COL |
| 157 | + { |
| 158 | + SendChar(0); //clear all COL |
| 159 | + //delay(10); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +//==========================================================// |
| 166 | +// Actually this sends a byte, not a char to draw in the display. |
| 167 | +// Display's chars uses 8 byte font the small ones and 96 bytes |
| 168 | +// for the big number font. |
| 169 | +static void SendChar(unsigned char data) |
| 170 | +{ |
| 171 | + //if (interrupt && !doing_menu) return; // Stop printing only if interrupt is call but not in button functions |
| 172 | + |
| 173 | + Wire.beginTransmission(OLED_address); // begin transmitting |
| 174 | + Wire.write(0x40);//data mode |
| 175 | + Wire.write(data); |
| 176 | + Wire.endTransmission(); // stop transmitting |
| 177 | +} |
| 178 | + |
| 179 | +//==========================================================// |
| 180 | +// Prints a display char (not just a byte) in coordinates X Y, |
| 181 | +// being multiples of 8. This means we have 16 COLS (0-15) |
| 182 | +// and 8 ROWS (0-7). |
| 183 | +static void sendCharXY(unsigned char data, int X, int Y) |
| 184 | +{ |
| 185 | + setXY(X, Y); |
| 186 | + Wire.beginTransmission(OLED_address); // begin transmitting |
| 187 | + Wire.write(0x40);//data mode |
| 188 | + |
| 189 | + for(int i=0;i<8;i++) |
| 190 | + Wire.write(pgm_read_byte(myFont[data-0x20]+i)); |
| 191 | + |
| 192 | + Wire.endTransmission(); // stop transmitting |
| 193 | +} |
| 194 | + |
| 195 | +//==========================================================// |
| 196 | +// Used to send commands to the display. |
| 197 | +static void sendcommand(unsigned char com) |
| 198 | +{ |
| 199 | + Wire.beginTransmission(OLED_address); //begin transmitting |
| 200 | + Wire.write(0x80); //command mode |
| 201 | + Wire.write(com); |
| 202 | + Wire.endTransmission(); // stop transmitting |
| 203 | +} |
| 204 | + |
| 205 | +//==========================================================// |
| 206 | +// Set the cursor position in a 16 COL * 8 ROW map. |
| 207 | +static void setXY(unsigned char row,unsigned char col) |
| 208 | +{ |
| 209 | + sendcommand(0xb0+row); //set page address |
| 210 | + sendcommand(offset+(8*col&0x0f)); //set low col address |
| 211 | + sendcommand(0x10+((8*col>>4)&0x0f)); //set high col address |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | +//==========================================================// |
| 216 | +// Prints a string regardless the cursor position. |
| 217 | +static void sendStr(unsigned char *string) |
| 218 | +{ |
| 219 | + unsigned char i=0; |
| 220 | + while(*string) |
| 221 | + { |
| 222 | + for(i=0;i<8;i++) |
| 223 | + { |
| 224 | + SendChar(pgm_read_byte(myFont[*string-0x20]+i)); |
| 225 | + } |
| 226 | + *string++; |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +//==========================================================// |
| 231 | +// Prints a string in coordinates X Y, being multiples of 8. |
| 232 | +// This means we have 16 COLS (0-15) and 8 ROWS (0-7). |
| 233 | +static void sendStrXY( char *string, int X, int Y) |
| 234 | +{ |
| 235 | + setXY(X,Y); |
| 236 | + unsigned char i=0; |
| 237 | + while(*string) |
| 238 | + { |
| 239 | + for(i=0;i<8;i++) |
| 240 | + { |
| 241 | + SendChar(pgm_read_byte(myFont[*string-0x20]+i)); |
| 242 | + } |
| 243 | + *string++; |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | + |
| 248 | +//==========================================================// |
| 249 | +// Inits oled and draws logo at startup |
| 250 | +static void init_OLED(void) |
| 251 | +{ |
| 252 | + sendcommand(0xae); //display off |
| 253 | + sendcommand(0xa6); //Set Normal Display (default) |
| 254 | + // Adafruit Init sequence for 128x64 OLED module |
| 255 | + sendcommand(0xAE); //DISPLAYOFF |
| 256 | + sendcommand(0xD5); //SETDISPLAYCLOCKDIV |
| 257 | + sendcommand(0x80); // the suggested ratio 0x80 |
| 258 | + sendcommand(0xA8); //SSD1306_SETMULTIPLEX |
| 259 | + sendcommand(0x3F); |
| 260 | + sendcommand(0xD3); //SETDISPLAYOFFSET |
| 261 | + sendcommand(0x0); //no offset |
| 262 | + sendcommand(0x40 | 0x0); //SETSTARTLINE |
| 263 | + sendcommand(0x8D); //CHARGEPUMP |
| 264 | + sendcommand(0x14); |
| 265 | + sendcommand(0x20); //MEMORYMODE |
| 266 | + sendcommand(0x00); //0x0 act like ks0108 |
| 267 | + |
| 268 | + //sendcommand(0xA0 | 0x1); //SEGREMAP //Rotate screen 180 deg |
| 269 | + sendcommand(0xA0); |
| 270 | + |
| 271 | + //sendcommand(0xC8); //COMSCANDEC Rotate screen 180 Deg |
| 272 | + sendcommand(0xC0); |
| 273 | + |
| 274 | + sendcommand(0xDA); //0xDA |
| 275 | + sendcommand(0x12); //COMSCANDEC |
| 276 | + sendcommand(0x81); //SETCONTRAS |
| 277 | + sendcommand(0xCF); // |
| 278 | + sendcommand(0xd9); //SETPRECHARGE |
| 279 | + sendcommand(0xF1); |
| 280 | + sendcommand(0xDB); //SETVCOMDETECT |
| 281 | + sendcommand(0x40); |
| 282 | + sendcommand(0xA4); //DISPLAYALLON_RESUME |
| 283 | + sendcommand(0xA6); //NORMALDISPLAY |
| 284 | + |
| 285 | + clear_display(); |
| 286 | + sendcommand(0x2e); // stop scroll |
| 287 | + //----------------------------REVERSE comments----------------------------// |
| 288 | + sendcommand(0xa0); //seg re-map 0->127(default) |
| 289 | + sendcommand(0xa1); //seg re-map 127->0 |
| 290 | + sendcommand(0xc8); |
| 291 | + delay(1000); |
| 292 | + //----------------------------REVERSE comments----------------------------// |
| 293 | + // sendcommand(0xa7); //Set Inverse Display |
| 294 | + // sendcommand(0xae); //display off |
| 295 | + sendcommand(0x20); //Set Memory Addressing Mode |
| 296 | + sendcommand(0x00); //Set Memory Addressing Mode ab Horizontal addressing mode |
| 297 | + // sendcommand(0x02); // Set Memory Addressing Mode ab Page addressing mode(RESET) |
| 298 | +} |
| 299 | + |
| 300 | + |
0 commit comments