Arduino uploading JPG using Google Drive Rest API - arduino

I have written a simple program to upload a JPG image from my arduino to Google Drive.
I use a POST request as described in the guide.
If I try to upload a 1600*1200 image, sometimes it works (200) and sometimes not (308) and I can't figure out why.
Then, when I receive 308 it does not send me back the byte range header as described in the guide.
I noticed that if I'm watching a film the response 308 and when I stop the film, after a while the upload becomes successful
The function which is giving me issues is this one:
bool httpsUploadFromSD(class Token token, String filepath) {
WiFiSSLClient client = token.getClient(); // I get the client class
File image = SD.open(filepath, FILE_READ); // I open the file I want to send
String name_metadata ="{\"name\": \"" + String(image.name()) + "\"}";
if (!image) {
Serial.println("FILE OPEN FAILED");
return false;
} else {
Serial.println("IMAGE OPENED of size" + String(image.size()));
}
bool received = false;
bool trytorefresh = false;
unsigned long startTime = millis();
String code = "";
String uploadID = "";
// Connecting -- Asking for upload permission
Serial.println("Asking for upload...");
do {
// Sending the upload request
if (client.connect("www.googleapis.com", 443)) {
client.println("POST /upload/drive/v3/files?uploadType=resumable HTTP/1.1");
client.println("Host: www.googleapis.com");
client.println("Authorization: " + token.token_type + " " + token.access_token);
client.println("Content-Length: " + String(name_metadata.length()));
client.println("Content-Type: application/json; charset=UTF-8");
client.println("X-Upload-Content-Type: image/jpeg");
client.println("X-Upload-Content-Length: " + String(image.size()));
client.println("Connection: close");
client.println();
client.println(name_metadata);
client.println();
Serial.println("Upload request sent");
received = false;
} else {
Serial.println("connection failed");
received = true;
}
//Listen to the client responsse
while ((millis() - startTime < 5000) && !received) { //try to listen for 5 seconds
int i = 0;
while (client.available() && i < 12) {
received = true;
char c = client.read();
Serial.write(c);
code = code + c;
i++;
}
//When I reckognize 200 I enter here and identify the uploadID;
if(i>0){
if (code == "HTTP/1.1 200") {
while (client.available()) {
char c = client.read(); // here I print in the Serial the response
Serial.write(c);
if (c == ':') {
c = client.read();
Serial.write(c);
c = client.read();
Serial.write(c);
do {
uploadID = uploadID + c; // Here I identify UploadID from the resp
c = client.read();
Serial.write(c);
} while (c != '\n');
break;
}
}
break;
}
else if (code == "HTTP/1.1 401") {
while(client.available()) {
char c = client.read();
Serial.write(c);
}
// If credentials are not valide, I'll try once to refresh the token;
if(!trytorefresh){
Serial.println("\nProbably you need to refresh the token\nI'm trying to refresh\n");
token.httpsTokenRefresh();
trytorefresh = !trytorefresh;
}
}
else if (code == "HTTP/1.1 400") {
while(client.available()) {
char c = client.read();
Serial.write(c);
}
break;
} else {
break;
}
}
}
} while (trytorefresh); // I try to refresh once if the resposnse is 401
if (code == "HTTP/1.1 200") {
// I stop the previous client session, because now I start a new one, to do the PUT request and upload the file
client.stop();
Serial.println("Token request has been succesful. Starting upload");
bool succesful = false;
// I have obtained the uploadID, now I start uploading
String location = "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=" + uploadID;
while(!succesful){ // I upload until it is successful
// Upload request
if (client.connect("www.googleapis.com", 443)) {
client.println("PUT " + location + " HTTP/1.1");
client.println("User-Agent: Arduino Camera");
client.println("Content-Length: " + String(image.size()));
client.println("Connecion: close");
client.println();
while (image.available()) {
client.write(image.read()); // Here I send the bytes of the image
}
image.close();
received = false;
} else {
Serial.println("Connection failed");
received = true;
}
// Listening to the response
startTime = millis();
String code = "";
while ((millis() - startTime < 5000) && !received) { //try to listen for 5 seconds
int i = 0;
while (client.available() && i < 12) {
received = true;
char c = client.read();
Serial.write(c);
code = code + c;
i++;
}
// HTTP 200 OK
if (code == "HTTP/1.1 200" || code == "HTTP/1.1 201")
{
while(client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.println("\n\nUpload succesful");
succesful = true;
client.stop();
return succesful;
}
// HTTP 308 I have to restart my upload
else if (code == "HTTP/1.1 308") {
// Here I print the response in serial and I identify the range to re-send. Actually it always happens that the range header is not present, so I re-send the whole image
bool range_present = false;
char Range[] = "xxxxxx";
String byte_range = "";
uint32_t re_send_length = 0;
while (client.available()) {
for (int k = 0; k<5; k++){
Range[k] = Range[k++];
}
char c = client.read();
Range[5] = c;
Serial.write(c);
if (Range == "Range:") {
break;
range_present = true;
}
}
if (range_present) {
int k = 0;
while (client.available() && k<7) {
char c = client.read();
Serial.write(c);
}
k = 0;
String Range2 = "";
while (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
break;
}
Range2 = Range2 + c;
}
while (client.available()) {
char c = client.read();
Serial.write(c);
}
bool a = false;
for (k = 0; k<Range2.length(); k++) {
if (a) {
byte_range = byte_range + Range2[k];
}
if (Range2[k] = '-') {
a = true;
}
}
Serial.println("byte_range = " + byte_range);
}
Serial.println("\n\nUpload interrupted. Starting a new session");
// I have to open image again
image = SD.open(filepath, FILE_READ);
delay(1000);
}
}
}
} else if (code == "HTTP/1.1 401" && trytorefresh) {
Serial.println("Upload failed. Probably you need a new token");
}
client.stop();
return false;
}
Here there is the token class
class TokenRequestData {
public:
TokenRequestData();
TokenRequestData(String deviceCode, String userCode, int expiresIn, int Interval, String verificationUrl, WiFiSSLClient Client);
String device_code;
String user_code;
int expires_in;
int interval;
String verification_url;
WiFiSSLClient client;
void httpsAskForTokenData();
};
class Token {
TokenRequestData data;
void httpsTokenPolling(class TokenRequestData requestData);
public:
Token();
Token(class TokenRequestData Data, String AccessToken, String RefreshToken, int ExpiresIn);
String access_token;
String refresh_token;
int expires_in;
String token_type;
void httpsTokenRequest(class TokenRequestData requestData);
void httpsTokenRefresh();
WiFiSSLClient getClient();
};

