Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert String to Char not work correctly #75

Closed
Hawkfree opened this issue Dec 12, 2018 · 3 comments
Closed

convert String to Char not work correctly #75

Hawkfree opened this issue Dec 12, 2018 · 3 comments

Comments

@Hawkfree
Copy link

I have program to sending data to server.
1) use clasic bootloader - the server receives a correct data
log: 192.168.0.106 - - [12/Dec/2018:09:31:53 +0100] "GET /arduino/elektromer.php?ID=AR06&t1=12&spotreba=12558&tarif=1" 200 2 "-" "-"
2) use minicore bootloader - the server NOT receives a correct data
log: 192.168.0.106 - - [12/Dec/2018:09:30:45 +0100] "GET /arduino/elektromer.php?ID=AR06&t\x06\xa8\b\xdc\xa5\x03\x122\x04\xa6\xbe\xca\x0f\x10\xdd\b'\x04l\x03\xba\b\xa5\x03K\x04\x98\x01 HTTP/1.0" 400 981 "-" "-"

`void sendPacket(String dataString){

char dataChar[50];
dataString.toCharArray(dataChar, 50);
ether.browseUrl(PSTR("/arduino/elektromer.php?"), dataChar, server, callback);

}`

Thank you for your advice.

@MCUdude
Copy link
Owner

MCUdude commented Dec 12, 2018

Can you isolate the problem please (smallest code possible while still being able to demonstrate the problem), and then upload the code so I'm able to test it myself?

@Hawkfree
Copy link
Author

Hawkfree commented Dec 13, 2018

Here's the code:

/* 
 *  Test program
 *  seding data to server 192,168,0,104
*/
#include <EtherCard.h>
#include <avr/wdt.h>
/********************************************************************/
#define DEBUG (true)

String ArduinoID= "ID=03";
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x33,0x43 };
static byte myip[] = { 192,168,0,36 };
static byte gwip[] = { 192,168,0,1 };
static byte server[] = { 192,168,0,104 };

byte Ethernet::buffer[400]; // tcp ip send and receive buffer
int t,power;

unsigned long timer = millis(); 
uint16_t ethPaket;
String dataString;
const char pageOk[] PROGMEM ="HTTP/1.0 200 OK\r\n Content-Type: text/html\r\n\r\nOK";
/////////////////////////////////////////////////////////////////// called when the client request is complete
static void callback (byte status, word off, word len) {
  Ethernet::buffer[off+300] = 0;
  if DEBUG Serial.println((const char*) Ethernet::buffer + off);   
}
////////////////////////////////////////////////////////////////////////////////////////
void setup () {
  if DEBUG Serial.begin(115200);
  if DEBUG Serial.println(F("Start - DEBUG ON"));
  wdt_enable(WDTO_4S);
  init_ethernet();
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////
void loop () { 

  ethPaket = ether.packetLoop(ether.packetReceive());
  if(ethPaket) prijemPaketu();//dotaz na http
  wdt_reset();         
  if (millis() > timer + 5000) 
  {
    timer = millis();
    power=random(1, 5000);
    t=random(-50, 50);

    dataString = ArduinoID + "&t1=" + t + "&power=" + power + "\r\n";
    odeslaniPaketu(dataString);
  } 
}//end loop
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////
void odeslaniPaketu(String dataString){

  char dataChar[dataString.length()+1];
  dataString.toCharArray(dataChar, dataString.length()); 
  if DEBUG Serial.println(dataChar);    
  if DEBUG Serial.print(F("<<< REQ "));

  ether.browseUrl(PSTR("/shome/arduino/elektromer.php?"), dataChar, server, callback);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
void prijemPaketu(){
  if DEBUG Serial.println(F("<<<------------->>>"));      
  char* data = (char *) Ethernet::buffer + ethPaket;

  if DEBUG Serial.println(data);

  memcpy_P(ether.tcpOffset(), pageOk, sizeof pageOk);
  ether.httpServerReply(sizeof pageOk - 1);
}
////////////////////////////////////////////////////////////////////////////////////////////////
void init_ethernet(void){  
  if (ether.begin(sizeof Ethernet::buffer, mymac,10)== 0) 
    if DEBUG Serial.println(F("Failed Ethernet controller"));

  if (!ether.dhcpSetup()){
    ether.staticSetup(myip, gwip);
    if DEBUG Serial.println(F("No DHCP, Static IP!"));
  }
  
  ether.copyIp(ether.hisip, server);
      
  if DEBUG{ 
      ether.printIp("IP: ", ether.myip);
      ether.printIp("GW: ", ether.gwip);
      ether.printIp("DNS: ", ether.dnsip);
      ether.printIp("DHCP: ", ether.dhcpip); // output IP address of the DHCP server
      ether.printIp("SRV: ", ether.hisip);
  } 
}
////////////////////////////////////////////////////////////////////////////////////////////////

@Hawkfree
Copy link
Author

Hawkfree commented Dec 13, 2018

I found the solution:
Convert data in loop{} not before sending packet.

global declaration:
char dataChar[51];

///////////////////////////////////////////////////////////////////////////////////////////////////////
void loop () { 
  ethPaket = ether.packetLoop(ether.packetReceive());
  if(ethPaket) prijemPaketu();
  wdt_reset();         
  if (millis() > timer + 5000) 
  {
    timer = millis();
    power=random(1, 5000);
    t=random(-50, 50);

    dataString = ArduinoID + "&t1=" + t + "&power=" + power + "\r\n";
    dataString.toCharArray(dataChar, 50); 
    
    odeslaniPaketu(dataChar);
  } 
}//end loop

//////////////////////////////////////////////////
void odeslaniPaketu(char dataChar[]){
  ether.browseUrl(PSTR("/shome/arduino/elektromer.php?"), dataChar, server, callback);
}

@MCUdude MCUdude closed this as completed Feb 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants