I'm new to this. I have a board nodemcu-v3-esp8266-12e with a small screen.
I can display text (using u8g2, rather than Serial like in most examples).
I can connect to wifi.
What I want to do eventually is get weather forecasts and display to screen. I think this requires ESP8266HTTPClient, sending GET requests. I have trouble getting good response codes from wttr.in, so I am starting with asking for IP addresses using two websites you'll see below. These give response code 200.
My current problem is that the payload from getString prints out as a few (or often just one) nonsense characters. From "curl -v www.icanhazip.com", I expect the payload to be "Content-Type: text/plain", so I hoped that http.getString() would get me a String, which I think I have to convert to char[] to print.
I guess some questions are:
• Where are there examples I can follow? So many examples seem to assume internal or non-existent servers.
• How can I get a plain-text payload?
• If that is too easy, I'd welcome pointers on how to split up multiline payloads, assuming I get there eventually.
• Also, I've run out of ideas how to get any response from wttr.in -- I've tried "http://wttr.in" and "http://5.9.243.187" but they give code 301. I'm hoping that I won't need JSON or HTTPS.
Thanks!
Current code (which gives me response codes 200, and responses such as "ô" and "ä"):
#include <Arduino.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char *ssid = "MY_SSID";
const char *password = "MY_PASSWORD";
WiFiClient client;
/* This connects to the screen: */
U8G2_SSD1306_128X64_NONAME_F_SW_I2C
u8g2(U8G2_R0, 14, 12, U8X8_PIN_NONE);
#define LEN_STRINGO 30
char stringo[LEN_STRINGO];
#define FONT u8g2_font_6x12_te
#define TEXT_Y_TOP 10
#define TEXT_Y_JUMP 13
#define TEXT_Y_MAX 63
int text_y = TEXT_Y_TOP;
#define NEWSCREEN(delayo) do{ \
delay(delayo); \
u8g2.clearBuffer(); \
text_y = TEXT_Y_TOP; \
}while(0)
#define WRITE_LINE(...) do{ \
if(text_y > TEXT_Y_MAX) NEWSCREEN(1000); \
sprintf(stringo,__VA_ARGS__); \
u8g2.drawStr(0,text_y,stringo); \
u8g2.sendBuffer(); \
text_y += TEXT_Y_JUMP; \
}while(0)
void
get_wifi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
WRITE_LINE("wifi search...");
}
}
void
setup()
{
char serverName[100];
HTTPClient http;
int httpCode;
String payload;
u8g2.begin();
u8g2.setFont(FONT);
NEWSCREEN(0);
get_wifi();
NEWSCREEN(0);
sprintf(serverName,"http://httpbin.org/ip");
http.begin(client,serverName); /* client is type WiFiClient, declared globally */
httpCode = http.GET();
WRITE_LINE("%d:%s", httpCode,serverName);
payload = http.getString();
WRITE_LINE("%s",payload);
http.end();
sprintf(serverName,"http://icanhazip.com");
http.begin(client,serverName);
httpCode = http.GET();
WRITE_LINE("%d:%s", httpCode,serverName);
WRITE_LINE("%s",http.getString());
http.end();
}
void
loop()
{ // nothing here
}