# Send Image to gemma4:e2b

**URL:** <https://forum.amebaiot.com/t/send-image-to-gemma4-e2b/5071>\
**Category:** AI\
**Tags:** rtl8735b\
**Created:** [September 17, 2026, 6:43pm UTC](https://forum.amebaiot.com/t/send-image-to-gemma4-e2b/5071 "2026-09-17T18:43:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkuo2000](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/rkuo2000/32/1105_2.png) [@rkuo2000](https://forum.amebaiot.com/u/rkuo2000)\
**Post date:** [September 17, 2026, 6:43pm UTC](https://forum.amebaiot.com/t/send-image-to-gemma4-e2b/5071/1 "2026-09-17T18:43:19Z")

</div>

Application : Android Edge-Gallery App (Model = gemma4:e2b , AI Chat with Text/Image API)

Example: [Ollama\_sendImage.ino](https://github.com/rkuo2000/EdgeAI-AmebaPro2/blob/main/Arduino/AMB82-mini/Ollama_sendImage/Ollama_sendImage.ino)

Question: the following code’s waitTime is fix at 10000 ms, how to modify it to keep waiting till all text are received from LLM ?

```auto
    Serial.println("Receive");
    uint32_t waitTime = 10000;
    uint32_t startTime = millis();
    boolean state = false;
    boolean markState = false;

    while ((startTime + waitTime) > millis()) {
        Serial.print(".");
        delay(1000);
    }

    while (client.available()) {
        char c = client.read();
        if (String(c) == "{") {
            markState = true;
        }
        if (state == true && markState == true) {
            Feedback += String(c);
        }
        if (c == '\n') {
            if (getResponse.length() == 0) {
                state = true;
            }
            getResponse = "";
        } else if (c != '\r') {
            getResponse += String(c);
        }
    }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/2X/7/7009caf971627f868f3de3d34312aff2d14c61e3.png) [@system](https://forum.amebaiot.com/u/system)\
**Post date:** [September 17, 2026, 6:43pm UTC](https://forum.amebaiot.com/t/send-image-to-gemma4-e2b/5071/2 "2026-09-17T18:43:22Z")

</div>

👋 Thanks for posting!

For documentation, SDK resources, FAQs, and community guidelines, please visit: [here](https://forum.amebaiot.com/t/forum-community-guidelines-and-resources/4969)

For commercial users, if you would like to have dedicated technical support or to connect with us, please fill in the [Private User Form](https://forms.cloud.microsoft/r/FK9WxDhvDg)

Happy building with Ameba!

* * *

👋 感謝您的分享！

如需查閱技術文件、SDK 資源、常見問題及社群指南，請參考: [這裡](https://forum.amebaiot.com/t/forum-community-guidelines-and-resources/4969)

若您是商業用戶，希望獲得專屬技術支援或與我們聯繫，請填寫 [Private User Form (商業用戶表單)](https://forms.cloud.microsoft/r/FK9WxDhvDg)

祝您使用 Ameba 開發順利！

---

<div class="post-metadata">

**Author:** ![geofrancis](https://avatars.discourse-cdn.com/v4/letter/g/c5a1d2/32.png) [@geofrancis](https://forum.amebaiot.com/u/geofrancis)\
**Post date:** [September 18, 2026, 9:06am UTC](https://forum.amebaiot.com/t/send-image-to-gemma4-e2b/5071/3 "2026-09-18T09:06:56Z")

</div>

i made the example copy and pasting a few things together, i never did figure that part out. im sure if you stick the code into one of the AI’s it will fix it.

gemini gave me this

void send\_Ollama()  
{  
Serial.println("Connect to " + String(myDomain));  
if (!client.connect(myDomain, 11434)) {  
Serial.println(“Connection failed”);  
return;  
}

Serial.println(“Connection successful”);  
Camera.getImage(0, &img\_addr, &img\_len);  
Serial.println(“Image Captured: " + String(img\_len) + " bytes”);

// 1. Prepare JSON structure  
String jsonStart = “{\“model\”: \”" + model + “\”, \“messages\”: [{\“role\”: \“user\”,\“content\”: [{ \“type\”: \“text\”, \“text\”: \“” + prompt + “\”},{\“type\”: \“image\_url\”, \“image\_url\”: {\“url\”: \“data:image/jpeg;base64,”};  
String jsonEnd = “\”}}]}]}";

// 2. Calculate Content-Length without building the string in memory  
// Base64 expands 3 bytes to 4 characters. We calculate exact padded length.  
uint32\_t b64Len = 4 \* ((img\_len + 2) / 3);  
uint32\_t contentLength = jsonStart.length() + b64Len + jsonEnd.length();

// 3. Send HTTP Headers  
Serial.println(“Sending Request…”);  
client.println(“POST /v1/chat/completions HTTP/1.1”);  
client.println("Host: " + String(myDomain));  
client.println("Authorization: Bearer " + ollama\_key);  
client.println(“Content-Type: application/json; charset=utf-8”);  
client.println("Content-Length: " + String(contentLength));  
client.println(“Connection: close”);  
client.println();

// 4. Stream JSON start  
client.print(jsonStart);

// 5. Stream Base64 directly to the client  
uint8\_t \*input = (uint8\_t \*)img\_addr;  
uint32\_t bytesLeft = img\_len;  
char b64Chunk[5]; // 4 bytes + null terminator

while (bytesLeft \> 0) {  
uint8\_t chunkLen = (bytesLeft \< 3) ? bytesLeft : 3;  
base64\_encode(b64Chunk, (char\*)input, chunkLen);  
client.print(b64Chunk);  
input += chunkLen;  
bytesLeft -= chunkLen;  
}

// 6. Stream JSON end  
client.print(jsonEnd);

// 7. Wait for response with a timeout (e.g., 60 seconds for LLM processing)  
Serial.println(“\nWaiting for server response…”);  
uint32\_t startTime = millis();  
while (client.connected() && !client.available()) {  
if (millis() - startTime \> 60000) {  
Serial.println(“Timeout waiting for response.”);  
client.stop();  
return;  
}  
delay(10);  
}

// 8. Read the response robustly  
String responseBody = “”;  
bool isBody = false;

while (client.connected() || client.available()) {  
if (client.available()) {  
String line = client.readStringUntil(‘\n’);  
line.trim(); // Remove \r

```auto
    if (line.length() == 0 && !isBody) {
        isBody = true; // Empty line separates HTTP headers from the body
    } else if (isBody) {
        responseBody += line;
    }
}

```

}

// 9. Parse JSON  
if (responseBody.length() \> 0) {  
DeserializationError error = deserializeJson(doc, responseBody);  
if (!error) {  
String content = doc[“choices”][0][“message”][“content”];  
Serial.println(“\n— Ollama Response —”);  
Serial.println(content);  
} else {  
Serial.print("JSON Parse Failed: ");  
Serial.println(error.c\_str());  
Serial.println(responseBody); // Print raw text to debug  
}  
}

client.stop();  
Serial.println(“Connection closed.”);

---

<div class="post-metadata">

**Author:** ![rkuo2000](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/rkuo2000/32/1105_2.png) [@rkuo2000](https://forum.amebaiot.com/u/rkuo2000)\
**Post date:** [September 19, 2026, 3:29pm UTC](https://forum.amebaiot.com/t/send-image-to-gemma4-e2b/5071/4 "2026-09-19T15:29:31Z")

</div>

remove waitTime won’t work,

it seems client.available() can not be checked before LLM response is generated ???

Since LLM response may take 10~15 seconds, I can only wait for 15 seconds.

Otherwise, it won’t receive and print out the message.
