JEPLANS.com ESP2Electronics2
HOME
Catalog
Stirling Engines
Air Engines
Electric Devices
Around The Home
Toys
Wooden Robots
Music
Compound Scrolling
Shop Accessories
Electronics/Enclosures
Chicken Related
Other
3D Printing
All Plans
Topics
Engines
Electronics
Printing Plans
Terms and Policies
Displacer Plan Maker
Tools
Comics
Other
Contact Us
Login
On YouTube!
HOME
Guest
(Login or Register)
ESP8266 WiFi Image Viewer
Arduino Sketch for ESP8266 WiFi Image Viewer
/** * Image Download and Display on ILI9341 * Started with StreamHTTPClient.ino example for ESP8266 * Peforms request to resizeimage-pout.php that then reformats file info binary size and pixel data specifically to the Adafruit color scheme * Created on: 4/23/2016 by James Eckert jeplans.com * Pin as they appear on my board * SDO/MISO---GPIO12 * LED--------resistor-- vcc (In future I will use PWM) * SCK--------GPI014 * MOSI-------GPIO13 * DC/RS------GPIO5 * Reset------GPIO2 * CS---------4 * GND--------GND * VCC--------VCC */ #include
#include
#include
#include "SPI.h" #include "Adafruit_GFX.h" #include "Adafruit_ILI9341.h" #define TFT_DC 5 #define TFT_CS 4 #define TFT_MOSI 13 #define TFT_CLK 14 #define TFT_MISO 12 #define TFT_RST 2 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO); int wfcnt=0; const char* ssid = "yourSSID"; // Put your SSID here const char* password = "youwifikey"; // Put your PASSWORD here boolean HRequestsActive=0; unsigned long t1Time = 0; unsigned long t2Time = 0; unsigned long t3Time = 0; String Lasttime=""; void setup() { Serial.begin(115200); Serial.println("Welcome to File get!"); tft.begin(); delay(20); uint8_t tx = tft.readcommand8(ILI9341_RDMODE); tx = tft.readcommand8(ILI9341_RDSELFDIAG); delay(20); Serial.print("Self Diagnostic: 0x"); Serial.println(tx, HEX); delay(20); tft.fillScreen(ILI9341_BLACK); delay(20); tft.setRotation(2); tft.drawRect(0, 0, 240, 320, 0x00FF); delay(20); // Next 2 line seem to be needed to connect to wifi after Wake up WiFi.disconnect(); WiFi.mode(WIFI_OFF); delay(20); ///////////////////////// // Connect to WiFi network tft.setTextColor(ILI9341_GREEN); tft.setTextSize(3); Lasttime="Connecting"; tft.setCursor(15, 195); tft.print(Lasttime); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { // try to connect wifi for 6 sec then reset tft.setTextColor(ILI9341_BLUE); tft.setCursor(15, 195); tft.print(Lasttime); delay(250); tft.setTextColor(ILI9341_RED); tft.setCursor(15, 195); tft.print(Lasttime); Serial.print("."); delay(250); wfcnt++; } Serial.println("WiFi connected"); Serial.println(""); Serial.println(WiFi.localIP()); tft.fillRect(1, 180, 238, 70, 0x0000); tft.setTextColor(ILI9341_GREEN); tft.setCursor(15, 195); tft.print(WiFi.localIP()); } int ah2i(uint8_t s) { int a = 0; uint8_t c = s; if (c >= 48 && c <= 57) { a = c - 48; } else if (c >= 65 && c <= 70) { a =16; a += (c - 65); } else if (c >= 97 && c <= 102) { a = 16; a += (c - 97); } return a; } void dldDImage (uint16_t xloc , uint16_t yloc) { uint16_t r; uint8_t hb,lb,cv,lv1,lv2,cb1,cb2; uint16_t frsize,iwidth,iheight; int x=0,y=0; int firstpacket=0; int buffcnt=0; int bytescolleted=0; HTTPClient http; Serial.print("[HTTP] begin...\n"); // configure server and url http.begin("http://10.16.10.11:8980/tempgr/resizeimage-pout.php?IMG=1"); Serial.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); if(httpCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if(httpCode == HTTP_CODE_OK) { unsigned long DrawTime = millis(); // get lenght of document (is -1 when Server sends no Content-Length header) int len = http.getSize(); // create buffer for read uint8_t buff[2048] = { 0 }; int buffidx = sizeof(buff); // get tcp stream WiFiClient * stream = http.getStreamPtr(); uint16_t BytesToRead1=0; uint16_t BytesToRead2=8192; // read all data from server while(http.connected() && (len > 0 || len == -1)) { // get available data size size_t size = stream->available(); //Serial.println("size="+String(size)); if(size) { // read up to 128 byte buffidx = 0; int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // Uncomment Below to send raw output it to Serial // Serial.write(buff, c); while (buffidx
(BytesToRead2-1)){ int rflag=0; int hcnt=0; // Serial.print("
"); while (rflag==0) { cv = buff[buffidx++]; //Serial.write(cv); if (cv==10 && lv1==13 && lv2>47 ) { rflag=1; } if (cv>47){ BytesToRead1=BytesToRead1 << 4; BytesToRead1=BytesToRead1+ah2i(cv); } lv2=lv1; lv1=cv; hcnt++; //if end packet size data not found in fist 10 bytes only one packet reset buffidx so we don't miss width data if (hcnt>10) { rflag=1; buffidx=0; BytesToRead1=8192;} } buffcnt=0; //start count over to watch for next packet start //First Two Bytes Contain Width if (firstpacket==0) { hb=buff[buffidx++]; lb=buff[buffidx++]; iwidth=hb; iwidth=iwidth << 8; iwidth=iwidth+lb; Serial.println("Width="+String(iwidth)); buffcnt=buffcnt+2; } yield(); //2nd Two Bytes Contain height if (firstpacket==0) { hb=buff[buffidx++]; lb=buff[buffidx++]; iheight=hb; iheight=iheight << 8; iheight=iheight+lb; Serial.println("Height="+String(iheight)); buffcnt=buffcnt+2; } firstpacket=1; //Serial.println("Bytes to next packet="+String(BytesToRead1)); BytesToRead2=BytesToRead1; //Serial.println("
"); } // read in bytes 1 at a time when you have two plot pixel // can't read 2 at a time becuse packet boundries are sometime odd if (bytescolleted<1){ cb1 = buff[buffidx++]; bytescolleted++; buffcnt++; } else { cb2 = buff[buffidx++]; bytescolleted++; buffcnt++; } if (bytescolleted>1) { r = word(cb1, cb2); if (y +yloc
iwidth-1) { y++;x=0; } } } if(len > 0) { len -= c; } } delay(2); } Serial.println("y="+String(y)); Serial.println("[HTTP] connection closed or file end.\n"); Serial.println(String(millis()-DrawTime)); tft.fillRect(1, 180, 238,70, 0x0000); tft.setCursor(15, 195); tft.setTextColor(ILI9341_RED); tft.setTextSize(4); tft.println(String(millis()-DrawTime)+"ms"); buffcnt=0; firstpacket=0; x=0;y=0; buffidx=0; t2Time = millis(); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); HRequestsActive=0; } void GetTicker() { HTTPClient http; Serial.print("Ticker [HTTP] begin...\n"); http.begin("http://10.16.10.11:8980//tempgr/oled.php"); Serial.print("[HTTP] GET...\n"); int httpCode = http.GET(); if(httpCode > 0) { Serial.printf("[HTTP] GET... code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); HRequestsActive=0; } void loop() { delay (50); if ( (millis()-t3Time)>995 ) { tft.setCursor(15, 228); tft.setTextColor(ILI9341_BLACK); tft.setTextSize(4); tft.print(Lasttime); tft.setTextColor(ILI9341_GREEN); Lasttime=String((millis()-t2Time)/1000)+"s"; tft.setCursor(15, 228); tft.print(Lasttime); t3Time = millis(); } // Pull new Picture every 30sec if ( t1Time==0 || (millis()-t1Time)>30000) { if((WiFi.status() == WL_CONNECTED)) { dldDImage (0,0) ; // Draw Image x,y specifies upper left starting corner } t1Time = millis(); } }
resizeimage-pout.php format and supply data stream for ESP8266 WiFi Image Viewer
<?php $target_dir = "/var/www/tempgr/ac/pics/"; $data=$_GET["IMG"]; $im = @imagecreatefromjpeg($target_dir."a$data.jpg"); $width = imagesx($im); $height = imagesy($im); $nwidth=240; $nheight= round($nwidth*$height/$width); $dest = imagecreatetruecolor($nwidth, $nheight); $PEN_color1 = imagecolorallocate($im, 255, 255, 255); imagecopyresized($dest, $im, 0, 0, 0, 0, $nwidth, $nheight, $width, $height); imagerectangle ( $dest , 0 , 0 , $nwidth-1, $nheight-1 , $PEN_color1 ); echo (pack("n", $nwidth)); echo (pack("n", $nheight)); for ($y = 0; $y <= $nheight-1; $y++) { for ($x = 0; $x <= $nwidth-1; $x++) { $color = imagecolorat($dest, $x, $y); $r = ($color >> 16) & 0xFF; $g = ($color >> 8) & 0xFF; $b = $color & 0xFF; $r=($r*31/255) << 11; $g=$g*(63/255) << 5; $b=$b*31/255; $ac=$r|$g|$b; echo (pack("n", $ac)); } } imagedestroy($im); imagedestroy($dest); ?>
>Last Page<
Login to Disable Ads