So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By faustotesta
#82323 Hi everyone,
i'm setting up, for training purposes, a system able to send/receive data over WiFi.
It is made of:

PC(Windows) --- ADSL --- ESP8266

Expected behaviour and outcomes are as follows:

ESP8266 tries to connect to Wifi: This works fine
ESP8266 fetches temperature/humidity data and sends them to PC: This works fine
From VB.net i try to send data to ESP8266 (POST): THIS FAILS

Application on PC is below. Instruction in bold is the one where the following error arises

("... Impossibile stabilire la connessione. Rifiuto persistente del computer di destinazione 192.168.1.3:80..")


Sub postaHTTP(ByVal azione As Integer, ByVal sensore As Integer, ByVal oste As String, ByVal porta As String, ByVal defo As String)

Dim s As HttpWebRequest
Dim enc As UTF8Encoding
s = HttpWebRequest.Create(defo + oste + ":" + porta)
enc = New System.Text.UTF8Encoding()
Dim postdata As String
Dim postdatabytes As Byte()
postdata = "sensore" + CStr(sensore) + ":" + CStr(azione) '"sensore0:0"
postdatabytes = enc.GetBytes(postdata)
s.Method = "POST"
s.ContentType = "application/x-www-form-urlencoded"
s.ContentLength = postdatabytes.Length
Dim posta As Stream = s.GetRequestStream()
posta.Write(postdatabytes, 0, postdatabytes.Length)
posta.Close()
MsgBox("Done", vbOKOnly, "Data sent")

End Sub

This is the piece of code in ESP8266 written/compile/loaded by means of ARDUINO IDE

boolean StartWiFi()
{

Serial.println(WiFi.localIP());
WiFi.disconnect();
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
Serial.println(ssid);
Serial.println(password);
while (WiFi.status() != WL_CONNECTED && WiFiCounter < 30) {
delay(dilei_uaifai);
WiFiCounter++;
Serial.print("*");
}
if (WiFiCounter < 30) {
return true;
} else {
return false;
}
delay(dilei_uaifai);
}

If i try to POST data to other destinations (i.e. 192.168.1.1or BBC.CO.UK) then it works.
Can anyone help ?
Thanks in advance
Fausto
User avatar
By QuickFix
#82331
quackmore wrote:Am I right?

Apart from that: that code won't do anything at all; there's no setup();- and loop();- block which are also required in Arduino code. :?

All this code would do (if it even compiles/runs on its own) is do some WiFi connecting and/or delay/looping: there's no actual work (as in heavy lifting) done.

@faustotesta: please show us your entire sketch, not just a part you think might be wrong. :idea:
User avatar
By faustotesta
#82341 Hi all,
here is the entire sketch:

#include <Wire.h> //I2C bus
#include <ESP8266WiFi.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <ESP8266HTTPClient.h>;
#include<stdlib.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

#define DHTTYPE DHT11
DHT dht(D2, DHTTYPE);


int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value


String oste = "192.168.1.6";
String Porta_HTTP = "13000";

int WiFiCounter = 0;
int porta_gialla = D7; // YELLOW
int porta_rossa = D6; // RED
int porta_verde = D5; // GREEN
int porta_temp_umid = D2;
int porta_relay = D1;


int httpCode;
String payload;
int acceso = HIGH;
int spento = LOW;
int porta;
int valore;
int dilei_lettura = 500;
int dilei_ciclo = 1000;
int dilei_uaifai = 1000;
int temp_umid;
int i;
String ssid = "VodafoneCASA";
String password = "#*HappyHead#1";
String slogan = "Wifi failure";
String cmd = "";
int azione;
int dilei_variabile;

boolean connesso = false;
long bodreit = 115200;
WiFiClient client;

void setup()
{
Serial.begin(bodreit);
dht.begin();
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
Serial.println("Connecting....");
}

pinMode(LED_BUILTIN, OUTPUT);
pinMode(porta_rossa, OUTPUT);
pinMode(porta_verde, OUTPUT);
pinMode(porta_gialla, OUTPUT);
pinMode(porta_temp_umid, INPUT);
digitalWrite(porta_verde, spento);
delay(dilei_lettura);
digitalWrite(porta_rossa, spento);
delay(dilei_lettura);
digitalWrite(porta_gialla, acceso);


connesso = StartWiFi();
Serial.println("IP : ");
Serial.println(WiFi.localIP());
delay(3000);

}


void loop() {
i = 0;
HTTPClient http;
hum = dht.readHumidity();
temp = dht.readTemperature();
Serial.print("Temperature (digital) : ");
Serial.println(temp);
Serial.print("Humidity : ");
Serial.println(hum);

if (WiFi.status() == WL_CONNECTED)
{
LeggiSeriale();
delay(dilei_lettura);
digitalWrite(porta_gialla, spento);
delay(dilei_lettura);
porta = porta_verde;
digitalWrite(porta, acceso);
Serial.println("Sending over the network...");
cmd = "temd:" + String(temp);
Manda(httpCode, payload, oste, cmd, Porta_HTTP);
cmd = "humi:" + String(hum);
Manda(httpCode, payload, oste, cmd, Porta_HTTP);
}
else
{
digitalWrite(porta_gialla, spento);
delay(dilei_lettura);
Serial.println("I'm offline..");
porta = porta_rossa;
digitalWrite(porta, acceso);
delay(dilei_lettura);
Serial.println("Attempting connection to WiFi");
connesso = StartWiFi();
}


delay(dilei_lettura);
digitalWrite(LED_BUILTIN, spento);
delay(dilei_lettura);
digitalWrite(LED_BUILTIN, acceso);
delay(dilei_lettura);
digitalWrite(porta, spento);
delay(dilei_lettura);
digitalWrite(porta_gialla, acceso);
delay(dilei_lettura);

delay(dilei_ciclo);

}


boolean StartWiFi()
{

Serial.println(WiFi.localIP());
WiFi.disconnect();
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
Serial.println(ssid);
Serial.println(password);
while (WiFi.status() != WL_CONNECTED && WiFiCounter < 30) {
delay(dilei_uaifai);
WiFiCounter++;
Serial.print("*");
}
if (WiFiCounter < 30) {
return true;
} else {
return false;
}
delay(dilei_uaifai);
}

void Manda(int httpCode, String payload, String oste, String cmd, String porta_HTTP)
{
HTTPClient http;
http.begin("http://" + oste + ":" + Porta_HTTP + "/hello"); //Specify request destination
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
httpCode = http.POST(cmd); //Send the request
payload = http.getString(); //Get the response payload
Serial.print("ACK from Web :");
Serial.println(payload);
Serial.print("length of ACK :");
Serial.println(payload.length());


}

void LeggiSeriale()
{
word seriale=client.read();
Serial.println(seriale,HEX);
}