Related

Sending data over ESP_NOW

I'm a total noob and just starting out with PlatformIO and Arduino/ESP32. I also want to say thanks in advance to any help I can get.
Plan:
I have 2 ESP32's talking over ESP_NOW, I just can't verify the data being sent in order to progress with my project. Basically, I have a Nextion display that sends specific info to an ESP32 (tested and working) and that ESP32 is then to send that information via ESP_NOW to the other ESP32 which will translate it into serial data to send to an Arduino Due and perform some tasks.
Problem:
The issue I have is that when I test, I see the data I think I am transmitting, but when I try to Serial.print said info, I get "0 0 0 0". I'm not sure that I am sending OR receiving the data properly. All I know is that when I press the button on the Nextion, I get a response on the ESP32 that is not connected.
Code:
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
// Example to receive data with a start marker and length byte
// For ease of testing this uses upper-case characters A and B
// for the request and response types
// and Z for the start marker
// this version saves all the bytes after the start marker
// if the TYPE byte is wrong it aborts
const byte numBytes = 32;
byte receivedBytes[numBytes];
const byte typeBytePosition = 6; // Position after the start byte
const byte requestType = 0x65;
const byte requestLength = 7;
boolean newData = false;
int LED = 21;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Define variables to store Nextion readings to be sent
byte NexEventID;
byte NexPageID;
byte NexObjectID;
byte NexStatusID;
// Define variables to store incoming readings// Define variables to store incoming readings
byte incomingEventID;
byte incomingPageID;
byte incomingObjectID;
byte incomingStatusID;
// Variable to store if sending data was successful
String success;
typedef struct struct_message
{
byte EventID;
byte PageID;
byte ObjectID;
byte StatusID;
} struct_message;
// Create a struct_message called NextionInfo to hold Nextion settings
struct_message NextionInfo;
// Create a struct_message called IncomingInfo to hold incoming info
struct_message IncomingInfo;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status == 0)
{
success = "Delivery Success :)";
}
else
{
success = "Delivery Fail :(";
}
}
// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
struct_message *IncomingInfo = (struct_message *)incomingData;
Serial.println(WiFi.macAddress());
// memcpy(&IncomingInfo, incomingData, sizeof(IncomingInfo));
Serial.print("Bytes received: ");
Serial.println(len);
// incomingEventID = IncomingInfo.EventID;
Serial.print((byte)IncomingInfo->EventID, HEX);
Serial.print(' ');
// incomingPageID = IncomingInfo.PageID;
Serial.print((byte)IncomingInfo->PageID, HEX);
Serial.print(' ');
// incomingObjectID = IncomingInfo.ObjectID;
Serial.print((byte)IncomingInfo->ObjectID, HEX);
Serial.print(' ');
// incomingStatusID = IncomingInfo.StatusID;
Serial.println((byte)IncomingInfo->StatusID, HEX);
}
void emptyBuffer()
{
for (byte n = 0; n < numBytes; n++)
{
receivedBytes[n] = 0;
}
}
void recvBytesWithStartMarker()
{
static boolean recvInProgress = false;
static int ndx = -1;
static byte numBytesReceived = 0;
static byte mesgLen = 0;
const byte startMarker = 0x65;
byte rc;
while (Serial2.available() > 0 && newData == false)
{
rc = Serial2.read();
receivedBytes[numBytesReceived] = rc;
numBytesReceived++;
if (numBytesReceived > 33)
{
Serial.println("Error Rx : RESET !!");
Serial.println();
emptyBuffer();
numBytesReceived = 0;
newData = false;
}
if (recvInProgress == true)
{
if (numBytesReceived == typeBytePosition)
{
ndx = 0; // enable saving of data (anticipate good data)
if (rc == requestType)
{
mesgLen = requestLength;
}
else
{
recvInProgress = false; // abort - invalid request type
ndx = -1;
}
}
if (ndx >= 0)
{
ndx++;
}
if (numBytesReceived >= (mesgLen + typeBytePosition))
{ // got the whole message
recvInProgress = false;
newData = true;
}
}
else if (rc == startMarker)
{
emptyBuffer();
recvInProgress = true;
numBytesReceived = 0;
ndx = -1; // prevent counting valid bytes for the moment
}
}
}
// void getNexInfo()
// {
// NexEventID = receivedBytes[0];
// NexPageID = receivedBytes[1];
// NexObjectID = receivedBytes[2];
// NexStatusID = receivedBytes[3];
// // Serial.print(NexEventID&&NexPageID&&NexObjectID&&NexStatusID);
// }
void showNewData()
{
if (newData == true)
{
Serial.println(WiFi.macAddress());
Serial.print(requestType, HEX);
Serial.print(' ');
for (byte n = 0; n < typeBytePosition; n++) // n < numBytes
{
if (n == 0)
{
NexEventID = receivedBytes[n];
}
else if (n == 1)
{
NexPageID = receivedBytes[n];
}
else if (n == 2)
{
NexObjectID = receivedBytes[n];
}
else if (n == 3)
{
NexStatusID = receivedBytes[n];
}
Serial.print(receivedBytes[n], HEX);
Serial.print(' ');
}
//Serial.print(getNexInfo(),HEX);
Serial.println();
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&NextionInfo, sizeof(NextionInfo));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
newData = false;
}
}
void sendNewData()
{
// Set values to send
NextionInfo.EventID = NexEventID;
NextionInfo.PageID = NexPageID;
NextionInfo.ObjectID = NexObjectID;
NextionInfo.StatusID = NexStatusID;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&NextionInfo, sizeof(NextionInfo));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
}
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(250000);
Serial2.begin(115200);
while (!Serial)
{
;
}
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
//Setup has completed
Serial.println("<ESP32 is ready>");
}
void loop()
{
recvBytesWithStartMarker();
showNewData();
}
Most of this has come from various tutorials across the web including YouTube and RandomNerdTutorials. I am by no means a master programmer, but I am trying to learn.
Here are the results I get from the serial monitors:
ESP32 1:
�<ESP32 is ready>
AC:67:B2:36:AA:A8
65 1 2 1 FF FF FF
Sent with success
Last Packet Send Status: Delivery Success
ESP32 2:
AC:67:B2:35:19:D8
Bytes received: 4
0 0 0 0
Note that I am only sending 4 bytes of info (65 1 2 1), so it seems to match up (I'm not sending FF FF FF)...
Thanks again for any help!!
I was able to resolve my issue. Below is the code that displayed the same information that was sent from the host and received by the slave ESP32.
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
// Example to receive data with a start marker and length byte
// For ease of testing this uses upper-case characters A and B
// for the request and response types
// and Z for the start marker
// this version saves all the bytes after the start marker
// if the TYPE byte is wrong it aborts
const byte numBytes = 32;
byte receivedBytes[numBytes];
const byte typeBytePosition = 6; // Position after the start byte
const byte requestType = 0x65;
const byte requestLength = 7;
boolean newData = false;
int LED = 2;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Variable to store if sending data was successful
String success;
struct __attribute__((packed)) dataPacket
{
byte EventID;
byte PageID;
byte ObjectID;
byte StatusID;
} packet, *packetPtr;
const dataPacket onLoc1R = {0x65, 0x01, 0x01, 0x01};
const dataPacket offLoc1R = {0x065, 0x01, 0x01, 0x00};
const dataPacket onLoc1L = {0x65, 0x01, 0x02, 0x01};
const dataPacket offLoc1L = {0x65, 0x01, 0x02, 0x00};
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void emptyBuffer()
{
for (byte n = 0; n < numBytes; n++)
{
receivedBytes[n] = 0;
}
}
void recvBytesWithStartMarker()
{
static boolean recvInProgress = false;
static int ndx = -1;
static byte numBytesReceived = 0;
static byte mesgLen = 0;
const byte startMarker = 0x65;
byte rc;
while (Serial2.available() > 0 && newData == false)
{
rc = Serial2.read();
receivedBytes[numBytesReceived] = rc;
numBytesReceived++;
if (numBytesReceived > 33)
{
Serial.println("Error Rx : RESET !!");
Serial.println();
emptyBuffer();
numBytesReceived = 0;
newData = false;
}
if (recvInProgress == true)
{
if (numBytesReceived == typeBytePosition)
{
ndx = 0; // enable saving of data (anticipate good data)
if (rc == requestType)
{
mesgLen = requestLength;
}
else
{
recvInProgress = false; // abort - invalid request type
ndx = -1;
}
}
if (ndx >= 0)
{
ndx++;
}
if (numBytesReceived >= (mesgLen + typeBytePosition))
{ // got the whole message
recvInProgress = false;
newData = true;
}
}
else if (rc == startMarker)
{
emptyBuffer();
recvInProgress = true;
numBytesReceived = 0;
ndx = -1; // prevent counting valid bytes for the moment
}
}
}
void showNewData()
{
if (newData == true)
{
Serial.println(WiFi.macAddress());
Serial.print(requestType, HEX);
packet.EventID = requestType;
Serial.print(' ');
for (byte n = 0; n < typeBytePosition; n++) // n < numBytes
{
if (n == 0)
{
packet.PageID = receivedBytes[n];
}
else if (n == 1)
{
packet.ObjectID = receivedBytes[n];
}
else if (n == 2)
{
packet.StatusID = receivedBytes[n];
}
Serial.print(receivedBytes[n], HEX);
Serial.print(' ');
}
Serial.println();
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &packet, sizeof(packet));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
newData = false;
}
}
// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *data, int len)
{
Serial.println(WiFi.macAddress());
memcpy(&packet, data, sizeof(packet));
Serial.printf("%x %x %x %x\n",(byte) packet.EventID,(byte) packet.PageID,(byte) packet.ObjectID,(byte) packet.StatusID);
Serial.println();
}
void sendNewData()
{
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &packet, sizeof(packet));
if (result == ESP_OK)
{
Serial.println("Sent with success");
}
else
{
Serial.println("Error sending the data");
}
}
void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(250000);
Serial2.begin(115200);
while (!Serial)
{
;
}
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
//Setup has completed
Serial.println("<ESP32 is ready>");
}
void loop()
{
recvBytesWithStartMarker();
showNewData();
}

How can I get the value of +IPD response

The problem is the value of +IPD response isn't show up at the ReadLastEntryThingSpeak(), so my code isn't correct or I must add something? I have searched for many websites Arduino forum, Thingspeak forum, and others. But I didn't see any solution. :(
I have tried to do this system for 5 days and it doesn't work, why?
#include <SoftwareSerial.h>
#include <string.h>
#define esp8266 Serial1
#define SSID "test" // put here the name of your wifi network
#define PASS "lolman123"
#define IP "184.106.153.149"
int counter = 120;
String keep;
String valuetosend;
const byte maxDataLength = 30; // maxDataLength is the maximum length
allowed for received data.
char receivedChars[31] ;
boolean newData = false;
String GET = "GET /update?key=FYPW04TOYR3FX2QH&"; // put here your
thingspeak key
String GET1 = "field1=";
String readGET1 = "GET
https://api.thingspeak.com/channels/864152/fields/1/last";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
esp8266.begin(115200);
esp8266.println("AT");
delay(5000);
if(esp8266.find("OK")){
connectWiFi();
Serial.println("WIFI CONNECTED");
}
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
newData = false;
}
void loop() {
// put your main code here, to run repeatedly:
int readBtn = digitalRead(7);
int readSend = digitalRead(8);
if (readBtn == 0)
{
ReadLastEntryThingSpeak();
}
if (readSend == 0)
{
WriteThingSpeak();
}
}
boolean connectWiFi(){
esp8266.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
esp8266.println(cmd);
delay(5000);
if(esp8266.find("OK")){
Serial.println("OK");
return true;
}else{
Serial.println("KO");
return false;
}
}
void updateFunction(String valuetosend){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error")){
Serial.print("Error1");
return;
}
cmd = GET + GET1;
cmd += valuetosend;
cmd += "\r\n";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
}
void ReadLastEntryThingSpeak()
{
String cmd = "AT+CIPSTART=\"TCP\",\""; //connect to thingspeak
cmd += IP;
cmd += "\",80";
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error")){
Serial.print("Error1");
return;
}
cmd = readGET1;
cmd += "\r\n";
Serial.print(cmd);
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
esp8266.print(cmd);
}else{
esp8266.println("AT+CIPCLOSE");
}
delay(2000);
recvWithStartEndMarkers(); // check to see if we have
received any new commands
if (newData) { processCommand(); }
}
void WriteThingSpeak ()
{
String valuetosend = String(counter);
updateFunction(valuetosend);
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ':';
char endMarker = 'C';
if (esp8266.available() > 0)
{
char rc = esp8266.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
if (ndx < maxDataLength) { receivedChars[ndx] = rc;
ndx++; }
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) { recvInProgress = true; }
}
}
void processCommand()
{
Serial.print("Recieved data = "); Serial.println(receivedChars);
newData = false;
}
I have the result of Received data = , but the actual output is Received data = 120.

