UNO + ENC28J60

artur_fcsm
Offline
Зарегистрирован: 12.02.2015

Приветствую друзья. Большая просьба подсказать.

Стоит две задачи

1. Отправлять запрос на сайт вида http://shop.igri-razuma.ru/ym.php?number=10 , где последние две циры - это переменная. Правильный url я смог сформировать, но возвращается ошибка BAD REQUIEST. Если url ввожу в ручную - все работает.

2. Забрать данные с сайта. Здесь пока никаких продвижений, подскажите примерчик пожалуйста.

Надеюсь на Вашу помощь.

#include <SPI.h>
#include <EtherCard.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[500];
static uint32_t timer;
const char website[] PROGMEM = "shop.igri-razuma.ru";

static void my_callback (byte status, word off, word len) {
  Serial.println(">>>");
  Ethernet::buffer[off+300] = 0;
  Serial.print((const char*) Ethernet::buffer + off);
  Serial.println("...");
}

static void get_time (char number) {
    Serial.println();
    Serial.print("<<< REQ ");
    char url[] = "/ym.php?number=???????";
    char * nHolder = strchr(url, '=') + 1;
    dtostrf(10, -1, 0, nHolder );
    Serial.print("URL=");
    Serial.println(url);
    
   // с этой строкой работает
    //ether.browseUrl(PSTR("http://shop.igri-razuma.ru/ym.php?number=52"),"", website, my_callback);
    //а с этой не хочет, хотя url верный
//ether.browseUrl(url,"", website, my_callback);

}
void setup() {
  Serial.begin(9600);

    // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
    Serial.println(F("Failed to access Ethernet controller"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));

      ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);
#if 1
  // use DNS to resolve the website's IP address
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
#elif 2
  // if website is a string containing an IP address instead of a domain name,
  // then use it directly. Note: the string can not be in PROGMEM.
  char websiteIP[] = "192.168.1.1";
  ether.parseIp(ether.hisip, websiteIP);
#else
  // or provide a numeric IP address instead of a string
  byte hisip[] = { 192,168,1,1 };
  ether.copyIp(ether.hisip, hisip);
#endif

  ether.printIp("SRV: ", ether.hisip);
 
  
}



void loop() {


    ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    timer = millis() + 5000;
     get_time("52");
  }
    
}

 

artur_fcsm
Offline
Зарегистрирован: 12.02.2015

// с этой строкой работает

ether.browseUrl(PSTR("/ym.php?number=52"),"", website, my_callback);

artur_fcsm
Offline
Зарегистрирован: 12.02.2015

Когда вывожу через такой код

ether.browseUrl(PSTR("/ym.php?number=52"),"", website, my_callback);

Ответ такой:

IP:  192.168.1.39
GW:  192.168.1.1
DNS: 192.168.1.1
SRV: 37.140.192.251
 
<<< REQ URL=/ym.php?number=10
>>>
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sat, 11 Aug 2018 11:52:38 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 30
Connection: close
X-Powered-By: PHP/5.3.28
 
12 км.&nbsp&nbsp&nbsp1:05:40...
 
<<< REQ URL=/ym.php?number=10
>>>
HTTP/1.1 200 OK
Server: nginx/1.14.0
Date: Sat, 11 Aug 2018 11:52:44 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 30
Connection: close
X-Powered-By: PHP/5.3.28
 
12 км.&nbsp&nbsp&nbsp1:05:40...
 
artur_fcsm
Offline
Зарегистрирован: 12.02.2015

Когда вывожу через такой код:

    char url[] = "/ym.php?number=???????";
    char * temperatureHolder = strchr(url, '=') + 1;
    dtostrf(10, -1, 0, temperatureHolder);
    Serial.print("URL=");
    Serial.println(url);
    ether.browseUrl(url,"", website, my_callback);

Ответ такой:

IP:  192.168.1.39
GW:  192.168.1.1
DNS: 192.168.1.1
SRV: 37.140.192.251
 
<<< REQ URL=/ym.php?number=10
>>>
HTTP/1.1 400 Bad Request
Server: nginx/1.14.0
Date: Sat, 11 Aug 2018 11:56:29 GMT
Content-Type: text/html
Content-Length: 173
Connection: close
 
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.14.0</ce...
 
artur_fcsm
Offline
Зарегистрирован: 12.02.2015

Проблему 1 решил следующим кодом:

static void get_time (char * number) {
    Serial.println();
    Serial.print("<<< REQ ");
    char url[] = "?number=???????";
    number = strchr(url, '=') + 1;
    dtostrf(10, -1, 0, number);
    Serial.print("URL=");
    Serial.println(url);
    ether.browseUrl(PSTR("/ym.php"),url, website, my_callback);

 

Проблема 2 остается. Подскажите пожалуйста как вытащить нужную информацию с ответа.