This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:iot:examples:oled [2021/03/05 01:51] – heiko.pikner | en:iot:examples:oled [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== | ||
| + | Oled module must be connected to controller module or with sensor module. | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | Needed libraries: | ||
| + | < | ||
| + | |||
| + | The example code above shows counter value on screen | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define WIFI_NAME " | ||
| + | #define WIFI_PASSWORD " | ||
| + | |||
| + | // OLED reset pin is GPIO0 | ||
| + | #define OLED_RESET 0 // GPIO0 | ||
| + | |||
| + | Ticker timeTicker; | ||
| + | Adafruit_SSD1306 display(OLED_RESET); | ||
| + | |||
| + | bool isBootModeNormal; | ||
| + | bool sendDataFlag; | ||
| + | |||
| + | int i = 0; // Counter variable is defined | ||
| + | |||
| + | // ITT splashs screen bitmap. Generator can be found in: http:// | ||
| + | static const unsigned char PROGMEM logo16_glcd_bmp[] = | ||
| + | { | ||
| + | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x1f, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x1f, 0xe3, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0x1f, 0xe3, 0xff, 0xc0, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x00, 0x00, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe3, 0xff, 0xff, 0xf8, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe0, 0x00, 0x00, 0x18, 0x00, 0x00, 0x07, 0x10, 0xe0, 0x00, 0x00, 0x18, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe0, 0x00, 0x00, 0x18, 0x00, 0x00, 0x07, 0x10, 0xe3, 0xfc, 0xff, 0xf8, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe3, 0xfc, 0xff, 0xf8, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x07, 0x10, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x07, 0xf0, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0xe2, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x80, 0x00, 0x00, | ||
| + | 0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | ||
| + | }; | ||
| + | |||
| + | // Ticker library callback, which will occur 0.5 second interval. | ||
| + | void sendData() | ||
| + | { | ||
| + | sendDataFlag=true; | ||
| + | } | ||
| + | |||
| + | // If message received print it out. | ||
| + | void iot_received(String topic, String msg) | ||
| + | { | ||
| + | Serial.print(" | ||
| + | Serial.print(topic); | ||
| + | Serial.print(" | ||
| + | Serial.println(msg); | ||
| + | } | ||
| + | |||
| + | // Function started after the connection to the server is established. | ||
| + | void iot_connected() | ||
| + | { | ||
| + | Serial.println(" | ||
| + | iot.log(" | ||
| + | isBootModeNormal = true; | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(115200); | ||
| + | Serial.println(" | ||
| + | |||
| + | // initialize with the I2C addr 0x3C (for the 64x48) | ||
| + | display.begin(SSD1306_SWITCHCAPVCC, | ||
| + | |||
| + | // Since the buffer is initialized with an Adafruit splashscreen | ||
| + | // internally, we should clear it | ||
| + | display.clearDisplay(); | ||
| + | |||
| + | // Load ITT splash screen into buffer | ||
| + | display.drawBitmap(0, | ||
| + | // Show image buffer on the display | ||
| + | display.display(); | ||
| + | |||
| + | // Display splashscreen two second | ||
| + | delay(2000); | ||
| + | |||
| + | // | ||
| + | // | ||
| + | |||
| + | // print IoT json config to serial | ||
| + | iot.printConfig(); | ||
| + | |||
| + | // Initialize IoT library | ||
| + | iot.setup(); | ||
| + | |||
| + | // Initialize Ticker interval and callback | ||
| + | timeTicker.attach(0.5, | ||
| + | } | ||
| + | |||
| + | // Main code, which runs in loop | ||
| + | void loop() | ||
| + | { | ||
| + | // IoT behind the plan work, it should be periodically called | ||
| + | iot.handle(); | ||
| + | |||
| + | // Increase counter value by 1 | ||
| + | i++; | ||
| + | |||
| + | // Display counter value on the OLED screen | ||
| + | display.clearDisplay(); | ||
| + | display.setTextSize(1); | ||
| + | display.setTextColor(WHITE); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | display.println(i); | ||
| + | display.display(); | ||
| + | |||
| + | // Send counter value to the computer | ||
| + | if(WiFi.isConnected() && isBootModeNormal) | ||
| + | { | ||
| + | if(sendDataFlag) | ||
| + | { | ||
| + | sendDataFlag = false; | ||
| + | String msg = String(i); | ||
| + | iot.publishMsg(" | ||
| + | Serial.println(msg); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | delay(500); // delay of 0.5s, to change the counter value slowly | ||
| + | } | ||
| + | |||
| + | </ | ||