bportaluri / WiFiEsp

Arduino WiFi library for ESP8266 modules
GNU General Public License v3.0
548 stars 210 forks source link

client not sending the data packet #196

Open KremSH opened 4 years ago

KremSH commented 4 years ago

Hello , I am having a problem sending a HTTP PUT request to a server , here's my code:

#include <SoftwareSerial.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <hp_BH1750.h>
#include "WiFiEsp.h"

#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(19, 18); // RX, TX
#endif

char ssid[] = "";            // your network SSID (name)
char pass[] = "";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
// Initialize the client object
WiFiEspClient client;
String webString1 = "";
String luxString = "";
String moistString = "";
float Deg = 0.0;
String directionString = "";
String message = "";
bool messageReady = false;
int Vvalue = 0;
char server[] = "api.jsonbin.io";
int httpCode = 0;

void setup() {
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);
    // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");

  Wire.begin();
  pinMode(A0, INPUT); //soil
  Serial.println("My program is starting");
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }

  bool avail = BH1750.begin(BH1750_TO_GROUND);
}

void loop() {
  Serial.println();
  printCurrentNet();
  printWifiData();
  BH1750.start();
  float lux = BH1750.getLux();
  //Illuminance
  if (90000.0 <= lux <= 120000.0) {
    luxString = "Bright Sunlight";
  }
  else if (15000.0 <= lux <= 90000.0) {
    luxString = "Blue Sky";
  }
  else if (5000.0 <= lux <= 15000.0) {
    luxString = "Slightly Cloudy";
  }
  else if (1000.0 <= lux <= 5000.0) {
    luxString = "Overcast";
  }
  else if (500.0 <= lux <= 1000.0) {
    luxString = "Rainstorm";
  }

 Vvalue = analogRead(3);
Wind Direction
 Deg = (360 * Vvalue) / 1023;
  Vvalue = 200;
  if (0.0 <= Deg < 30) {
    directionString = "North";
  }
  else if (30.0 <= Deg < 60.0) {
    directionString = "North-East";
  }
  else if (60.0 <= Deg < 100.0) {
    directionString = "East";
  }
  else if (100.0 <= Deg < 150.0) {
    directionString = "South-East";
  }
  else if (150.0 <= Deg < 190.0) {
    directionString = "South";
  }
  else if (190.0 <= Deg < 230.0) {
    directionString = "South-West";
  }
  else if (230.0 <= Deg < 280.0) {
    directionString = "West";
  }
  else if (280.0 <= Deg < 320.0) {
    directionString = "North-West";
  }
  else if (320.0 <= Deg < 360.0) {
    directionString = "North";
  }
  int moist = analogRead(A0);
  if (moist >= 1000) {
    moistString = "Sensor is not in the Soil or DISCONNECTED";
  }
  if (moist < 1000 && moist >= 600) {
    moistString = "Soil is DRY";
  }
  if (moist < 600 && moist >= 370) {
    moistString = "Soil is HUMID";
  }
  if (moist < 370) {
    moistString = "Sensor in WATER";
  }
  webString1 = "{\"Weather\":[{\"Type\": \"Temperature\" , \"Value\": " + String((float)bme.readTemperature()) + "},{\"Type\": \"Humidity\" , \"Value\": " + String((float)bme.readHumidity()) + "},{\"Type\": \"Pressure\" , \"Value\": " + String((float)bme.readPressure() / 100.0F) + "},{\"Type\": \"Altitude\" , \"Value\": " + String((float)bme.readAltitude(SEALEVELPRESSURE_HPA)) + "} , {\"Type\": \"Wind Direction\" , \"Value\": " + String((float)Deg) + "°, " + directionString + "},{\"Type\": \"Lux\" , \"Value\": " + String((float)lux) + "lux , " + luxString + "},{\"Type\": \"Moisture\" , \"Value\": " + String((int)moist) + " , " + moistString + "}]}";
       if (client.connect(server, 443)) {
    Serial.println("Connected to server");
    }
    char c = client.read();
    Serial.println(c);
    client.println("Content-Type: application/json");
    client.println("secret-key: ");
    client.println("PUT b/5e9ec849435f5604bb4553b8 HTTP/1.1");
  client.println("Host: api.jsonbin.io:8080");
   client.println("Accept: */*");

    client.println();
    client.println(webString1);
    c = client.read();
    Serial.println(c);
  delay(5000);

}`

The Wifi connects and everything and the server connects as well but the error I recieve is :

unknown

I also tried using connectSSL() however when I use that i get a timeout error before even connecting to the server

Can someone help please ?

JAndrassy commented 4 years ago

it must be connectSSL. SoftwareSerial and this library don't work at 115200 baud. set 9600. in firmware too