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

User avatar
By gbenna
#81189 I am trying to connect my ESP8266 up to my wifi. I can hard wire it through my Arduino UNO and use AT commands but it I go to another network it doesn't work. So I wired up the ESP8266 through my Arduino and uploaded wifiManager.h and other needed files onto the ESP8266. Once uploaded the ESP couldn't find my wifi so it opened the appropriate page and I input new ip address and password. Everything worked great, the ESP connected to my wifi and passed the water level sensor information along to thingSpeak. The problem is when I unplugged the Arduino and ESP8266 from the computer and then replugged them back in the ESP doesn't reconnect to my internet and send the information up to thingSpeak. I have disconnected the GPIO0 from ground and disconnected the Arduino's reset from ground but it still doesn't work. Below is my code:
Code: Select all#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
//WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;
    //reset saved settings
    //wifiManager.resetSettings();
    //set custom ip for portal
    //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifiManager.autoConnect("AutoConnectAP");
    //or use this for auto generated name ESP + ChipID
    //wifiManager.autoConnect();
pinMode(2,INPUT);
    char apiKey[20] = "";
    WiFiClient client;
    char defaultHost[100] = "184.106.153.149";  //Thing Speak IP address (sometime the web address causes issues with ESP's :/
    long itt = 500;
    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");
}
void loop() {
  delay(5000);
  pinMode(2,INPUT);
  const int waterInPin = 2;  // Analog input pin that the potentiometer is attached to
    int waterSensorInValue;//reading our water lever sensor
int waterSensorOutValue;//conversion of water sensor value
    // put your main code here, to run repeatedly:
    waterSensorInValue = analogRead(waterInPin);
  waterSensorOutValue = map(waterSensorInValue,0,1024,0,225);
 Serial.print("WaterOutValue = ");
  Serial.print(waterSensorOutValue );
  Serial.print("WaterInValue = ");
  Serial.print(waterSensorInValue );
    delay(18000);
    WiFiManager wifiManager;
   char apiKey[20] = "";
    WiFiClient client;
    char defaultHost[100] = "184.106.153.149";  //Thing Speak IP address (sometime the web address causes issues with ESP's :/
    long itt = waterSensorOutValue;
    //if you get here you have connected to the WiFi
    WiFiManagerParameter customHostServer("defaultHost", "Host Server", defaultHost, 100);
    WiFiManagerParameter customAPIKey("apiKey", "Host API Key", apiKey, 20);
    wifiManager.addParameter(&customHostServer);
    wifiManager.addParameter(&customAPIKey);
    if (client.connect(defaultHost,80))
    { // "184.106.153.149" or api.thingspeak.com
        itt++;  //Replace with a sensor reading or something useful
        String postStr = apiKey;
        postStr +="&field1=";
        postStr += String(itt);
        postStr += "\r\n\r\n";
 client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+String("XXXXXXXXXXXXXXXXXX")+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.print(postStr);
Serial.println("% send to Thingspeak");
    }
 client.stop();
  Serial.println("Waiting…");
}


Does anyone have any suggestions?