Arduino module esp8266 returns bad request - http

I am testing the wifi module esp8266 with my arduino uno.
I made it work with direct connection RX/TX and through softwareserial.
This is my code:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(3, 2); // RX | TX
#define DEBUG true
int ERROR_PIN = 7;
int OK_PIN = 6;
char serialbuffer[400];//serial buffer for request url
const String ssid = "XXX";
const String pw = "XXX";
int state = 0;
void setup()
{
delay(1000);
/**
// init leds
pinMode(ERROR_PIN, OUTPUT);
pinMode(OK_PIN, OUTPUT);
state = 0;
digitalWrite(ERROR_PIN, HIGH);
digitalWrite(OK_PIN, LOW);
/**/
// init ports
Serial.begin(19200);
Serial.println("initializing esp8266 port...");
esp8266.begin(19200);
delay(400);
// init WIFI
/**/
while(!esp8266.available())
{
Serial.print("...");
delay(300);
}
Serial.println();
Serial.println("FINISH esp8266 initializing!");
//
/**
digitalWrite(ERROR_PIN, LOW);
digitalWrite(OK_PIN, HIGH);
state = 1;
/**/
/**/
// Setup connection
sendData("AT+RST\r\n",2000,DEBUG);
sendData("AT+CWMODE?\r\n",1000,DEBUG);
//sendData("AT+CWMODE=1\r\n",2000,DEBUG);
//sendData("AT+RST\r\n",3000,DEBUG);
//sendData("AT+CWLAP\r\n",6000,DEBUG);
sendData("AT+CWJAP=\"" + ssid + "\",\""+ pw +"\"\r\n",12000,DEBUG);
sendData("AT+CIFSR\r\n",8000,DEBUG);
sendData("AT+CIPMUX=1\r\n", 6000, DEBUG);
webRequest("");
/**/
/**/
}
void loop()
{
if (esp8266.available())
{
char c = esp8266.read() ;
Serial.print(c);
/**
if(state == 0)
{
state = 1;
digitalWrite(ERROR_PIN, LOW);
digitalWrite(OK_PIN, HIGH);
}
/**/
}
else
{
/**
if(state > 0)
{
state = 0;
digitalWrite(ERROR_PIN, HIGH);
digitalWrite(OK_PIN, LOW);
}
/**/
}
if (Serial.available())
{
char c = Serial.read();
esp8266.print(c);
}
}
//////////////////////////////////////////////////////////////////////////////
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
//////////////////////////////////////////////////////////////////////////////////
String webRequest(String url)
{
String response = "";
url = "www.google.es";
//String tmpCommand = "AT+CIPSTART=4," + "\"TCP\",\"" + url + "\",80";
String tmpSTARTCommmand = "AT+CIPSTART=0,\"TCP\",\"retro.hackaday.com\",80\r\n\r\n";
String tmpGETCommand = "GET / HTTP/1.1\r\nHost: ";
tmpGETCommand += "retro.hackaday.com";
tmpGETCommand += ":80\r\n\r\n";
String tmpSENDCommand = "AT+CIPSEND=0," + String(tmpGETCommand.length()) + "\r\n";
sendData(tmpSTARTCommmand, 8000, DEBUG);
sendData(tmpSENDCommand, 8000, DEBUG);
sendData(tmpGETCommand, 15000, DEBUG);
}
This works until the point where I do the webrequest. I receive a Bad request response.
initializing esp8266 port...
.........
FINISH esp8266 initializing!
BâÂúØÐPÊþ^X8Â�Ä^Âú[8ÐûÈâ·CâËØè[8Ð{Èâ·GâÃØRÈ蚉5˜‰0
bÕ
ready
AT+CWMODE?
+CWMODE:3
OK
AV®)AB•«Ë—mX·et","XXX"
OK
AT+CIFSR
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:9b:c3:83"
+CIFSR:STAIP,"192.168.1.89"
+CIFSR:STAMAC,"18:fe:34:9b:c3:83"
OK
AT+CIPMUX=1
OK
AV%AMEÕÕ*$‘²troÐ…�‘½µ‰,80
0,CONNECT
OK
AVCIPSEND=0,47
> GE#/!QQAŠrŠ%åõÑ: ÊÑɽB�……¹½µÂ‚%\n\r\nbusy s...
SEND OK
+IPD,0,323:HTTP/1.1 400 Bad Request
Server: nginx/1.6.2
Date: Sat, 04 Apr 2015 16:17:29 GMT
Content-Type: text/html
Content-Length: 172
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
OK
"qXÑzÂC!É1âø‚h[•�™cü ÐQ!}Ñfócú I]Ø÷ÃBj 1¤(ÑÃÖa”K!~CóbÕ
ready
Any idea?

I secceded using this code:
https://github.com/itead/ITEADLIB_Arduino_WeeESP8266/blob/master/ESP8266.cpp
but I have mega2560..
hardware connection:
https://github.com/McOffsky/Arduino_ESP8266_HTTP_Client

Check if it works perfectly, if you use Hardware serial Rx(pin0), Tx(pin1) and Serial monitor, ESP8266 responds properly.
Try lowering baud rate. Most the time SoftwareSerial and
Arduino Uno are not able to handle fast response and loose bit of
information.
If you still receiving the garbage response most likely you module have got damaged.

You shouldn't include port number in get command. Port already specified at cipstart.Something wrong in your http request text. Everything else (sending request and getting response )is ok.
http://en.m.wikipedia.org/wiki/Hypertext_Transfer_Protocol

Check your power supply. I have had these random characters showed up every once in a while when I simply connected the module directly to the Arduino power pins. Those seems like the module is resetting itself constantly and spits out the module info.
My solution was to connect another 3.3v regulator in parallel with Arduino. Connect the grounds together, and supply ESP8266 from that 3.3v power source. Problem solved.

Related

Communication between Postman and Arduino

I plan to send from a server a POST request to my Arduino Uno WiFi Rev2. More correctly, when the server sends this request a servomotor controlled by the Arduino should start moving. Right now, I'm using Postman to try and connect with the Arduino but I can't get it to work. So first I connect the Arduino to WiFi using my smartphone as a hotspot. This should be the unit's IP address, right?
I then try to send a POST request to this IP but it doesn't work. I'm also unsure which port number I should use, so I have just been trying with the standard ones (80, 4430, etc).
What am I doing wrong and how should I proceed?
EDIT: Here is my code.
#include <SPI.h>
#include <Servo.h>
#include <WiFiNINA.h>
char ssid[] = "MyNetwork"; // The network SSID
char pass[] = "testtest"; // The network password
int status = WL_IDLE_STATUS; // The Wifi radio's connection status
Servo servo_9; // Initializes the servomotor
WiFiServer server(80); // Server socket
//WiFiClient client;
WiFiClient client = server.available();
void setup() {
// Connects the servomotor to pin 9
servo_9.attach(9, 500, 2500);
// Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}
enable_WiFi();
connect_WiFi();
server.begin();
printCurrentNet();
printWifiData();
}
void loop() {
// Check the network connection once every 10 seconds:
delay(10000);
client = server.available();
if(client){
printWEB();
}
}
void enable_WiFi(){
// Check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// Don't continue
while (true);
}
// Check if the latest Firmware version is installed
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
}
void connect_WiFi(){
// 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);
// Wait 10 seconds for connection:
delay(10000);
}
// Now the arduino is connected, so print out the data:
Serial.print("You're connected to the network: ");
Serial.println();
}
void printCurrentNet() {
// Print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// Print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI): ");
Serial.println(rssi);
// Print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type: ");
Serial.println(encryption, HEX);
Serial.println();
}
void printWifiData() {
Serial.println("Your board's IP and MAC address: ");
// Print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// Print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
Serial.println();
}
// Find the MAC adress for your Arduino board
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
void printWEB() {
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void servomotorGate(){
int position = 0;
for (position = 0; position <= 90; position += 1) {
servo_9.write(position);
Serial.println("Opening the gate");
}
delay(5000); // Wait for 5000 millisecond(s)
for (position = 90; position >= 0; position -= 1) {
servo_9.write(position);
Serial.println("Closing the gate");
}
}
I added the server client to listen for connections. However, the main reason why I couldn't connect to my IP address with Postman was that I was trying to get WiFi from my phone as a hotspot. I guess there is a problem with the port forwarding when using a hotspot. In the end, I solved the issue by connecting to a normal router WiFi network.

Why Node-red Debug node doesn't show my mqtt message?

I'm trying to send data from my Wemos to Node-red via MQTT. I created a nested object I want to send to MQTT. From the serial of Arduino IDE the output is this (and that's what I want):
[{"AcX":-1,"AcY":-1,"AcZ":-1},{"AcX":-1,"AcY":-1,"AcZ":-1},{"AcX":-1,"AcY":-1,"AcZ":-1},{"AcX":-1,"AcY":-1,"AcZ":-1},{"AcX":-1,"AcY":-1,"AcZ":-1}]
It seems all correct but the debug node show nothing. what am I missing?
Here's the code:
//LIBRARY
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <PubSubClient.h>
#include<Wire.h>
//JSON Array and mqtt data
#include <ArduinoJson.h>
#define MQTT_BUFFER 512
//MPU
const int MPU = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ;
// IP adress Raspberry Pi
const char* mqttServer = "raspi-hyperink";
const int mqttPort = 1883;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
//MPU
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
//SERIAL
Serial.begin(115200);
// WiFiManager
// Local intialization.
WiFiManager wifiManager;
// Uncomment and run it once, if you want to erase all the stored information
//wifiManager.resetSettings();
// set custom ip for portal
//wifiManager.setAPConfig(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");
// if you get here you have connected to the WiFi
Serial.println("Connected.");
//server.begin();
//CLIENT
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
server.begin();
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
//RECONNECT FUNCTION
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("esp8266")) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic", "hello world");
// ... and resubscribe
//client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop(){
WiFiClient(espClient) = server.available(); // Listen for incoming clients
if (espClient) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (espClient.connected()) { // loop while the client's connected
if (espClient.available()) { // if there's bytes to read from the client,
char c = espClient.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// Web Page Heading
client.println("<body><h1>ESP8266 Web Server</h1>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
espClient.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
if (!client.connected()) {
reconnect();
}
client.loop();
//compute the required size
const size_t CAPACITY = JSON_ARRAY_SIZE(5) + 5*JSON_OBJECT_SIZE(3);
//allocate the memory for the document
StaticJsonDocument<CAPACITY> doc;
//Create an empty array
JsonArray arr = doc.to<JsonArray>();
//definiamo quanti campioni registrare prima di inviarli tramite mqtt
for (int i=0; i<5; i++){
//MPU reading
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
//Create JSON Array
JsonObject obj = doc.createNestedObject();
obj["AcX"] = AcX;
obj["AcY"] = AcY;
obj["AcZ"] = AcZ;
delay(3000);
}
//MQTT PUBLISHING JSON PACKAGE
char mqttData[MQTT_BUFFER];
serializeJson(doc, mqttData);
Serial.println(mqttData);
client.publish("esp8266", mqttData);
//client.subscribe("esp8266");
client.subscribe("esp8266");
delay(10000);
//Inserire un ciclo WHILE dove
//mentre il wifi è connesso invia i dati all'mqtt
//altrimenti li salva e basta
}
This is the mqtt node
Node-red debug
Normally debug should show you what you are publishing to your MQTT broker. As simple troubleshooting I would start moving backwards:
Replace the NodeRED client by any other one. Is the problem still there? Very likely it will be meaning that the problem is not on the MQTT client.
So we move a step "backwards"
Replace the MQTT Broker, use another one from the internet, one that you know works fine. The problem, is it still there? If it's not there, voilà, you found the problem (the broker), if it's still there, it means that the issue is on client publishing your msgs. It might be the msg itself.
So we move a step "backwards"
Replace you msg by another one, much simpler. Does it work ?
You get the idea :)

RC522 with ESP8266 not working Arduino uno

I have written below code for Arduino Uno to scan an RFID card using a RC522 module and an ESP8266 module to connect to my router.
Now when I scan any card it should read the card number and send a request to my server's IP address and get the response.
But after successfully reading the RFID card, the connection to the server via TCP does not work:
esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
So I am not able to send a request to my server.
But when I remove the RC522 code (for testing) it is working!
What is the problem using the RC522 and the ESP8266 together?
#include <SPI.h>
#include <MFRC522.h>
#include "SoftwareSerial.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//I2C pins declaration
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define SS_PIN 9
#define RST_PIN 7
#define mainLock 2
String ssid = "MYSSID";
String password = "PASSWORD";
SoftwareSerial esp(2, 3);// RX, TX
String server = "192.168.1.102"; //Your Host
String uri = "/get_data.php?rfid_key=";
//#define LED_G 4 //define green LED pin
//#define LED_R 5 //define red LED
#define BUZZER 6 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String rfidUid = "";
#define DEBUG true
void setup()
{
// Serial.begin(9600); // Initiate a serial communication
esp.begin(9600);
Serial.begin(9600);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
connectWifi();
httpget();
delay(1000);
Serial.println("Put your card to the reader...");
Serial.println();
pinMode(mainLock, OUTPUT);
lcd.begin(16, 2); //Defining 16 columns and 2 rows of lcd display
lcd.backlight();//To Power ON the back light
}
void connectWifi() {
sendData("AT+RST\r\n", 2000, DEBUG); //This command will reset module to default
sendData("AT+CWMODE=3\r\n", 1000, DEBUG);
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
esp.println(cmd);
delay(4000);
if (esp.find("OK")) {
Serial.println("Connected!");
}
else {
Serial.println("Cannot connect to wifi ! Connecting again...");
connectWifi();
}
}
/////////////////////////////GET METHOD///////////////////////////////
void httpget() {
// // Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
Serial.print("UID tag :");
String content = "";
byte letter;
rfidUid = "";
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte, HEX);
content.concat(String(mfrc522.uid.uidByte < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte, HEX));
rfidUid += String(mfrc522.uid.uidByte < 0x10 ? "0" : "");
rfidUid += String(mfrc522.uid.uidByte, HEX);
}
Serial.println();
content.toUpperCase();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(rfidUid);
esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
if ( esp.find("OK")) {
Serial.println("TCP connection ready");
} delay(1000);
if ( esp.find("OK")) {
Serial.println("TCP connection ready");
} delay(1000);
String getRequest =
"GET " + uri + rfidUid + " HTTP/1.0\r\n" +
"Host: " + server + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n";
String sendCmd = "AT+CIPSEND=";
esp.print(sendCmd);
esp.println(getRequest.length());
delay(500);
if (esp.find(">")) {
Serial.println("Sending..");
esp.print(getRequest);
if (esp.find("SEND OK")) {
Serial.println("Packet sent");
while (esp.available()) {
String response = esp.readString();
Serial.println("response.." + response);
}
esp.println("AT+CIPCLOSE");
}
}
}
void loop()
{
httpget();
}
String sendData(String command, const int timeout, boolean debug) // Function to send the data to the esp8266
{
String response = "";
esp.print(command); // Send the command to the ESP8266
long int time = millis();
while ( (time + timeout) > millis()) // ESP8266 will wait for some time for the data to receive
{
while (esp.available()) // Checking whether ESP8266 has received the data or not
{
char c = esp.read(); // Read the next character.
response += c; // Storing the response from the ESP8266
}
}
if (debug)
{
Serial.print(response); // Printing the response of the ESP8266 on the serial monitor.
}
return response;
}
Forum link - https://forum.arduino.cc/index.php?topic=538180.0
Unfortunately you are not showing the differences between the code that is working (partially, TCP connection) and the one that is not (at all, or just the TCP connection?).
when i remove the Rc522 code testing then it is working !
Just try to describe what is working and what isn't a little more in detail. You could also provide your debug output and add some comments.
Also I'd try removing more non-essential code (like the display) to help narrow down the cause. This might already get you on the right track to fix it yourself, but it would also make your code here easier to read (also see How to create a Minimal, Complete, and Verifiable example though I doubt many will have the exact parts to reproduce your problem - but you might get lucky).
But, from the code you've provided, here's just a guess:
The Arduino might not be receiving data from the ESP8266 module because you are reconfiguring the RX pin as output (but you're not using it!?):
#define mainLock 2
...
pinMode(mainLock, OUTPUT);
conflicts with
SoftwareSerial esp(2, 3);// RX, TX
So I'd recommend double-checking your pin use and connections (also, providing this information would increase your chances of somebody spotting an error).

Broker messages that recurs with nodeMCU

I’m having some problem with a home automation project of mine. I bought a nodeMCU v3 from aliexpress that i want to control my blinds with.
This is the code I’m using on it. I use the Arduino IDE to push this code in to the nodeMCU.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SimpleTimer.h>
// MQTT Server
const char* ssid = "****";
const char* password = "****";
const char* mqtt_server = "****";
char message_buff[100];
int photoValue = 0;
int rainValue = 0;
int photo = A0;
int rain = D6;
int relayUp = D7;
int relayDown= D8;
long interval = 10000;
long previousMillis = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void setup() {
pinMode(photo, INPUT);
pinMode(rain, INPUT);
pinMode(relayUp, OUTPUT);
pinMode(relayDown, OUTPUT);
digitalWrite(relayUp ,LOW);
digitalWrite(relayDown, LOW);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
if (client.connect("ESP8266Client")) {
client.subscribe("home/relayBlinds");
} else {
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
// Connect (or reconnect) to mqtt broker on the openhab server
reconnect();
}
// Read Photo- and Rain-sensors
photoValue = analogRead(photo);
rainValue = analogRead(rain);
// publish Temperature reading every 10 seconds
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
// publish Photo
String pubStringPhoto = String(photoValue);
pubStringPhoto.toCharArray(message_buff, pubStringPhoto.length()+1);
client.publish("home/photo", message_buff);
// publish Rain
String pubStringRain = String(rainValue);
pubStringRain.toCharArray(message_buff, pubStringRain.length()+1);
client.publish("home/rain", message_buff);
}
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
// MQTT inbound Messaging
int i = 0;
// create character buffer with ending null terminator (string)
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
if (msgString == "BLINDSUP") {
digitalWrite(relayUp ,HIGH);
delay(5000);
digitalWrite(relayUp ,LOW);
} else if (msgString == "BLINDSDOWN") {
digitalWrite(relayDown ,HIGH);
delay(5000);
digitalWrite(relayDown ,LOW);
}
}
The plan was to have a Raspberry Pi with openHAB as a controller. I have used several guides to setup mosquitto and openHAB and i always get the same result.
So this is what happens: the nodeMCU connects to my Wifi and publishes both the rain and photo values. I can read them in the openHAB GUI without problems.
When i press the activation button in openHAB to publish BLINDSUP or BLINDSDOWN the messages arrives without any problems and i can see the message on my mosquitto terminal. Now is when the unexpected result starts happening. The same message gets delivered multiple times to my nodeMCU without it showing up in the mosquitto terminal.
I have been trying to find out why it would act this way and I think it is because the line:
if (!client.connected()) {
is false and the nodeMCU reconnects and gets the same message somehow. But it is always the first message. If I send BLINDSUP and then BLINDSDOWN it will only register BLINDSUP forever.
I'm really out of ideas how to fix this and would appreciate any help, thanks.
URL to the nodeMCU if that helps anyhow: nodeMCU
Try connecting to MQTT broker with a clean session. Probably you published the topic with the retain flag set to true.
If you do like this, the broker will deliver the last retained message when the nodeMCU connects to the broker and subscribes to the retained topic.

DHT22 (sensor) to Pachube via Arduino with Ethernet shield error

I'm trying to connect a DHT22 sensor to the Pachube online service.
I am understanding the code and have everything wired up correctly but I get this error:
DHT22 Library Demo
Requesting data at 6335
Sync Timeout
What does sync timeout mean? Is is a network problem?
How could I fix this?
Here is my code anyway:
/* Feed temperature and humidity to Pachube.
Based on the following examples:
Sample code from nethoncho's DHT22 library:
https://github.com/nethoncho/Arduino-DHT22
Tom Igoe's PachubeClient:
http://arduino.cc/en/Tutorial/PachubeCient
*/
#include <DHT22.h>
#include <SPI.h>
#include <Ethernet.h>
// Data wire is plugged into port 7 on the Arduino
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
#define DHT22_PIN 2
// Setup a DHT22 instance
DHT22 myDHT22(DHT22_PIN);
static unsigned long lWaitMillis;
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// assign an IP address for the controller:
byte ip[] = {
192,168,0,30 };
byte gateway[] = {
192,168,1,2};
byte subnet[] = {
255, 255, 255, 0 };
// The address of the server you want to connect to (pachube.com):
byte server[] = {
173,203,98,29 };
// initialize the library instance:
Client client(server, 80);
boolean lastConnected = false; // state of the connection last time through the main loop
const long postingInterval = 180000; //delay between updates to Pachube.com
int backoff = 0;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("DHT22 Library Demo");
// start the ethernet connection:
Ethernet.begin(mac, ip);
// give the ethernet module time to boot up:
delay(1000);
lWaitMillis = millis() + 5000;
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
while(client.status() != 0) {
Serial.print("Client status: ");
Serial.println(client.status());
delay(5);
}
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (long)( millis() - lWaitMillis ) >= 0 ) {
if (readData()) {
int temp = myDHT22.getTemperatureC();
int humidity = myDHT22.getHumidity() + .5;
sendData(temp, humidity);
}
lWaitMillis += postingInterval;
if (lWaitMillis < millis()) {
lWaitMillis = millis() + postingInterval;
}
Serial.print("Next attempt at ");
Serial.println(lWaitMillis);
Serial.println();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
boolean readData()
{
DHT22_ERROR_t errorCode;
Serial.print("Requesting data at ");
Serial.println(millis());
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
Serial.print("Got Data ");
Serial.print(myDHT22.getTemperatureC());
Serial.print("C ");
Serial.print(myDHT22.getHumidity());
Serial.println("%");
return true;
break;
case DHT_ERROR_CHECKSUM:
Serial.print("check sum error ");
Serial.print(myDHT22.getTemperatureC());
Serial.print("C ");
Serial.print(myDHT22.getHumidity());
Serial.println("%");
break;
case DHT_BUS_HUNG:
Serial.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial.println("Polled too quick ");
break;
}
return false;
}
// this method makes a HTTP connection to the server:
void sendData(int temp, int humidity) {
// if there's a successful connection:
if (client.connect()) {
backoff = 0;
Serial.println("connecting...");
// send the HTTP PUT request.
// fill in your feed address here:
client.print("PUT /v2/feeds/36800.csv HTTP/1.1\n");
client.print("Host: api.pachube.com\n");
// fill in your Pachube API key here:
client.print("X-PachubeApiKey: a6714b6a217827edadfd003843c03c259a08add554eda3871b844612eddc6819\n");
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
int thisLength = 2 + getLength(temp) + 2 + 2 + getLength(humidity);
client.println(thisLength, DEC);
// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close\n");
// here's the actual content of the PUT request:
client.print(0, DEC);
client.print(",");
client.println(temp, DEC);
client.print(1, DEC);
client.print(",");
client.println(humidity, DEC);
}
else {
// if you couldn't make a connection:
Serial.println("connection failed, resetting.");
Ethernet.begin(mac, ip);
delay(1000);
client.stop();
delay(1000);
}
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
You're running into DHT_ERROR_SYNC_TIMEOUT sensor error, that means that the DHT sensor is running into some sync problem, I guess.
What arduino are you using? Is your board's frequence 8 or 16Mhz?
Give a try to the edit described here, too. If it still doesn't work, I would try using sensor itself (i.e. without connecting to patchube) with some test sketch you can easily find for DHT22, just to make sure the sensor is working properly.
I had similar problem when using Arduino Nano v3.0 + ENC28J60 Ethernet shield. I tried to connect RF receiver to digital PIN #2 but this never worked.
Then I used different pin for the RF module (PIN #4 in my case) and everything worked fine.

Resources