Post topics, source code that relate to the Arduino Platform

User avatar
By DevStef
#23317 Hello everyone,

I'm working on a code to connect to the openweathermap.org api and display the weather information on a LCD via another arduino board (I wasn't able to make the I2C Bus work for the esp8266).

The code is kinda dirty right now and has a few old things in it, which should not be troubling though. So far it's kind of working. I can connect to the api and collect my data. Then I send it to the Arduino mega and it displays the data/numbers on my lcd. For some reason this is only working for the forecast. I can not make it work for the current weather information.

I hope someone can help me out here, cause I'm running out of ideas.

Working Code so far:

Code: Select all//#include <ArduinoJson.h>

#include <ESP8266WiFi.h>

//#define LOCATIONID "<LOCATION>" // location id
//#define API "http://api.openweathermap.org/data/2.5/forecast/city?id=2947416&APPID=..." //http://api.openweathermap.org/
 
const char* ssid = "SSIDNAME";//"mytest";
const char* password = "SSIDPASSWORD"; //"123456789";
 
int ledPin = 13; // GPIO13
//WiFiServer server(80);
WiFiClient client;

int wetter = 0;
float temp = 0.0;
float wind = 0.0;

//StaticJsonBuffer<200> jsonBuffer;
//JsonParser<32> parser;

char server[] = "openweathermap.org";
//char weather[544];
 
void setup() {
  Serial.begin(9600);
  delay(10);
 
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
   
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
   
  WiFi.begin(ssid, password);
   
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
   
  // Start the server
  //server.begin();
  //Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  }
   
  /*WiFi.begin(ssid, password);
   
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
   */
  /*WiFi.mode(WIFI_STA);
  WiFi.softAP(ssid, password);
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.softAPIP());
  Serial.println("/");*/
 
void loop() {
  bool onlygiveone = false;
    if (client.connect(server, 80))
    {
    //Serial.println("connected to server");
    // Make a HTTP request:
    client.println(F("GET /data/2.5/forecast?id=2947416 HTTP/1.1"));  // <- works. http://api.openweathermap.org/data/2.5/weather?id=2947416 <- does not work.
    client.println("Host: api.openweathermap.org");
    //client.println("Content-Type: text/html");
    client.println("Connection: close");
    client.println();
    }
 onlygiveone = false;   
 while (client.available()) {
    //char c = client.read();
    if (onlygiveone == false)
    {
      client.find("temp");
      temp = client.parseFloat() - 273.15;
      client.find("weather");
      wetter = client.parseInt();
      client.find("speed");
      wind = client.parseFloat();
      client.stop();
      onlygiveone=true;
    }
  }
  Serial.println(String(temp));
  Serial.println(String(wetter));
  Serial.println(String(wind));
  delay(60000);

  /*// if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
    while(true)
    {};
  }*/

}


When connecting to http://api.openweathermap.org/data/2.5/ ... id=2947416 it tells me, that it connected but for some reason the whole respond is empty, like there is nothing to be read.
User avatar
By Stoney
#24836 cant help with the openweather yet< i subscribed last week but have not looked into it.

I have however got an I2C 128 x 64 OLED working happily on an esp01.

includes are

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// need an initialiser like this, my display does not use a reset line but the adafruit source does so it is just a dummy at this stage

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

setup for the display has these lines ..

Wire.pins(0, 2);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
display.display();
delay(1000);
display.clearDisplay();

display.setTextSize(1); //21 char x 7 lines
//display.setTextSize(2); //10 char x 4 lines
//display.setTextSize(3); //7 char x 2 lines
display.setTextColor(WHITE);
display.setCursor(0, 0);

and loop code ...

display.print("Z");
display.display();

hardware requirements ..
make sure your display board has pullup resistors ..
make sure the address makes sense, you may have to edit the address value in the adafruit library code, I did .. from 3d to 3c from memory.
User avatar
By AdaBill
#25036 Stoney,

Can you post your code. I learn a lot about how to use the devices by looking at working code. I think the OLED is a great addition to the ESP8266, I'm using a 12E . The only way I have been able to use the OLED so far is with code that only allows one size. Small. 16 colmuns x 8 rows. I also bought a 1.3" OLED which should make it a little easier to read. An LCD is oversized for this ESP8266 if you are trying to keep a small footprint with internet capabilities. I didn't need to connect the resistors for the CLK and Data lines. I tried with and without and there was no difference. I only have the one I2C device connected. You might need to add them if more I2C devices are used.
User avatar
By Stoney
#25043
AdaBill wrote:Stoney,

Can you post your code. I learn a lot about how to use the devices by looking at working code. I think the OLED is a great addition to the ESP8266, I'm using a 12E . The only way I have been able to use the OLED so far is with code that only allows one size. Small. 16 colmuns x 8 rows. I also bought a 1.3" OLED which should make it a little easier to read. An LCD is oversized for this ESP8266 if you are trying to keep a small footprint with internet capabilities. I didn't need to connect the resistors for the CLK and Data lines. I tried with and without and there was no difference. I only have the one I2C device connected. You might need to add them if more I2C devices are used.


I will start a thread for it later, I pulled the I2C stuff from the code by itself as I did not want to muddy the waters with the rest of a much longer script.
Above is everything in there related to the oled.

Are you using an oled on a pcb already ? They generally have the resistors on the carrier board, 4k7 or 5k2, since I was using a second i2c device (BMP180) on the bus I removed the resistors on one oled as I did not need a second set of pull up. I forgot at one point and that oled would not work without the bmp in place.

the esp has high value pull up resistors internally and I tried using them instead, still did not work most of the time as I think the init code would fail, if I used pullup resistors on a breadboard it woukd initialise, then remove them and it could refresh the screen without them but I did get some dots on screen. Since screen is inverted from data then without pullups and using intenal pullup it was reading 1 as 0 at times. Touching the wires caused far more corruption.