The use of the ESP8266 in the world of IoT

User avatar
By schufti
#74713 beeing able to send data with one browser but not the other might indeed point towards proxy being involved. Some browsers come with "auto proxy discovery" but not esp, so you need information about proxy, especially name/ip and port.

Most recent proxies can be used like this:
direct your client to the proxy
Code: Select allif (myClient.connect(proxy_name,proxy_port)){ ...

so you contact proxy instead of thingspeak and proxy reads real server credentials from
Code: Select allmyClient.print("Host: api.thingspeak.com:80\n");

to connect you to wanted server on internet.
User avatar
By Dan Kiefer
#74763 Finally I was able to make it work. I do not yet know what was the error, but when I broke the problem down to just the essential lines of code, it worked fine. Here's the code, including the ping and the reception of the response to the POST:

Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>                    // https://github.com/dancol90/ESP8266Ping
#include "DHT.h"                            // https://github.com/adafruit/DHT-sensor-library

#define DHTPIN 12                           // Pin 12 for DHT22
#define DHTTYPE DHT22                     

DHT dht(DHTPIN, DHTTYPE);                       

const char* ssid      = "myNetworkName";    // Name of the WLAN network
const char* password  = "myPassword";       // Password of the WLAN network
const char* server    = "api.thingspeak.com";
String apiKey         = "xxxxyyyyyyyyxxxx"; // Write key from thingspeak channel

const IPAddress remote_ip(208, 67, 222, 222);// Address of OpenDNS

float humidity;
float temperature;
boolean error;
   
WiFiClient myClient;

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid,password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println();
  Serial.print("Pinging ip: ");
  Serial.println(remote_ip);

  if(Ping.ping(remote_ip)){Serial.println("Ping PASS");}
  else                    {Serial.println("Ping FAIL");}
}

void loop() {
  if (0 == (millis()%20000)){
    humidity    = dht.readHumidity();
    temperature = dht.readTemperature(); 

    if ((isnan(humidity) || isnan(temperature))){
      Serial.println("Error reading DHT22");
    }
    else {
      if (myClient.connect(server,80)){
        String postStr = apiKey;
       
        postStr += "&field1="; 
        postStr += String(temperature);         
        postStr += "&field2=";
        postStr += String(humidity);         
        postStr += "\r\n\r\n";
        myClient.print("POST /update HTTP/1.1\n");
        myClient.print("Host: api.thingspeak.com\n");
        myClient.print("Connection: close\n");
        myClient.print("X-THINGSPEAKAPIKEY:" + apiKey +"\n");
        myClient.print("Content-Type: application/x-www-form-urlencoded\n");
        myClient.print("Content-Length: ");
        myClient.print(postStr.length());
        myClient.print("\n\n");
        myClient.print(postStr);
   
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.println(" *C");
        Serial.print("Humidity: ");
        Serial.print(humidity);
        Serial.println(" %");
      }

      /* The following lines were copied from the answer to the thread
       * https://arduino.stackexchange.com/questions/39705/how-to-get-the-server-response-when-i-post-to-a-web-api
       */
      Serial.println("Server Response:");
      uint32_t lastRead = millis();
      while (millis() - lastRead < 2000){
        while (myClient.available()){
          Serial.write(myClient.read());
          lastRead = millis();
        }
      } 
     
      myClient.stop();   
      Serial.println();
    }
  }
}


Thanks to those who tried to help!
User avatar
By Dan Kiefer
#74766 Addendum:

I just realised that it's the reading of the response that makes it work. If I comment the reading out, it stops working. Maybe some body can explain why I don't have to do that when conneted to my home network.