OV2640图像上传到云端OneNet
本帖最后由 零知实验室-roc 于 2019-1-12 19:29 编辑原贴:零知板+OV2640图像上传到云端OneNet ESP8266 wifi EDP协议
http://www.lingzhilab.com/forum.php?mod=viewthread&tid=911&fromuid=2
(出处: 零知实验室)
本实验使用之前的零知增强板OV2640获取图像,ESP8266 WIFI传输等试验知识,我们把获取的图像传输到云端OneNet平台。
1、硬件连接:
零知增强板自带OV2640摄像头接口,直接接入即可;零知-增强板与ESP8266模块连接进行wifi传输,可以使用串口也可以使用SPI方式,本次实验使用SPI接口,接线方式查看之前的零知增强板+WIFI模块传输实验。
2、代码编写
零知增强板获取图像数据的代码请查看之前的帖子,这里重点是上传数据到云端,主要分为:建立客户端,连接EDP服务器,向平台请求连接,向云端上传数据。具体代码如下:
/*
WiFiSPI example: esp8266连接AP,UDP传输数据
电路连接:
1. On ESP8266 must be running (flashed) WiFiSPIESP application.
2. Connect the Arduino to the following pins on the esp8266:
ESP8266 |
GPIO NodeMCU Name| Lingzhi Enhanced Board
===================================
15 D8 SS | 53
13 D7 MOSI| 51
12 D6 MISO| 50
14 D5 SCK | 52
*/
#include "WiFiSpi.h"
#include "WiFiSpiUdp.h"
#include "cJSON.h"
#include "Common.h"
#include "EdpKit.h"
#include "camera.h"
#include <fsmc_lcd.h>
CAMERA_DCMI_INTERFACE camera;
unsigned int localPort = 80;
//OneNet平台相关参数
char API_KEY[] = "API key";//你的oneNet上的api key
char DEVICE_ID[]= "device id"; //你的设备ID
#define JPG_DATA "ov2640_jpg"
#define EDPserver "jjfaedp.hedevice.com"
#define ssid "lingzhilab"//换成自己的wifi热点
#define password "lingzhilab.com"
//char udpServerIP[] = "183.230.40.34";
char serverIP[] = "192.168.0.126";//for testing
int edpServerPort = 876;
WiFiSpiClient client;
uint8_t connect_server_flag = 0;
#ifdef JPEG_OUTPUT_MODE
uint32_t jpeg_buf; //JPEG数据缓存buf
extern volatile uint8_t jpeg_data_ok;
extern volatile uint32_t jpeg_data_len;
#endif
void connectEDP();
void sendPacket(uint8_t *jpg_data, uint32_t len);//发送一帧的图像
void setup()
{
// initialize serial for debugging
Serial.begin(9600);
// initialize the ESP module
WiFiSpi.init();
// 检查是否连接了运行SPIWifi程序的8266模块
if (WiFiSpi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
int status = WL_IDLE_STATUS;
// attempt to connect to WiFi network
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to wifi with SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFiSpi.begin(ssid, password);
}
// 已经连接到wifi
Serial.println("You're connected to the network");
if (client.connect(EDPserver, edpServerPort)) {
Serial.println("connected to server");
}else{
Serial.println("connect server failed");
while(1);
}
//开启camera,这里传输jpg图像
camera.begin(OV2640_DEVICE_ID);
#ifdef JPEG_OUTPUT_MODE
camera.setOutSize(240,320);
camera.startCapture((uint32_t)&jpeg_buf);
#endif
//请求连接EDP服务器
connectEDP();
}
void loop()
{
uint8_t *p;
//发送摄像头数据
if(jpeg_data_ok==1) //
{
p=(uint8_t*)jpeg_buf;
Serial.println("send one frame data...");
//去掉开头的无效数据,有时候前面一堆0,??
bool start_flag = false;
int start_index = 0;
for(int i=0; i<jpeg_data_len*4; i++)
{
if(p == 0xff)
{
if(p == 0xd8)
{
start_index = i;
start_flag = true;
break;
}
}
}
//valid frame
if(start_flag)
sendPacket(p+start_index, jpeg_data_len*4-start_index);
jpeg_data_ok=2; //
}
delay(3000);
}
void connectEDP()
{
/*利用sdk 中EdpKit.h 中PacketConnect1 方法封包连接协议*/
EdpPacket* send_pkg = PacketConnect1(DEVICE_ID,API_KEY);
/*发送连接协议包数据*/
//int ret = DoSend (sockfd, send_pkg->_data, send_pkg->_write_pos);
int size = send_pkg->_write_pos;
Serial.print("send size:");
Serial.println(size);
client.write(send_pkg->_data, send_pkg->_write_pos);
/*使用完后必须删除send_pkg , 否则会造成内存泄漏*/
DeleteBuffer(&send_pkg);
}
// send an packet to server
void sendPacket(uint8_t *jpg_data, uint32_t len)
{
Serial.println("send jpg");
char text2[]="{\"ds_id\": \"ov2640_jpg\"}";
cJSON *desc_json;
desc_json=cJSON_Parse(text2);
EdpPacket* send_pkg = PacketSavedataBin(NULL, desc_json, jpg_data, len, 0);
Serial.println("buff size:");
Serial.print("jpg_buff:");Serial.println(len);
Serial.print("packet_size:");Serial.println(send_pkg->_write_pos);
uint8_t *send_buff = send_pkg->_data;
uint32_t send_len = send_pkg->_write_pos;
int packetLen = 1024;
int t = 0;
for(t=0; t< send_len/packetLen; t++)
{
Serial.print("ofset:");Serial.println(t*packetLen);
Serial.print("lenth:");Serial.println(packetLen);
client.write(send_buff+t*packetLen, packetLen);
delay(100);
}
Serial.print("ofset:");Serial.println(t*packetLen);
Serial.print("lenth:");Serial.println(send_len-packetLen*t);
client.write(send_buff+t*packetLen, send_len-packetLen*t);
DeleteBuffer(&send_pkg);
Serial.println("send done");
}
上述代码中,请更改自己的wifi热点连接,以及在OneNet平台建立设备时的API key和设备ID号。
3、OneNet 平台建立设备和应用进行数据显示
详细的做法请查看这篇文章:
OneNet云平台显示摄像头数据详细操作教程 EDP OV2640 ESP8266
4、测试验证
在云平台建立好应用和设备后,先用平台提供的调试工具调试好,再使用零知平台设备发送数据进行显示:
上述步骤正确完成后,我们在零知IDE串口中看到如下信息:
云端效果:
楼主你好,零知板在哪买呀?这个完整工程开源吗:) 你好,我用的是arduino的板子,这个程序可以上传图像到onenet吗?求大神指导、我用的是ov7670带FIFO的摄像头模块,求大佬指导一下,非常感谢!
页:
[1]