Arduino Sketch - Reading Serial Bytes

I have the following code on my Arduino that constantly checks for a serial command that's sent over TCP using a Wifly library.
What the following code does is split a string like the following when sent over serial:
{power,tv}
It sets these properties accordingly:
char command[32];
char value[32];
It then executes certain methods using sendCommand(command, value); based on the properties set in the loop below.
Keep in mind this works just fine using the Wifly library.
void loop() {
Client client = server.available();
if (client) {
boolean start_data = false;
boolean next = false;
char command[32];
char value[32];
int index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
if (c == '}') {
break;
}
if(start_data == true) {
if(c != ',') {
if(next)
value[index] = c;
else
command[index] = c;
index++;
} else {
next = true;
command[index] = '\0';
index = 0;
}
}
if (c == '{') {
start_data = true;
}
}
}
value[index] = '\0';
client.flush();
client.stop();
sendCommand(command,value);
}
}
Instead of using WiFi I've purchased some Xbee modules. They basically allow you to send serial bytes as well. The only problem is that I'm not quite sure how to handle the looping considering there's no while(client.connected()) anymore. Instead of that I've used while(Serial.available()) thinking that will work, but it doesn't set the value property for some reason.
I get command but I don't get value.
Also I'm not sure whether the loop above is the best way of doing what I'm after, all I know is that it works just fine the way it is. :)
Here is my new loop, which only returns command and not value for some reason:
void loop() {
// if there are bytes waiting on the serial port
if (Serial.available()) {
boolean start_data = false;
boolean next = false;
char command[32];
char value[32];
int index = 0;
while (Serial.available()) {
char c = Serial.read();
Serial.print(c);
if (c == '}') {
break;
}
if(start_data == true) {
if(c != ',') {
if(next)
value[index] = c;
else
command[index] = c;
index++;
} else {
next = true;
command[index] = '\0';
index = 0;
}
}
if (c == '{') {
start_data = true;
}
}
value[index] = '\0';
sendCommand(command,value);
}
}
If the following works with the new loop, I'll be very happy!
void sendCommand(char *command, char *value) {
// do something wonderful with command and value!
}
Got it working by using the following code:
#define SOP '{'
#define EOP '}'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(9600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
char *cmd = strtok(inData, ",");
if(cmd)
{
char *val = strtok(NULL, ",");
if(val)
{
sendCommand(cmd, val);
}
}
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
I would change the structure similar to:
while( c != '}') {
if (Serial.available()) {
.
.
.
}
}
Serial characters are received significantly slower then the loop.

Arduino making decision according to a packet received from serial port

The program listen to messages from serial port in the form or where first character (A or D) means analog or digital, the 2nd character - pin, the 3rd character - 1/0 or from 0 to 255. The markers < and > show the beginning and the end of packet.
For example, if packet is received, the light is turned on by digitalWrite(13,1)
But nothing happens. When I send via serial monitor, for instance: light is supposed to blink but it does not. The same with analogue outputs.
bool started = false;
bool ended = false;
char inData[5];
byte index;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '<')
{
index = 0;
started = true;
ended = false;
}
else if (inChar == '>')
{
ended = true;
break;
}
else
{
if (index <= 4)
{
inData[index] = inChar;
index++;
}
}
if (started && ended)
{
if (inData[0] == 'A')
{
pinMode(inData[2],OUTPUT);
analogWrite(inData[2],inData[4]);
}
else if (inData[0] == 'D')
{
if (inData[4] == 1)
{
pinMode(inData[2],OUTPUT);
digitalWrite(inData[2],HIGH);
}
else if (inData[4] == 0)
{
pinMode(inData[2],OUTPUT);
digitalWrite(inData[2],LOW);
}
}
started = false;
ended = false;
index = 0;
}
}
Serial.println("Sending");
}
The following code will allow you to execute a method with an example serial string:
<power,led>
Once it processes this string, it'll execute the following method:
sendCommand(cmd, val);
See below for an example of how to turn on an LED on PIN 11.
#include <avr/pgmspace.h>
int IRledPin = 11;
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup() {
pinMode(IRledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read all serial data available, as fast as possible
while (Serial.available() > 0) {
char inChar = Serial.read();
if (inChar == SOP) {
index = 0;
inData[index] = '\0';
started = true;
ended = false;
} else if (inChar == EOP) {
ended = true;
break;
} else {
if (index < 79) {
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if (started && ended) {
// The end of packet marker arrived. Process the packet
char *cmd = strtok(inData, ",");
if (cmd) {
char *val = strtok(NULL, ",");
if (val) {
sendCommand(cmd, val);
}
}
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
void sendCommand(char *command, char *value) {
if (strcmp(command,"power") == 0) {
power(value);
}
}
void power(char* value) {
if (strcmp(value, "led") == 0) {
digitalWrite(IRledPin, HIGH);
}
}
If the 2nd character is the pin, then you want inData[1] for your pin numbers instead of inData[2].
Why do you go from inData[0] to inData[2]? Wouldn't the second character be in inData[1]?
You're setting the pinMode to the actual value of inData[2]. That means to turn on pin 13, you need to send a carriage return character ('\r').
The code doesn't run below but it should help you to sort out your problem.
What it tries to do is split the inData into the Tokens[] array.
It then turns the ASCII data into integers with the atoi() statement.
Hope it helps.
Got the splitter from Segmentation Fault when using strtok_r
bool started = false;
bool ended = false;
char inData[5];
byte index;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '<')
{
index = 0;
started = true;
ended = false;
}
else if (inChar == '>')
{
ended = true;
break;
}
else
{
inData[index] = inChar;
index++;
}
if (started && ended)
{
// https://stackoverflow.com/questions/2227198/segmentation-fault-when-using-strtok-r
// Splint up input data
char *p = inData;
char *tokens[50];
int i = 0;
while (i < 50) {
tokens[i] = strtok_r(p, ",", &p);
if (tokens[i] == NULL) {
break;
}
i++;
}
if (tokens[0] == '<A')
{
pinMode(tokens[1],OUTPUT);
analogWrite(tokens[2],tokens[3]);
}
else if (token[0] == '<D')
{
if (atoi(token[3]) == 1)
{
pinMode(atoi(token[1]),OUTPUT);
digitalWrite(atoi(token[1]),HIGH);
}
else if (atoi(tokens[3]) == 0)
{
pinMode(atoi(tokens[1]),OUTPUT);
digitalWrite(atoi(tokens[1]),LOW);
}
}
started = false;
ended = false;
index = 0;
}
}

Why recv() in winsock client cannot get any data once httpRetransmition happens?

I am trying to record the time between 'http request' package and 'http response' package.
I write an socket client using winsock. The code is below
if (send(sock, request.c_str(), request.length(), 0) != request.length())
die_with_error("send() sent a different number of bytes than expected");
// Record the time of httpRequestSent
::QueryPerformanceCounter(&httpRequestSent);
::QueryPerformanceFrequency(&frequency);
//get response
response = "";
resp_leng= BUFFERSIZE;
http_leng= 381;
while(resp_leng==BUFFERSIZE||http_leng>0)
{
resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
http_leng= http_leng - resp_leng;
if (resp_leng>0)
response+= string(buffer).substr(0,resp_leng);
//note: download lag is not handled in this code
}
::QueryPerformanceCounter(&httpResponseGot);
//display response
cout << response << endl;
// Display the HTTP duration
httpDuration = (double)(httpResponseGot.QuadPart - httpRequestSent.QuadPart) / (double)frequency.QuadPart;
printf("The HTTP duration is %lf\n", httpDuration);
The code works nicely except one situation: HTTP Retransmition. I used wireshark to monitor packages and found out once there is a retransmition the code seems block on recv(), but cannot get any data from the socket buffer. I wonder why would this happen. Could somebody explain the reasons?
Any help will be appreciated.
Here is a second answer with more dynamic buffer handling and more error checking:
void send_data(SOCKET sock, void *data, unsigned int data_len)
{
unsigned char *ptr = (unsigned char*) data;
while (data_len > 0)
{
int num_to_send = (int) std::min(1024*1024, data_len);
int num_sent = send(sock, ptr, num_to_send, 0);
if (num_sent < 0)
{
if ((num_sent == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
continue;
die_with_error("send() failed");
}
if (num_sent == 0)
die_with_error("socket disconnected");
ptr += num_sent;
data_len -= num_sent;
}
}
unsigned int recv_data(SOCKET sock, void *data, unsigned int data_len, bool error_on_disconnect = true)
{
unsigned char *ptr = (unsigned char*) data;
unsigned int total = 0;
while (data_len > 0)
{
int num_to_recv = (int) std::min(1024*1024, data_len);
int num_recvd = recv(sock, ptr, num_to_recv, 0);
if (num_recvd < 0)
{
if ((num_recvd == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
continue;
die_with_error("recv() failed");
}
if (num_recvd == 0)
{
if (error_on_disconnect)
die_with_error("socket disconnected");
break;
}
ptr += num_recvd;
datalen -= num_recvd;
total += num_recvd;
}
while (true);
return total;
}
std::string recv_line(SOCKET sock)
{
std::string line;
char c;
do
{
recv_data(sock, &c, 1);
if (c == '\r')
{
recv_data(sock, &c, 1);
if (c == '\n')
break;
line += '\r';
}
else if (c == '\n')
break;
line += c;
}
return line;
}
void recv_headers(SOCKET sock, std::vector<std::string> *hdrs)
{
do
{
std::string line = recv_line(sock);
if (line.length() == 0)
return;
if (hdrs)
hdrs->push_back(line);
}
while (true);
}
unsigned int recv_chunk_size(SOCKET sock)
{
std::string line = recv_line(sock);
size_t pos = line.find(";");
if (pos != std::string::npos)
line.erase(pos);
char *endptr;
unsigned int value = strtoul(line.c_str(), &endptr, 16);
if (*endptr != '\0')
die_with_error("bad Chunk Size received");
return value;
}
std::string find_header(const std::vector<std::string> &hrds, const std::string &hdr_name)
{
std::string value;
for(size_t i = 0; i < hdrs.size(); ++i)
{
const std::string hdr = hdrs[i];
size_t pos = hdr.find(":");
if (pos != std::string::npos)
{
if (hdr.compare(0, pos-1, name) == 0)
{
pos = hdr.find_first_not_of(" ", pos+1);
if (pos != std::string::npos)
return hdr.substr(pos);
break;
}
}
}
return "";
}
{
// send request ...
std::string request = ...;
send_data(sock, request.c_str(), request.length());
// Record the time of httpRequestSent
::QueryPerformanceCounter(&httpRequestSent);
::QueryPerformanceFrequency(&frequency);
// get response ...
std::vector<std::string> resp_headers;
std::vector<unsigned char> resp_data;
recv_headers(sock, &resp_headers);
std::string transfer_encoding = find_header(resp_headers, "Transfer-Encoding");
if (transfer_encoding.find("chunked") != std::string::npos)
{
unsigned int chunk_len = recv_chunk_size(sock);
while (chunk_len != 0)
{
size_t offset = resp_data.size();
resp_data.resize(offset + chunk_len);
recv_data(sock, &resp_data[offset], chunk_len);
recv_line(sock);
chunk_len = recv_chunk_size(sock);
}
recv_headers(sock, NULL);
}
else
{
std::string content_length = find_header(resp_headers, "Content-Length");
if (content_length.length() != 0)
{
char *endptr;
unsigned int content_length_value = strtoul(content_length.c_str(), &endptr, 10);
if (*endptr != '\0')
die_with_error("bad Content-Length value received");
if (content_length_value > 0)
{
resp_data.resize(content_length_value);
recv_data(sock, &resp_data[0], content_length_value);
}
}
else
{
unsigned char buffer[BUFFERSIZE];
do
{
unsigned int buffer_len = recv_data(sock, buffer, BUFFERSIZE, false);
if (buffer_len == 0)
break;
size_t offset = resp_data.size();
resp_data.resize(offset + buffer_len);
memcpy(&resp_data[offset], buffer, buffer_len);
}
while (true)
}
}
::QueryPerformanceCounter(&httpResponseGot);
// process resp_data as needed
// may be compressed, encoded, etc...
// Display the HTTP duration
httpDuration = (double)(httpResponseGot.QuadPart - httpRequestSent.QuadPart) / (double)frequency.QuadPart;
printf("The HTTP duration is %lf\n", httpDuration);
}
You are not doing adequate error checking on the calls to send() and recv(). Try something like this instead:
char *req_ptr = request.c_str();
int req_leng = request.length();
int req_index = 0;
do
{
int req_sent = send(sock, req_ptr, req_leng, 0);
if (req_sent < 1)
{
if ((req_sent == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
continue;
die_with_error("send() failed");
}
req_ptr += req_sent;
req_leng -= req_sent;
}
while (req_leng > 0);
// Record the time of httpRequestSent
::QueryPerformanceCounter(&httpRequestSent);
::QueryPerformanceFrequency(&frequency);
//get response
std::string response;
int resp_leng = BUFFERSIZE;
int http_leng = -1;
bool http_leng_needed = true;
do
{
if (http_leng_needed)
{
std::string::size_type pos = response.find("\r\n\r\n");
if (pos != std::string::npos)
{
std::string resp_hdrs = response.substr(0, pos);
// look for a "Content-Length" header to see
// if the server sends who many bytes are
// being sent after the headers. Note that
// the server may use "Transfer-Encoding: chunked"
// instead, which has no "Content-Length" header...
//
// I will leave this as an excercise for you to figure out...
http_leng = ...;
// in case body bytes have already been received...
http_leng -= (response.length() - (pos+4));
http_leng_needed = false;
}
}
if (http_leng_needed)
resp_leng = BUFFERSIZE;
else
resp_leng = min(http_leng, BUFFERSIZE);
if (resp_leng == 0)
break;
resp_leng = recv(sock, buffer, resp_leng, 0);
if (resp_leng < 1)
{
if ((resp_leng == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
continue;
die_with_error("send() failed");
}
response += string(buffer, resp_leng);
if (!http_leng_needed)
http_leng -= resp_leng;
}
while ((http_leng_needed) || (http_leng > 0));
::QueryPerformanceCounter(&httpResponseGot);
//display response
cout << response << endl;
// Display the HTTP duration
httpDuration = (double)(httpResponseGot.QuadPart - httpRequestSent.QuadPart) / (double)frequency.QuadPart;
printf("The HTTP duration is %lf\n", httpDuration);
With this said, the "correct" way to handle HTTP in general is to read the inbound data line by line, rather than buffer by buffer, until you encounter the end of the response headers, then you can read the rest of the data buffer by buffer based on the data length indicated by the headers.

Resources