Advertisement
metalx1000

ESP8266 LCD I2C Code

Aug 14th, 2020
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal_I2C.h>
  2.  
  3. #include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
  4.  
  5. //needed for library
  6. #include <DNSServer.h>
  7. #include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
  8. #include <WiFiClient.h>
  9. #include <ESP8266HTTPClient.h>
  10.  
  11. char url[]="http://filmsbykris.com/qr/2020/msg.php";
  12.  
  13.  
  14. //amount to wait before loop start
  15. int wait=10000;
  16.  
  17. //amount to wait each loop
  18. int loop_wait=5000;
  19.  
  20. void scrollText();
  21. void request();
  22. // set the LCD number of columns and rows
  23. int lcdColumns = 16;
  24. int lcdRows = 2;
  25.  
  26. // set LCD address, number of columns and rows
  27. // if you don't know your display address, run an I2C scanner sketch
  28. LiquidCrystal_I2C lcd(0x3F, lcdColumns, lcdRows);  
  29.  
  30. void setup(){
  31.   // initialize LCD
  32.   lcd.init();
  33.   // turn on LCD backlight                      
  34.   lcd.backlight();
  35.       WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  36.  
  37.     // put your setup code here, to run once:
  38.     Serial.begin(115200);
  39.    
  40.     // WiFi.mode(WiFi_STA); // it is a good practice to make sure your code sets wifi mode how you want it.
  41.  
  42.     //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  43.     WiFiManager wm;
  44.  
  45.     //reset settings - wipe credentials for testing
  46.     //wm.resetSettings();
  47.  
  48.     // Automatically connect using saved credentials,
  49.     // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
  50.     // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
  51.     // then goes into a blocking loop awaiting configuration and will return success result
  52.  
  53.     bool res;
  54.     // res = wm.autoConnect(); // auto generated AP name from chipid
  55.     res = wm.autoConnect("Spy_Pager"); // anonymous ap
  56.     // res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
  57.  
  58.     if(!res) {
  59.         Serial.println("Failed to connect");
  60.         // ESP.restart();
  61.     }
  62.     else {
  63.         //if you get here you have connected to the WiFi    
  64.         Serial.println("connected...yeey :)");
  65.     }
  66.     lcd.clear();
  67.     lcd.setCursor(0, 0);
  68.     // print message
  69.     lcd.print("Secret Spy MSG!");
  70.     lcd.setCursor(1, 0);
  71.     lcd.print("Loading...");
  72. }
  73.  
  74.  
  75.  
  76. void loop(){
  77.   request(url);
  78.   delay(loop_wait);
  79. }
  80.  
  81. void request(char *url){
  82.     WiFiClient client;
  83.  
  84.     HTTPClient http;
  85.  
  86.     Serial.print("[HTTP] begin...\n");
  87.     if (http.begin(client, url)) {  // HTTP
  88.  
  89.  
  90.       Serial.print("[HTTP] GET...\n");
  91.       // start connection and send HTTP header
  92.       int httpCode = http.GET();
  93.  
  94.       // httpCode will be negative on error
  95.       if (httpCode > 0) {
  96.         // HTTP header has been send and Server response header has been handled
  97.         Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  98.  
  99.         // file found at server
  100.         if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
  101.           String payload = http.getString();
  102.           Serial.println(payload);
  103.           lcd.clear();
  104.           lcd.setCursor(0, 0);
  105.           // print message
  106.           lcd.print("Secret Spy MSG!");
  107.           scrollText(1, payload, 250, lcdColumns);
  108.          
  109.         }
  110.       } else {
  111.         Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  112.         char msg[] = "[HTTP] GET... failed, error:";
  113.         scrollText(1, msg, 250, lcdColumns);
  114.       }
  115.  
  116.       http.end();
  117.     } else {
  118.       Serial.printf("[HTTP} Unable to connect\n");
  119.       scrollText(1, "[HTTP} Unable to connect", 250, lcdColumns);
  120.     }
  121.  
  122. }
  123.  
  124. void scrollText(int row, String message, int delayTime, int lcdColumns) {
  125.   for (int i=0; i < lcdColumns; i++) {
  126.     message = " " + message;  
  127.   }
  128.   message = message + " ";
  129.   for (int pos = 0; pos < message.length(); pos++) {
  130.     lcd.setCursor(0, row);
  131.     lcd.print(message.substring(pos, pos + lcdColumns));
  132.     delay(delayTime);
  133.   }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement